Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active Jan 24, 2020
Embed
What would you like to do?
/******************************************************************************
TMP36_averaging.ino
Written by Ho Yun "Bobby" Chan
@ SparkFun Electronics
Date: Nov 4, 2019
Description: This sketch configures temperature sensor and prints the
temperature in degrees celsius and fahrenheit. A sample set of data points
are saved in an array and averaged in an attempt to smooth out readings.
Simply adjust the `output_select` to view the °C, °F, or both. Open the
Serial Monitor or Plotter at 115200 baud to view the data.
Development Environment Specifics:
Arduino 1.8.9+
License:
This code is released under the MIT License (http://opensource.org/licenses/MIT)
Distributed as-is; no warranty is given.
******************************************************************************/
//variables for TMP36
float tmp36_voltage = 0; //the voltage measured from the TMP36
float tmp36_degC = 0; //the temperature in Celsius, calculated from the voltage
float tmp36_degF = 0; //the temperature in Fahrenheit, calculated from the voltage
int increment = 100; //step size for averaging
float avg_degC[100]; //array to help average reading
float avg_degF[100]; //array to help average reading
//0 = output degrees °C
//1 = output degrees °F
//any other number = output degrees °C and °F
int output_select = 2; //select output
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud
if (output_select == 0 ) {
Serial.println("TMP36[°C]");
}
else if (output_select == 1) {
Serial.println("TMP36[°F]");
}
else {
Serial.print("TMP36[°C]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
}// end setup
void loop() {
for (int i = 0; i < increment; i++) {
//get TMP36 readings and calculate
tmp36_voltage = analogRead(A0) * 0.004882814; //convert the analog reading, which varies from 0 to 1023, back to a voltage value from 0-5 volts
tmp36_degC = (tmp36_voltage - 0.5) * 100.0; //convert the voltage to a temperature in degrees Celsius
tmp36_degF = tmp36_degC * (9.0 / 5.0) + 32.0; //convert the voltage to a temperature in degrees Fahrenheit
avg_degC[i] = tmp36_degC; //save sample for averaging
avg_degF[i] = tmp36_degF; //save sample for averaging
}
for (int j = 0; j < increment; j++) {
if (j == 0) {
tmp36_degC = 0;
tmp36_degF = 0; //set value to 0 to calculate average
}
tmp36_degC = tmp36_degC + avg_degC[j];
tmp36_degF = tmp36_degF + avg_degF[j]; //add data points
}
tmp36_degC = tmp36_degC / increment;
tmp36_degF = tmp36_degF / increment; //average temp after grabbing 100 data points
if (output_select == 0 ) {
// Print temperature in °C
//Serial.print("Temperature in Celsius: ");
Serial.println(tmp36_degC);//TMP36 temperature
}
else if (output_select == 1) {
// Print temperature in °F
//Serial.print("Temperature in Fahrenheit: ");
Serial.println(tmp36_degF);
}
else {
Serial.print(tmp36_degC); //TMP36 temperature
Serial.print(","); //seperator
Serial.println(tmp36_degF);
}
//delay(50); // Delay added for easier readings
}//end loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment