Skip to content

Instantly share code, notes, and snippets.

@bboyho
Last active January 24, 2020 19:04
Show Gist options
  • Save bboyho/39403f9bb4e8444e9398a5f31a411991 to your computer and use it in GitHub Desktop.
Save bboyho/39403f9bb4e8444e9398a5f31a411991 to your computer and use it in GitHub Desktop.
/******************************************************************************
TMP117vsTMP102vsTMP36.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
TMP117, 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)
SparkFunTMP117.h (included in the src folder) http://librarymanager/All#SparkFun_TMP117
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.
******************************************************************************/
/*
NOTE: For the most accurate readings using the TMP117:
- Avoid heavy bypass traffic on the I2C bus
- Use the highest available communication speeds
- Use the minimal supply voltage acceptable for the system
- Place device horizontally and out of any airflow when storing
For more information on reaching the most accurate readings from the sensor,
reference the "Precise Temperature Measurements with TMP116" datasheet that is
linked on Page 35 of the TMP117's datasheet
*/
#include <Wire.h> // Used to establish serial communication on the I2C bus
#include <SparkFun_TMP117.h> // Used to send and recieve specific information from our sensor
#include "SparkFunTMP102.h" // Used to send and recieve specific information from our sensor
// The default address of the device is 0x48 = (GND)
TMP117 sensor; // Initalize sensor
// The default address of the device is 0x48 = (GND) as well
// but it's already being used so we use a different address
TMP102 sensor0(0x49); //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();
Serial.begin(115200); // Start serial communication at 115200 baud
Wire.setClock(400000); // Set clock speed to be the fastest for better communication (fast mode)
//Serial.println("TMP117 Example 1: Basic Readings");
if (sensor.begin() == true ) // Function to check if the sensor will correctly self-identify with the proper Device ID/Address
{
//Serial.println("Begin");
sensor0.wakeup();//turn on TMP102
if (output_select == 0 ) {
Serial.print("TMP117[°C]");
Serial.print(",");
Serial.println("TMP102[°C]");
Serial.print(",");
Serial.println("TMP36[°C]");
}
else if (output_select == 1) {
Serial.print("TMP117[°F]");
Serial.print(",");
Serial.print("TMP102[°F]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
else {
Serial.print("TMP117[°C]");
Serial.print(",");
Serial.println("TMP102[°C]");
Serial.print(",");
Serial.print("TMP36[°C]");
Serial.print(",");
Serial.print("TMP117[°F]");
Serial.print(",");
Serial.print("TMP102[°F]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
}
else
{
Serial.println("Device failed to setup- Freezing code.");
while (1); // Runs forever
}
}
void loop()
{
// Data Ready is a flag for the conversion modes - in continous conversion the dataReady flag should always be high
if (sensor.dataReady() == true) // Function to make sure that there is data ready to be printed, only prints temperature values when data is ready
{
//get TMP117 sensor readings
float tempC = sensor.readTempC();
float tempF = sensor.readTempF();
//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.print(tempC);//TMP117 temperature
Serial.print(","); //seperator
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(tempF);
Serial.print(","); //seperator
Serial.print(tmp102_tempF); //TMP102 temperature
Serial.print(","); //seperator
Serial.println(tmp36_degF);
}
else {
Serial.print(tempC);//TMP117 temperature
Serial.print(","); //seperator
Serial.print(tmp102_tempF); //TMP102 temperature
Serial.print(","); //seperator
Serial.print(tmp36_degC);//TMP36 temperature
Serial.print(","); //seperator
Serial.print(tempF);
Serial.print(","); //seperator
Serial.print(tmp102_tempF); //TMP102 temperature
Serial.print(","); //seperator
Serial.println(tmp36_degF);
}
//delay(5); // Delay added for easier readings
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment