Raspberry Pi MCP9805 Temperature Sensor Python Tutorial

by Dcube Tech Ventures in Circuits > Electronics

45 Views, 0 Favorites, 0 Comments

Raspberry Pi MCP9805 Temperature Sensor Python Tutorial

Raspberry Pi MCP9805 Temperature Sensor Python Tutorial

MCP9805 is a memory module digital temperature sensor. It is incorporated with user programmable registers that provide flexibility for temperature sensing applications. This sensor is designed to be incorporated in mobile platform memory module temperature sensor. Here is the demonstration with a java code using Raspberry Pi.

What You Need..!!

MCP9805_I2CS_A_1.png
pi.jpg

Connections

MCP9805_I2CS_A_1.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 MCP9805 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 MCP9805 can be downloaded from our GitHub repository- Dcube Store.

Here is the link for the same :

https://github.com/DcubeTechVentures/MCP9805

The datasheet of MCP9805 can be found here:

http://ww1.microchip.com/downloads/en/DeviceDoc/21...

We have used SMBus library for python code, the steps to install SMBus on the 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.

# MCP9805

# This code is designed to work with the MCP9805_I2CS I2C Mini Module available in Dcube Store.


import smbus

import time

# Get I2C bus

bus = smbus.SMBus(1)

# MCP9805 address, 0x18(24)

# Select configuration register, 0x01(1)

# 0x0000(00) Continuous conversion mode, Power-up

config = [0x00, 0x00]

bus.write_i2c_block_data(0x18, 0x01, config)

# MCP9805 address, 0x18(24)

# Select resolution rgister, 0x08(8)

# 0x03(03) Resolution = +0.0625/C

bus.write_byte_data(0x18, 0x08, 0x03)

time.sleep(0.5)

# MCP9805 address, 0x18(24)

# Read data back from 0x05(5), 2 bytes

# cTemp MSB, cTemp LSB

data = bus.read_i2c_block_data(0x18, 0x05, 2)

# Convert the data to 13-bits

cTemp = ((data[0] & 0x1F) * 256) + data[1]

if cTemp > 4095 :

cTemp -= 8192

cTemp = cTemp * 0.0625

fTemp = cTemp * 1.8 + 32

# Output data to screen

print "Temperature in Celsius is : %.2f C" %cTemp

print "Temperature in Fahrenheit is : %.2f F" %fTemp

Applications..:

MCP9805 can be incorporated in various systems which include dual in-line memory module (DIMM) temperature monitoring systems, personal computers and servers. Commonly, it can be used as a general purpose temperature sensor.