Raspberry Pi MCP9803 Temperature Sensor Python Tutorial

by Dcube Tech Ventures in Circuits > Electronics

21 Views, 0 Favorites, 0 Comments

Raspberry Pi MCP9803 Temperature Sensor Python Tutorial

Raspberry Pi MCP9803 Temperature Sensor Python Tutorial

MCP9803 is a 2-wire high accuracy temperature sensor. They are embodied with user- programmable registers that facilitate temperature sensing applications. This sensor is suited for highly sophisticated multi-zone temperature monitoring system. Here is the demonstration with a python code using Raspberry Pi.

What You Need..!!

MCP9803_I2CS.png
pi.jpg

1. Raspberry Pi

2. MCP9803

3. I²C Cable

4. I²C Shield for Raspberry Pi

5. Ethernet Cable

Connections

MCP9803_I2CS.png
pi.jpg
sensor.jpg
complete_connection.jpg

Take a I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.

Then connect the one end of I2C cable to MCP9803 sensor and the other end to the I2C shield.

Also connect the Ethernet cable to the pi or you can use a WiFi module. Connections are shown in the picture above.

Code

complete_connection.jpg

The python code for MCP9803 can be downloaded from our github repository- Dcube Store

Here is the link for the same :

https://github.com/DcubeTechVentures/MCP9803/blob/master/Python/MCP9803.py

We have used SMBus library for python code, the steps to install SMBus on raspberry pi is described here:

https://pypi.python.org/pypi/smbus-cffi/0.5.1

You can also copy the code from here, it is given as follows:

# Distributed with a free-will license.

# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.

# MCP9803

# This code is designed to work with the MCP9803_I2CS I2C Mini Module .

import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# MCP9803 address, 0x48(72)

# Select configuration register, 0x01(1)# 0x60(96) Continuous conversion mode, Power-up, Comparator mode

bus.write_byte_data(0x48, 0x01, 0x60)

time.sleep(0.5)

# MCP9803 address, 0x48(72)# Read data back from 0x00(00), 2 bytes

# cTemp MSB, cTemp LSB

data = bus.read_i2c_block_data(0x48, 0x00, 2)

# Convert the data to 12-bits

ctemp = (data[0] * 256 + data[1]) / 16.0

if ctemp > 2047 :

ctemp -= 4096

ctemp = ctemp * 0.0625

ftemp = ctemp * 1.8 + 32

# Output data to screen

print "Temperature in Celsius : %.2f C" %ctemp

print "Temperature in Fahrenheit : %.2f F" %ftemp

Applications..:

MCP9803 can be employed in a wide arena of devices which include personal computer and peripherals, hard disk drives, various entertainment systems, office systems and data communication systems. This sensor can be incorporated in various sophisticated systems.