I want to read the voltage of a battery using an ADC connected to a PLC. The signal oscillates a bit and I want to smooth it. The ADC samples the signal at 860 samples per second and stores it in variable ADCValue. Then I take an ADCValue every 0.5 seconds to create an array of size four and finally every two seconds I do an average of these four values.
My understanding is that this averaging by software is equivalent to a low pass filter. Is this correct? If so, how do I calculate the cut-off frequency of it?.
Edit: find here the Arduino code related to this, which uses two timers that call the interrupt functions associated in the setup():
#include <TimerOne.h>
#include <TimerThree.h>
#include <Wire.h>
const int AVGNUMELEM = 4;
float ADCValue ; // This value comes from an external ADC and it is configured at 860 SPS
float arrayAvg[AVGNUMELEM];
float avg = 0;
int counteravg = 0;
//2 seconds
const long TIMER1TIME = 2000000;
//0.5 seconds
const long TIMER3TIME = 500000;
// Timer called every 0.5 seconds
void ISR_Store4values() {
if(counteravg < AVGNUMELEM){
arrayAvg[counteravg] = ADCValue;
counteravg ++;
}
else
{
for(int i = 0; i < (AVGNUMELEM-1); i++){
arrayAvg[i] = arrayAvg[i + 1];
}
arrayAvg[(AVGNUMELEM-1)] = ADCValue;
}
}
// Timer called to compute the average every 2 seconds
void ISR_PrintEvery2s() {
avg = 0;
for(int i = 0; i < AVGNUMELEM; i++){
avg = avg + arrayAvg[i];
}
avg = avg / AVGNUMELEM;
Serial.print(avg,4);
}
void setup() {
Timer1.initialize(TIMER1TIME);
Timer1.attachInterrupt(ISR_PrintEvery2s);
Timer3.initialize(TIMER3TIME);
Timer3.attachInterrupt(ISR_Store4values);
}
void loop() {
}