Here's how to connect a MCP9804.

You can use it like this:
root@raspberrypi:~# modprobe i2c-dev
root@raspberrypi:~# modprobe i2c-bcm2708
root@raspberrypi:~# i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 1f
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
root@raspberrypi:~# i2cget -y 0 0x1f 0x5 w
0x67c1
Converting 0x67c1 to a temperature is a little convoluted. The MSB is 0xc1 and the LSB is 0x67
The first 4 bits of the MSB are dropped and which leaves the temperature in 16ths of a degree
(0xc1&0xf)*16+0x67/16.0 = 22.4375 degrees
Python Example
In addition to loading the i2c modules above, you'll need to install the python-smbus package. You can reduce self-heating by shutting down the MCP9804 between readings.
#!/usr/bin/env python
import time
from smbus import SMBus
class MCP9804(object):
def __init__(self, bus, addr):
self.bus = bus
self.addr = addr
def wakeup(self):
self.bus.write_word_data(self.addr, 1, 0x0000)
def shutdown(self):
self.bus.write_word_data(self.addr, 1, 0x0001)
def get_temperature(self, shutdown=False):
if shutdown:
self.wakeup()
time.sleep(0.26) # Wait for conversion
msb, lsb = self.bus.read_i2c_block_data(self.addr, 5, 2)
if shutdown:
self.shutdown()
tcrit = msb>>7&1
tupper = msb>>6&1
tlower = msb>>5&1
temperature = (msb&0xf)*16+lsb/16.0
if msb>>4&1:
temperature = 256 - temperature
return temperature
def main():
sensor = MCP9804(SMBus(0), 0x1f)
while True:
print sensor.get_temperature()
time.sleep(1)
if __name__ == "__main__":
main()