Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active January 24, 2020 19:51
Show Gist options
  • Select an option

  • Save bboyho/c30b70fc308f41b92a6f1b07e5a54838 to your computer and use it in GitHub Desktop.

Select an option

Save bboyho/c30b70fc308f41b92a6f1b07e5a54838 to your computer and use it in GitHub Desktop.
/******************************************************************************
TMP36.ino
Written by Ho Yun "Bobby" Chan
@ SparkFun Electronics
Date: Nov 4, 2019
https://gist.github.com/bboyho/c30b70fc308f41b92a6f1b07e5a54838
Description: This sketch configures temperature sensor and prints the
temperature in degrees celsius and fahrenheit. 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
//0 = output degrees °C
//1 = output degrees °F
//any other number = output degrees °C and °F
int output_select = 1; //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() {
//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
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(5); // 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