Figure 1 shows an analog RC lowpass filter and its design equations. As an alternative, you can clean up noisy signals that remain within the ADC's linear range by using the digital equivalent of an analog RC lowpass filter. The filter's software comprises only two lines of C code: LPOUT=LPACC/K, where the output value of the filter is LPACC divided by a constant, and LPACC=LPACC+LPIN–LPOUT, where you add the difference between input and output to update LPACC. You specify all variables as integers.
The lowpass accumulator, LPACC, can grow large for large time constants and large input values. It can grow as large as K times the largest possible LPIN value. Under these conditions, you need to make sure that LPACC does not overflow, and you may need to specify a larger data type to contain LPACC. To avoid a long settling time during start-up, before the start of the sampling loop, you can initialize LPACC to a value of K times the current input value.
You can extend the basic filter concept presented to accommodate higher order filters with greater high-frequency rejection by executing multiple filter code segments in sequence. Also, you can use an array of variables for LPACC and an array of values of the constant K to filter signals that multiple data channels acquire.