Intro: Temperature Measurement Using TMP112 and Raspberry Pi
TMP112 High-Accuracy, Low-Power, Digital Temperature Sensor I2C MINI module. The TMP112 is ideal for extended temperature measurement. This device offers an accuracy of ±0.5°C without requiring calibration or external component signal conditioning.
In this tutorial the interfacing of the TMP112 sensor module with raspberry pi is demonstrated and its programming using Java language has also been illustrated. To read the temperature values, we have used raspberry pi with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.
Step 1: Hardware Required:
Step 2: Hardware Hookup:
The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:
The TMP112 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.
Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!
Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.
These connections are demonstrated in the pictures above.
Step 3: Java Code for Temperature Measurement:
The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the Java. The java code for TMP112 can be downloaded from our github community that is Control Everything Community.
As well as for the ease of the users, we are explaining the code here also:
As the first step of coding you need to download the pi4j library in case of java, because this library supports the functions used in the code. So, to download the library you can visit the following link:
You can copy the working java code for this sensor from here also:
public class TMP112
public static void main(String args) throws Exception
// Create I2C bus
I2CBus bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, TMP112 I2C address is 0x48(72)
I2CDevice device = bus.getDevice(0x48);
byte config = new byte;
// Continous Conversion mode, 12-Bit Resolution, Fault Queue is 1
config = (byte)0x60;
// Polarity low, Thermostat in Comparator mode, Disables Shutdown mode
config = (byte)0xA0;
// Write config to register 0x01(1)
device.write(0x01, config, 0, 2);
// Read 2 Bytes of data from address 0x00(0), msb first
byte data = new byte;
device.read(0x00, data, 0, 2);
// Convert data
int temp = (((data & 0xFF) * 256) + (data & 0xFF))/16;
if(temp > 2047)
temp -= 4096;
double cTemp = temp * 0.0625;
double fTemp = cTemp * 1.8 + 32;
// Output to screen
System.out.printf("Temperature in Celsius is : %.2f C %n", cTemp);
System.out.printf("Temperature in Fahrenheit is : %.2f F %n", fTemp);
The library which facilitates i2c communication between the sensor and the board is pi4j, its various packages I2CBus, I2CDevice and I2CFactory help to establish the connection.
import com.pi4j.io.i2c.I2CDevice; import com.pi4j.io.i2c.I2CFactory; import java.io.IOException;
write() and read() functions are used to write some particular commands to the sensor to make it work in a particular mode and read the sensor output respectively.
The output of the sensor is also shown in the picture above.
Step 4: Applications:
Various applications incorporating TMP112 low power, high accuracy digital temperature sensor include Power-Supply Temperature Monitoring, Computer Peripheral Thermal Protection, Battery Management as well as office machines.