Advertisement
NickAllain

Code for TMP102 Tempurature Sensor

Mar 12th, 2013
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include <Wire.h>
  2. int tmp102Address = 0x48;
  3.  
  4. void setup(){
  5.  Serial.begin(9600); //Set our baud rate to 9600 for our serial connection
  6.  Wire.begin(); // Initiate the Wire library and join the I2C bus
  7. }
  8.  
  9. void loop(){
  10.  
  11.  float celsius = getTemperature(); // Get the temperature in C and save it as a float called celsius.
  12.  Serial.print("Celsius: "); // Print our description of the temp
  13.  Serial.println(celsius); // Print our temp in C
  14.  
  15.  
  16.  float fahrenheit = (1.8 * celsius) + 32;  // Get the temperature in C and save it as a float called fahrenheit after converting it from celsius.
  17.  Serial.print("Fahrenheit: "); // Print our description of the temp.
  18.  Serial.println(fahrenheit); // Print the temp in F
  19.  
  20.  delay(200); //just here to slow down the output. You can remove this
  21. }
  22.  
  23. // Let’s take a look at our getTempurature function. Without it, nothing would work.
  24. float getTemperature(){
  25.  Wire.requestFrom(tmp102Address,2); // We use our wire library’s get two readings from our TMP102. We pass it the address and the quantity.
  26.  
  27.  
  28. // You may have noticed that we didn’t do anything with the request. That’s because we have to read back we requested. We do that sequentially.
  29.  byte MSB = Wire.read(); // The first byte we get, the Most Significant, get’s stored in MSB.
  30.  byte LSB = Wire.read(); // The second byte we get, the Least Significant, get’s stored in LSB.
  31.  
  32.  
  33.  int TemperatureSum = ((MSB << 8) | LSB) >> 4;
  34.  
  35.  float celsius = TemperatureSum*0.0625; // Convert our temp to celsius.
  36.  return celsius; // Return our result to be printed.
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement