Article count:1546 Read by:1782402

Account Entry

Here comes the strategy: Use Raspberry Pi to read the data of temperature sensor MCP9808

Latest update time:2023-12-27
    Reads:
Q
A
&

Q: Reading temperature data from MCP9808 using Raspberry Pi

The MCP9808 is a temperature sensor that uses the I2C communication standard to transmit continuously polled temperature. It supports alarm function and standby mode to save power when needed. In this instruction document, we will execute a few python commands to get the temperature of the sensor.




I2C on MCP9808

Microchip uses 0x18 as the main address on this board, with the help of 3 pins on the chip to change the address in case of I2C address conflict. When any register in the sensor is called, the board should write or get a two-byte response (MSB first, LSB last). I'm using Adafruit 's breakout board [ 1528-1032-ND ], which already contains some forward bias resistors.

Registers to note:

0x01 : 0000000X 00011111 : The X here is the spare bit in the configuration, the default is 0, this is continuous polling mode. The temperature register is constantly updated, but there are power consumption issues. Setting this bit to 1 will stop polling and save power.
0x05 : AAASMMMM LLLLLLLL : This register contains 12-bit floating point temperature, where "A" is the alarm information, "S" is the sign bit, "M" and "L" represent MSB and LSB respectively. The final output will appear as MMMMLLLL.LLLL.
Note that in this description I have not set the sign bit and alarm bit, but you will need to set them if you wish to measure sub-zero temperatures.

Implementation on Raspberry PI:

The benefit of using a Raspberry Pi with an I2C interface is the interactivity of the Python terminal. I'm using a Raspberry Pi 4 with I2C enabled on the Raspbian distribution . Make sure your GPIO supports 3V3, GND, SDA and SCL supports the correct pins on the MCP9808. Open a terminal in Raspbian and execute the following three commands:
python
from smbus import SMBus
bus = SMBus(1)
This command will create a bus object to be connected, which will in turn collect the I2C data we requested. Next, ask the chip for its temperature data:
temp_binary = format(bus.read_word_data(0x18, 0x05),'016b')
The will retrieve the data from the sensor, we use a closed function to read the information as a binary number, this is to make it simple to extract these two bytes. Python usually likes to use int or float, but this gets in the way because the value is returned in multiple bytes. We will index it in order, with the MSB in the second half of the array. bus.read_byte_data(Address, Register) format() [0:8][8:16]

Calculated output:

We can use the following function to get the output value correctly
def word_To_LSB_MSB(word):
return word[0:8], word[12:16] // note that word indices [8,9,10,11] are not used in this example.
Please look at the image below to understand our required output.
Finally, just combine the two together and add some floating point operations.
LSB, MSB = word_To_LSB_MSB(temp_binary)
float(int(MSB + LSB,2)) / 16
Divide by 16 to convert the result to MMMMLLLL.LLLL and obtain decimal precision. Note that the returned results should be in Celsius. In the case shown above, the temperature we calculated from the binary float 00010111.0101 is 23.3125 degrees Celsius.

Enter power saving mode when finished:

If you wish to shut down the device when finished, simply set the standby bit to power save mode:
bus.write_byte_data(0x18,0x01,0b00000001)
Since only the MSB in the MCP9809 configuration register needs to be changed, we write one byte, but we shorten the characters so the LSB in the register is not changed.
To return the MCP9808 to a fully operational functional state, the sensor must be power cycled or the register's zero refresh mode must be used.
Microchip's data sheet is still very useful in this sensor application, so be sure to check it regularly if you are going to use the MCP9808. Note that some important features of this sensor are not covered in this article, such as interrupts, sign bit for negative temperatures, and standby operation.

Items used:

Raspberry Pi: 1690-RASPBERRYPI4B/4GB-ND Female to Male Breadboard Cable: 1568-1511-ND Adafruit Breakout Board for MCP9808: 1528-1032-ND



Finally, if you like this article, please share it with more friends ! Remember to give it a like !


Tips Click the menu Design Support: Engineer Tips to get more engineer tips



Continuous learning of secret skills and endless enjoyment of exclusive benefits

Just waiting for you to join!





Earn points and redeem gifts

Go to "Member Benefits" to view your benefits now!
If you have any questions, please contact us
DigiKey ’s customer service team

China (RMB) Customer Service

4 00-920-1199
Service Support > Contact Customer Service > WeChat Customer Service
service.sh@digikey.com
QQ online real-time consultation: 4009201199



China (USD)/Hong Kong Customer Service


400-882-4440

852-3104-0500
c hina.support@digikey.com

Click "Read the original text" below to see more

Let me know you 're watching yo


Latest articles about

 
EEWorld WeChat Subscription

 
EEWorld WeChat Service Number

 
AutoDevelopers

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building,Block B, 18 Zhongguancun Street, Haidian District,Beijing, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号