Instantly share code, notes, and snippets.

Embed
What would you like to do?
High Resolution Temperature Measurements with a LM35
/*
High Resoltution Temperature Measurements with a LM35
02/2017 by Madgyver
https://madgyver.de/en/2017/02/09/high-resolution-temperature-measurements-with-a-lm35/
This code example calculates the moving average of 2048 measurements of a LM35 temperature
sensor. Due to oversampling a resolution of 0.01°C can be achieved and the sensor readings
appear to be plausible.
This example code is in the public domain.
*/
#define LM35 A0 //Sensor Pin
#define ref_voltage 1.08
#define volt2temp 100
#define prop_constant ref_voltage * volt2temp / 1024
float cumulativeAverage(long input)
{
#define N 2048
static float avg_accu = 0;
avg_accu = (N * avg_accu + input) / (N + 1); //recurisve formula
return avg_accu;
}
void setup() {
// initialize serial communication at 250000 bits per second:
Serial.begin(250000);
analogReference(INTERNAL);
}
void loop() {
int sensorValue = analogRead(LM35);
// print out the value you read:
Serial.println(cumulativeAverage(sensorValue) * prop_constant,2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment