Introduction: Temperature Monitoring Using MCP9808 and Raspberry Pi
MCP9808 is a highly accurate digital temperature sensor ±0.5°C I2C mini module. They are embodied with user- programmable registers that facilitate temperature sensing applications. The MCP9808 high-accuracy temperature sensor has become an industry standard in terms of form factor and intelligence, providing calibrated, linearised sensor signals in digital, I2C format.
In this tutorial the interfacing of the MCP9808 sensor module with raspberry pi is demonstrated and its programming using python language has also been illustrated. To read the temperature values, we have used raspberry pi with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.
Step 1: Hardware Required:
Step 2: Hardware Hookup:
The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:
The MCP9808 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.
Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!
Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.
These connections are demonstrated in the pictures above.
Step 3: Code for Temperature Measurement:
The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the python. Python is one of the easiest programming languages with easiest syntax. The python code for MCP9808 can be downloaded from our github community that is DCUBE Store Community.
As well as for the ease of the users, we are explaining the code here also:
As the first step of coding you need to download the SMBus library in case of python, because this library supports the functions used in the code. So, to download the library you can visit the following link:
You can copy the working code from here also:
<p>import smbus</p><p>import time</p><p># Get I2C busbus = smbus.SMBus(1)</p><p># MCP9808 address, 0x18(24) </p><p># Select configuration register, 0x01(1)</p><p># 0x0000(00) Continuous conversion mode, Power-up default</p><p>config = [0x00, 0x00]bus.write_i2c_block_data(0x18, 0x01, config)</p><p># MCP9808 address, 0x18(24)</p><p># Select resolution rgister, 0x08(8)</p><p># 0x03(03) Resolution = +0.0625 / C</p><p>bus.write_byte_data(0x18, 0x08, 0x03)</p><p>time.sleep(0.5) </p><p># MCP9808 address, 0x18(24)</p><p># Read data back from 0x05(5), 2 bytes</p><p># Temp MSB, TEMP LSB</p><p>data = bus.read_i2c_block_data(0x18, 0x05, 2)</p><p># Convert the data to 13-bits</p><p>ctemp = ((data & 0x1F) * 256) + data</p><p>if ctemp > 4095 : </p><p>ctemp -= 8192</p><p>ctemp = ctemp * 0.0625</p><p>ftemp = ctemp * 1.8 + 32</p><p># Output data to screen</p><p>print "Temperature in Celsius is : %.2f C" %ctemp</p><p>print "Temperature in Fahrenheit is : %.2f F" %ftemp</p>
The code is executed using the following command:
<pre>$> python MCP9808.pygt; python MCP9808.py
gt; python MCP9808.py
The output of the sensor is shown in the picture above for the reference of the user.
Step 4: Applications:
MCP9808 Digital Temperature Sensor has several industry level applications which incorporate industrial freezers and refrigerators along with various food processors. This sensor can be employed for various personal computers, servers as well as other PC peripherals.