Created
January 23, 2017 10:12
-
-
Save Robotto/9aec00665dce1ba4a4a2ad1c52a24413 to your computer and use it in GitHub Desktop.
Exponential filtering of ADC measurements, with voltage calculation thrown in..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//exponentialFilter.ino | |
//example sketch to pull and filter measurements from eg. an ADC | |
//the filter has a beta value which determines the weight of new values. | |
//A high beta value will filter more noise but slow down the response of the filter. | |
static float filterBeta=32; //1, 2 , 4, 8, 16, 32, 64, 128 and so on... | |
//where is the data coming from? | |
static unsigned int adcPin=A0; | |
//the filtered value for printing or whatever: | |
unsigned long ADCfiltered=1; //default to 0V | |
//the filtered value converted to a voltage: | |
//adcFiltered ranges from 0-1023 corresponding to a voltage between 0 and adcMAXvoltage | |
static float adcMAXvoltage = 5; //5V for arduino uno, 3,3V for some nodeMCU boards, 1V for others. | |
float vADC=0; //converted value of adcFiltered | |
void setup() { | |
Serial.begin(115200); | |
//seed the filter with 4x$filterBeta measurements (eliminates the need for a settling time) | |
for(int i=0; i<=4*filterBeta; i++) ADCfiltered=((ADCfiltered*filterBeta)+analogRead(adcPin))/(filterBeta+1); //low pass filtering of ADC0 | |
} | |
void loop() { | |
ADCfiltered=((ADCfiltered*filterBeta)+analogRead(adcPin))/(filterBeta+1); //pull a value from the ADC pin and merge into the filter. | |
vADC=adcMAXvoltage/(float)1023*(float)ADCfiltered; //ADC -> voltage | |
//debug printout: | |
Serial.print("ADCfiltered: "); Serial.println(ADCfiltered); | |
Serial.print("voltage: "); Serial.println(vADC); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment