Skip to content

Instantly share code, notes, and snippets.

@bboyho
Created November 12, 2019 01:27
Show Gist options
  • Save bboyho/eb5308aa8aa8575c89ee103140ee8ae0 to your computer and use it in GitHub Desktop.
Save bboyho/eb5308aa8aa8575c89ee103140ee8ae0 to your computer and use it in GitHub Desktop.
/******************************************************************************
TMP117vsTMP36.ino
Modified Example 1 to Compare the Precision of TMP117 against TMP36
Madison Chodikov @ SparkFun Electronics
Modified by Ho Yun "Bobby" Chan
May 29 2019
~
This sketch configures the TMP117 temperature sensor and prints the
temperature in degrees celsius and fahrenheit. For comparison, the
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.
Resources:
Wire.h (included with Arduino IDE)
SparkFunTMP117.h (included in the src folder)
Development environment specifics:
Arduino 1.8.9+
Hardware Version 1.0.0
This code is beerware; if you see me (or any other SparkFun employee) at
the local, and you've found our code helpful, please buy us a round!
Distributed as-is; no warranty is given.
******************************************************************************/
/*
NOTE: For the most accurate readings:
- 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
// The default address of the device is 0x48 = (GND)
TMP117 sensor; // Initalize 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 = 0; //select output
boolean dataready_flaggy = 0;
void setup()
{
Wire.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");
if (output_select == 0 ) {
Serial.print("TMP117[°C]");
Serial.print(",");
Serial.println("TMP36[°C]");
}
else if (output_select == 1) {
Serial.print("TMP117[°F]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
else {
Serial.print("TMP117[°C]");
Serial.print(",");
Serial.print("TMP36[°C]");
Serial.print(",");
Serial.print("TMP117[°F]");
Serial.print(",");
Serial.println("TMP36[°F]");
}
}
else
{
Serial.println("Device failed to setup- Freezing code.");
while (1); // Runs forever
}
sensor.setContinuousConversionMode();
Serial.println("Continuous Mode");
//sensor.setOneShotMode();//set to oneshot mode
//Serial.println("One-Shot Mode");
delay(3000);
Serial.print("Initial Data Ready = ");
dataready_flaggy = sensor.dataReady();
Serial.println(dataready_flaggy);
}
void loop()
{
// Data Ready is a flag for the conversion modes - in continous conversion the dataReady flag should always be high
if (dataready_flaggy == 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 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(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.println(tmp36_degF);
}
else {
Serial.print(tempC);//TMP117 temperature
Serial.print(","); //seperator
Serial.print(tmp36_degC);//TMP36 temperature
Serial.print(","); //seperator
Serial.print(tempF);
Serial.print(","); //seperator
Serial.println(tmp36_degF);
}
}
Serial.print("Data Ready = ");
dataready_flaggy = sensor.dataReady();
Serial.println(dataready_flaggy);
delay(500); // Delay added for easier readings
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment