Skip to content

Instantly share code, notes, and snippets.

@dwaq
Created December 19, 2013 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwaq/8033332 to your computer and use it in GitHub Desktop.
Save dwaq/8033332 to your computer and use it in GitHub Desktop.
reading temperature from SA56004ED temperature sensor http://tinkeringetc.blogspot.com/
// reading temperature from SA56004ED temperature sensor
// http://tinkeringetc.blogspot.com/
#include <Wire.h>
#define SA56004ED 0x4C // SA56004ED temperature sensor device address
#define LTHB 0x00 // local temperature high byte register address
#define LTLB 0x22 // local temperature high byte register address
float lthb, ltlb; // local temperature low and high bytes
float tempC, tempF; // temperatures in Celcius and Farenheit
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
// get local temperature high byte
Wire.beginTransmission(SA56004ED); //Start talking to SA56004ED
Wire.write(LTHB); //Ask for Register for local temperature high byte
Wire.endTransmission(); //Complete Transmission
Wire.requestFrom(SA56004ED, 1); // request 1 byte from SA56004ED
while(Wire.available()) // slave may send less than requested
{
lthb = Wire.read(); // receive the local temperature high byte
}
// get local temperature low byte
Wire.beginTransmission(SA56004ED); //Start talking to SA56004ED
Wire.write(LTLB); //Ask for Register zero
Wire.endTransmission(); //Complete Transmission
Wire.requestFrom(SA56004ED, 1); // request 1 byte from slave device #0
while(Wire.available()) // slave may send less than requested
{
ltlb = Wire.read(); // receive the local temperature low byte
}
// ltlb = xxx0 0000
// shift 8 places to the right to make it a decimal
// ltlb = 0.xxx
// ltlb / (2^8) == ltlb >> 8 [but you can't shift bits in a float]
ltlb = ltlb / pow(2, 8);
tempC = lthb + ltlb;
tempF = ((tempC*9)/5)+32; // convert Celcius to Farenheit
Serial.println(tempF);
Serial.println();
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment