TMP75

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
Post Reply
User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

TMP75

Post by roland_vs » Sun Dec 16, 2018 4:43 pm

Dear all,

Did anyone find or has written (and willing to publish this under MIT) a driver using a TMP75 I2C temperature sensor?

Beste regards,

Roland

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: TMP75

Post by mcauser » Mon Dec 17, 2018 2:15 am

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)

User avatar
mcauser
Posts: 507
Joined: Mon Jun 15, 2015 8:03 am

Re: TMP75

Post by mcauser » Mon Dec 17, 2018 2:16 am

Should also work for other TMPx75 sensors, TMP275, TMP175 etc.

User avatar
roland_vs
Posts: 89
Joined: Tue Dec 08, 2015 8:28 pm
Location: Netherlands
Contact:

Re: TMP75

Post by roland_vs » Thu Dec 20, 2018 11:07 pm

TNX's

Happy XMAS

R

Post Reply