Home Code TMP102 sensor and Raspberry Pi python example

TMP102 sensor and Raspberry Pi python example

by shedboy71

In this article we look at the TMP102 digital sensor and we will connect it up to a Raspberry Pi

The TMP102 device is a digital temperature sensor ideal for NTC/PTC thermistor replacement where high accuracy is required. The device offers an accuracy of ±0.5°C without requiring calibration or external component signal conditioning. Device temperature sensors are highly linear and do not require complex calculations or lookup tables to derive the temperature. The on-chip 12-bit ADC offers resolutions down to 0.0625°C.

The TMP102 device features SMBus™, two-wire and I2C interface compatibility, and allows up to four devices on one bus. The device also features an SMBus alert function. The device is specified to operate over supply voltages from 1.4 to 3.6 V with the maximum quiescent current of 10 µA over the full operating range.

The TMP102 device is ideal for extended temperature measurement in a variety of communication, computer, consumer, environmental, industrial, and instrumentation applications. The device is specified for operation over a temperature range of –40°C to 125°C.

 

Layout

Raspberry Pi and TMP102

Raspberry Pi and TMP102

 

Parts List

Name Link
Raspberry Pi Raspberry Pi 3 Model B With WiFi & Bluetooth
TMP102 TMP102 Digital Temperature Sensor Breakout Board Module 12-bit 1.4V To 3.6VDC Sensor Module Temperature Module
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Code

Download this and rename as .py file extension – tmp102

[codesyntax lang=”python”]

import time
import smbus

i2c_ch = 1

# TMP102 address on the I2C bus
i2c_address = 0x48

# Register addresses
reg_temp = 0x00
reg_config = 0x01

# Calculate the 2’s complement of a number
def twos_comp(val, bits):
if (val & (1 << (bits – 1))) != 0:
val = val – (1 << bits)
return val

# Read temperature registers and calculate Celsius
def read_temp():

# Read temperature registers
val = bus.read_i2c_block_data(i2c_address, reg_temp, 2)
temp_c = (val[0] << 4) | (val[1] >> 5)

# Convert to 2s complement (temperatures can be negative)
temp_c = twos_comp(temp_c, 12)

# Convert registers value to temperature (C)
temp_c = temp_c * 0.0625

return temp_c

# Initialize I2C (SMBus)
bus = smbus.SMBus(i2c_ch)

# Read the CONFIG register (2 bytes)
val = bus.read_i2c_block_data(i2c_address, reg_config, 2)
print(“Old CONFIG:”, val)

# Set to 4 Hz sampling (CR1, CR0 = 0b10)
val[1] = val[1] & 0b00111111
val[1] = val[1] | (0b10 << 6)

# Write 4 Hz sampling back to CONFIG
bus.write_i2c_block_data(i2c_address, reg_config, val)

# Read CONFIG to verify that we changed it
val = bus.read_i2c_block_data(i2c_address, reg_config, 2)
print(“New CONFIG:”, val)

# Print out temperature every second
while True:
temperature = read_temp()
print(round(temperature, 2), “C”)
time.sleep(1)

[/codesyntax]

Output

Run the above code by typing in sudo python tmp102.py. You should see something like this

tmp102 output

tmp102 output02

Link

http://www.ti.com/lit/gpn/tmp102

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More