Raspberry Pi Temperature Sensor

A few weeks ago, I purchased a Raspberry Pi. After reading Eben Upton’s Raspberry Pi User Guide, particularly the two chapters on which he focuses attention on the GPIO, I had an idea for my first project. The post covers the first iteration of a home temperature monitoring project I put together using a Raspberry Pi, a MCP9808, an old Mac Mini (early 2008), InfluxDB, Grafana, a bit of Python, and runit.

The Sensor Hardware

For this project I chose to use the MCP9808 Breakout Board from Adafruit – an excellent source for components, circuits, and ideas. I chose this unit for a few reasons:

  1. It’s controlled over I²C – Raspberry Pi’s GPIO supports the require I²C bus over pins 3 (Serial Data Line, SDA) and 5 (Serial Clock Line, SCL)
  2. It runs in the 2.7V to 5.5V power and logic range – the Raspberry Pi provides power lines at 3.3V and 5V
  3. It was pretty cheap (< $5 USD) – My soldering skills are not the best.

Raspberry Pi Temperature Sensor

Circuit Assembly

The MCP9808 Breakout Board ships from Adafruit mostly assembled. This particular kit requires only that you solder the included 8 pin header strip to the breakout board.

I used a GPIO Breakout and a breadboard to connect the Raspberry Pi to the MCP9808; this approach is a bit easier to manage, correct wiring mistakes, and less permanent than soldering the sensor to the Raspberry Pi. To read temperatures from the MCP9808, only the power pin, ground, and the I²C SDA and SCL pins are required:

The remaining, optional, pins are not used in this project. They provide workarounds for I²C addressing issues when multiple devices are used on the same bus and a pin for alerting if the sensor reads a temperature above or below a threshold.

The Datastore

I knew that I wanted to back the project with a persistent datastore. This would allow me to capture data points and later analyze them for general trends, cross-reference heating/cooling patterns with weather events, etc.

I chose InfluxDB because of its time centric query language and storage model. I installed Influx on an old Mac Mini (Early 2009 with OSX 10.10) I had sitting under my desk. Getting a basic installation of InfluxDB up and running is well documented; since I already use the Homebrew to manage most of my 3rd party dependencies and a formula for InfluxDB exists, installation was completed by issuing brew install influxdb.

Configure the InfluxDB database

With InfluxDB installed, I setup a database for storing my temperature readings and a database user to manage it. I used my InfluxDB instance’s web console to do this; by default it runs on port 8083 of the InfluxDB host.

Raspberry Pi Configuration

Now that the hardware and datastore are setup, there’s a bit of OS configuraiton needed to an out-of-the-box Raspberry Pi in order communicate with the MCP9808 over the I²C bus.

Enable I²C

By default, the Raspberry Pi does not load the required kernel modules to use the I²C bus. To enable I²C communication over the GPIO, I added the following two lines to /etc/modules


Then reboot the Raspberry Pi

sudo reboot

After the system initalizses the system should be able to recognize the MCP9808 is connected. I used the i2cdetect cli tool to do so:

sudo i2cdetect 1 # channel 1 is the default on the Raspberry Pi B+ model

The Sensor Software

Adafruit provides a MCP9808 wrapper and a I²C abstraction. I made use of both of these in the main driver script for this project.

Install build dependencies

sudo apt-get update
sudo apt-get install build-essential python-dev python-smbus

Install the Adafruit_Python_MCP9808 wrapper

cd ~/Downloads
git clone https://github.com/adafruit/Adafruit_Python_MCP9808/blob/master/Adafruit_MCP9808
cd Adafruit_MCP9808
sudo python setup.py install

This will also install the I²C abstraction as the MCP9808 wrapper depends on it.

Read, Report, Repeat

Next I wrote a little python script, poll.py, to read from the MCP9808 on an interval and report its findings to the mcp9808_test InfluxDB database instance.

Raspberry Pi Temperature Sensor Schematic

#!/usr/bin/python import time import Adafruit_MCP9808.MCP9808 as MCP9808 from influxdb import InfluxDBClient # Generates the necessary payload to post # temperature data into the InfluxDB def temperature_data(degrees_c): return [ { 'points': [[c_to_f(degrees_c)]], 'name': 'Temperature Readings', 'columns':['degrees_f']}] # Converts temperature representations in Centigrade # to Farenheight def c_to_f(c): return c * 9.0 / 5.0 + 32.0 # Initializes comminication with the MCP9808 # over the I2C bus. sensor = MCP9808.MCP9808() sensor.begin() # Defines the interval on which the capture logic # will occur capture_interval = 60.0 # Every 60 seconds # Establishes a connection to the mcp9808_test # InfluxDB instance influxClient = InfluxDBClient('<influx-db-host>', 8086, 'mcp9808', '<my_mcp9808_influxdb_user_password>', 'mcp9808_test') # Read, Report, Repeat while True: temp = sensor.readTempC() print "Temperature {0:0.3} F".format(c_to_f(temp)) influxClient.write_points(temperature_data(temp)) time.sleep(capture_interval)

Now it can be run using the following command; note that the script needs to be run as the root user of the Raspberry Pi in order to interact with the GPIO.

sudo python <path_to>/poll.py


Horray! Everything was up and running… until I kicked the on/off switch of the powerstip under my desk. At this point I realized that I wanted to ensure that the poll.py script ran so long as the Raspberry Pi had power. To achieve this, I used the runit process supervisor.


For more detail: Raspberry Pi Temperature Sensor

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top