Low Pass Filter Time Constant

A common way to low pass filter:

void loop()
{
	...
	
	y += 0.04 * (raw - y);
	
	...
}

Another way to write the same code:

	y = 0.04 * raw + 0.96 * y;

Time constant is,

where dt is the execution interval and α is 0.04 in the above example.

Check with α = 0.04 and dt = 0.001 (τ = 0.024):

Above confirms that y reaches 63.2% of raw's changes in τ seconds.

First code can be rewritten so that constants have physical meanings:

// Function call frequency [Hz]
// loop() is called every 1/LOOP_FREQUENCY seconds
const double LOOP_FREQUENCY = 1000.0;

// Low pass filter time constant [sec]
// Increase for more filtering but slower response
const double TIME_CONSTANT  = 0.024;

void loop()
{
	...
	
	y += (raw - y) / (LOOP_FREQUENCY * TIME_CONSTANT + 1.0);
	
	...
}

Now, if the loop needs to run at a different interval, only LOOP_FREQUENCY needs to be changed. Filter properties will stay unaffected.