I don't have one of these sensors, however, the interface seems simple enough.
http://www.ti.com/lit/ds/symlink/tmp75.pdf
Configure the sensor to use a specific resolution, 9,10,11 or 12 bit.
Read 12 bits from the temperature register.
Convert data from twos compliment to temperature in Celsius.
Scan the I2C bus for the device.
Code: Select all
from machine import I2C, Pin
i2c = I2C(scl=Pin(5), sda=Pin(4), freq= 400000)
i2c.scan()
The device by default can be found at 0x48, so you should see [72].
You can change the address by pulling A0,A1,A2 to GND.
Read the config register.
Code: Select all
config = i2c.readfrom_mem(0x48, 0x01, 1)
bit0 = shutdown mode (power savings)
bit 1 = thermostat mode (0=comparitor, 1=interrupt)
bit 2 = alert polarity
bits 3,4 = fault queue (number of failed measurements)
bits 5,6 = resolution (00=9bit, 01=10bit, 10=11bit, 11=12bit)
bit 7 = one shot (performs one measurement then shuts down)
Set the resolution.
Code: Select all
resolution = 12 # allowed values are: 9,10,11,12
config &= ~0x60 # clear bits 5,6
config |= (resolution - 9) << 5 # set bits 5,6
i2c.writeto_mem(0x48, 0x01, config)
Read the temperature register.
Code: Select all
data = i2c.readfrom_mem(0x48, 0, 2) # read 2 bytes from device 0x48 starting at address 0
# each bit represents 0.0625 degrees Celsius.
# negative numbers are represented in binary 2s complement format.
def twos_comp(val, width):
if val >= 2**width:
raise ValueError("Value: {} out of range of {} bit value".format(val, width))
else:
return val - int((val << 1) & 2**width)
temp = data[0] << 4 | data[1] >> 4 # ignore the 4 least significant bits of the 2nd byte
temp = twos_comp(temp, 12) * 0.0625 # convert to Celsius
print(temp)