Need help getting started with I2C communication and temperature decoding.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Need help getting started with I2C communication and temperature decoding.
This example also applies to TMP102.
#include <Wire.h> #define TMP112Addr 0x49 /* Address Pin Options: * GND 1001000 0x48 * V+ 1001001 0x49 * SDA 1001010 0x4A * SCL 1001011 0x4B */ void setup() { Wire.begin(); Serial.begin(9600); } void loop() { Wire.beginTransmission(TMP112Addr); Wire.write(0x0); /* Write Pointer=0 to select Temperature Register */ Wire.endTransmission(); Wire.requestFrom(TMP112Addr, 2); /* Request 2 bytes */ int16_t ret = (Wire.read() << 8) | Wire.read(); float tempC = (ret >> 4) * 0.0625; Serial.println(tempC); delay(500); }
This will print temperature in degrees celsius to the Serial Monitor. It's important to note that storing/casting the 16 bit result in a signed 16 bit data type will correctly convert it to a negative number as appropriate. Here is the Serial Plotter showing a shot of freeze spray applied to the sensor, with negative temperatures correctly decoded: