Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active March 27, 2020 08:23
Show Gist options
  • Save bboyho/64b7eaf64fa4557216bb9797d5eb0dad to your computer and use it in GitHub Desktop.
Save bboyho/64b7eaf64fa4557216bb9797d5eb0dad to your computer and use it in GitHub Desktop.
/******************************************************************************
TMP102vsTMP36.ino
Written by: Ho Yun "Bobby" Chan
@ SparkFun Electronics
Date: Nov 4, 2019
Description: This sketch configures temperature sensors and prints the
temperature in degrees celsius and fahrenheit. For comparison, the
TMP102 and TMP36 temperature sensor is also printed to compare the output
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.
Resources/Libraries:
Wire.h (included with Arduino IDE)
SparkFunTMP102.h (included in the src folder) https://github.com/sparkfun/SparkFun_TMP102_Arduino_Library
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.
******************************************************************************/
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include "SparkFunTMP102.h" // Used to send and recieve specific information from our sensor
// The default address of the device is 0x48 = (GND) like the TMP117
// but it's already being used so we use a different address
TMP102 sensor0; //initialize sensor
//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()
{
Wire.begin();
sensor0.begin(0x49);
Serial.begin(115200); // Start serial communication at 115200 baud
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
sensor0.wakeup();//turn on TMP102
if (output_select == 0 ) {
Serial.println("TMP102[°C]");
Serial.print(",");
Serial.println("TMP36[°C]");
}
else if (output_select == 1) {
Serial.print("TMP102[°F]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
else {
Serial.println("TMP102[°C]");
Serial.print(",");
Serial.print("TMP36[°C]");
Serial.print(",");
Serial.print("TMP102[°F]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
}
void loop()
{
//get TMP102 sensor readings
float tmp102_tempC = sensor0.readTempC();
float tmp102_tempF = sensor0.readTempF();
//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(tmp102_tempC); //TMP102 temperature
Serial.print(","); //seperator
Serial.println(tmp36_degC);//TMP36 temperature
}
else if (output_select == 1) {
// Print temperature in °F
//Serial.print("Temperature in Fahrenheit: ");
Serial.print(tmp102_tempF); //TMP102 temperature
Serial.print(","); //seperator
Serial.println(tmp36_degF);
}
else {
Serial.print(tmp102_tempC);//TMP102 temperature
Serial.print(","); //seperator
Serial.print(tmp36_degC);//TMP36 temperature
Serial.print(","); //seperator
Serial.print(tmp102_tempF);
Serial.print(","); //seperator
Serial.println(tmp36_degF);
}
delay(50); // Delay added for easier readings
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment