clixxio
Posts: 7
Joined: Fri Mar 29, 2013 1:01 pm

Reading Temperature from MCP9808 Temperature Sensor

Wed Apr 17, 2013 2:38 am

I bought some of these:

- http://uk.farnell.com/microchip/mcp9808 ... dp/2080523

Wiring it all is fine. "i2cdetect -y 1" gives 0x18 and I can read 3 bytes from the device.

However, I can't get a proper temperature reading using the following code. What's wrong
is that it's not accepting my command or I'm not transmitting data in the correct order:

Code: Select all

class tempSensorMCP9808(i2c_device):
    """
    MCP9808 is an I2C Temperature sensor
    """
    def read_temperature(self):
        self.write(0x05)
        s = 0
        b = self.read_nbytes_data(s,3)
        t = b[1] & 0x0f + b[2]
        return t
I'm having trouble getting the same functionality as the datasheet code.

The datasheet gives this code:

Code: Select all

i2c_start(); // send START command
i2c_write (AddressByte & 0xFE); //WRITE Command (see Section 4.1.4 “Address Byte”)
i2c_write(0x05); // Write TA Register Address
//also, make sure bit 0 is cleared ‘0’
i2c_start(); //Repeat START
i2c_write(AddressByte | 0x01); // READ Command (see Section 4.1.4 “Address Byte”)
UpperByte = i2c_read(ACK); // READ 8 bits
LowerByte = i2c_read(NAK); // READ 8 bits
i2c_stop(); // send STOP command
//also, make sure bit 0 is Set ‘1’
//and Send ACK bit
//and Send NAK bit
//Convert the temperature data
//First Check flag bits
if ((UpperByte & 0x80) == 0x80){
//TA 3 TCRIT
}
if ((UpperByte & 0x40) == 0x40){
//TA > TUPPER
}
if ((UpperByte & 0x20) == 0x20){
//TA < TLOWER
}
UpperByte = UpperByte & 0x1F; //Clear flag bits
if ((UpperByte & 0x10) == 0x10){ //TA < 0°C
UpperByte = UpperByte & 0x0F;
//Clear SIGN
Temperature = 256 - (UpperByte x 16 + LowerByte / 16);
}else
//TA
3 0°C
Temperature = (UpperByte x 16 + LowerByte / 16);
//Temperature = Ambient Temperature (°C)

nonik
Posts: 1
Joined: Thu Apr 24, 2014 11:47 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Fri Apr 25, 2014 12:23 am

I just got this sensor as well. After a few different approaches using smbus, this code seems to work for me:

Code: Select all

import smbus

t_reg = 0x05
address = 0x18
bus = smbus.SMBus(1) # change to 0 for older RPi revision
reading = bus.read_i2c_block_data(address, t_reg)
t = (reading[0] << 8) + reading[1]

# calculate temperature (see 5.1.3.1 in datasheet)
temp = t & 0x0FFF
temp /=  16.0
if (t & 0x1000):
    temp -= 256

print 'TempC: %f' % temp
print 'TempF: %f' % (temp * (9.0/5.0) + 32.0)

3.14_in_the_sky
Posts: 7
Joined: Fri May 23, 2014 2:30 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 2:45 am

I'm only getting a UU when I run i2cdetect -y 1

(I have the proper raspberry pi for the 1)

Could you guys post your pinouts, and anything else you did to get this working such as what you unblacklisted etc..

Thanks!

User avatar
joan
Posts: 13582
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 3:02 am

3.14_in_the_sky wrote:I'm only getting a UU when I run i2cdetect -y 1

(I have the proper raspberry pi for the 1)

Could you guys post your pinouts, and anything else you did to get this working such as what you unblacklisted etc..

Thanks!
At what address?

You have not connected your device correctly.

SDA, SCL, Ground, Power.

3.14_in_the_sky
Posts: 7
Joined: Fri May 23, 2014 2:30 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 3:22 am

Thanks for the reply. I just verified I'm using SCL, SDA, grnd and 5v. No change and I'm still getting the UU. Any other suggestions?

User avatar
joan
Posts: 13582
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 3:38 am

3.14_in_the_sky wrote:Thanks for the reply. I just verified I'm using SCL, SDA, grnd and 5v. No change and I'm still getting the UU. Any other suggestions?
Exactly the same as before.

At what address?

3.14_in_the_sky
Posts: 7
Joined: Fri May 23, 2014 2:30 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 5:02 am

Sorry I missed that in your post. The UU appears on the 30 line, and column b.
Thanks!

User avatar
DougieLawson
Posts: 34167
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 8:26 am

It's an I2C device with three address select lines. Tie A0/A1/A2 to GND on your breadboard (that will be wired to PIN #6 on your RPi). Wire Vdd to Pin #1 (3V3), wire GND to Pin #6 (GND), wire SDA to Pin #3 (SDA) and wire SCL to Pin #5 (SCL).
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Massi
Posts: 1679
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Reading Temperature from MCP9808 Temperature Sensor

Wed Jul 23, 2014 11:40 am

3.14_in_the_sky wrote:Sorry I missed that in your post. The UU appears on the 30 line, and column b.
Thanks!
i think that the question was at what address you were expecting to find the device, not at what address there is the UU :)
UU is some reservation of something, don't care about it, the point is that a IIC device must be set to an address (where it will be reachable on the iic bus), and from your output it seems that your device has not an address.
I have to admit that i'm a little bit scared of you powering it at 5V, but i've not checked the datasheet..
So, for sure if you bought the bulk sensor, you have to set the address (i think some pins will do that..) soldering them to ground or to Vcc
Then, connect it back to the raspi and search for it on the bus :)

Please check correct voltage..

3.14_in_the_sky
Posts: 7
Joined: Fri May 23, 2014 2:30 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Thu Jul 24, 2014 8:45 pm

Thank you everyone for your help!
I'm expecting the address to be 0x18 & I've confirmed that it can use 3.3V or 5v:
https://learn.adafruit.com/adafruit-mcp ... e/overview
Here is what I used to set up my i2c on the Pi:
http://www.instructables.com/id/Raspber ... /?ALLSTEPS

I have two of these sensors. I tried them both on 5v and 3.3v. I also wired them according to the previous post, trying to tie in A0..An to ground and without them connected. My next step will be to try this on my arduino to verify the sensor itself is working.

Thanks again!

User avatar
joan
Posts: 13582
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading Temperature from MCP9808 Temperature Sensor

Thu Jul 24, 2014 8:53 pm

It seems like you should tie A0/A1/A2 to ground say.

Have you soldered the header pins on? Loose connections are a common I2C problem.

3.14_in_the_sky
Posts: 7
Joined: Fri May 23, 2014 2:30 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Sat Jul 26, 2014 3:43 am

Soldering did the trick! I had no idea i2c was so finicky. Thank you very much for clearing that up for me!

3.14_in_the_sky
Posts: 7
Joined: Fri May 23, 2014 2:30 pm

Re: Reading Temperature from MCP9808 Temperature Sensor

Mon Sep 08, 2014 10:34 pm

Hey everyone... I got it working on my pi! Works great. Here's my quick guide!

http://www.ericescobar.com/wordpress/ra ... re-sensor/

Return to “Automation, sensing and robotics”