Raspberry Pi – sensing temperature

A small project, good for beginners, and can be done in 15 minutes. The Raspberry Pi as a temperature monitor using munin.


There has been a Raspberry Pi Model B in my possession for more than 1,5 years. In the early months my system administrator part was strong, so it was used as a DNS/DHCP/LAMP server with Raspbian and Arch. Later it was used as a smart extension for a HDMI capable TV with OpenELEC, but as a physicist it was not put to proper use.

120412_1049_OpenELEC30B1On a course at the university we were learning about micro-controllers, FPGA and some useful sensor extensions. We were programming an ATmega128 to read the temperature from a DS18B20, and write it out on a LCD. The funny thing was that everything was digital, because the sensor had an AD converter inside itself, and the communication was done with the Dallas 1-wire protocol. The bad thing was that we had to write everything from scratch using the datasheets. But later that week we had a Free Software Conference in Szeged, and one session was about the Raspberry Pi, where the lecturer was speaking about sensors too. For me the most valuable piece of information was that the Linux kernel has the 1-wire protocol built inside, and that was the spark for me.


  • Raspberry Pi B + 4GB SD card
  • Preinstalled Raspbian (or any other normal distro) || [setup]
  • DS18B20
  • 4k7 Ohm resistor
  • Wires
  • Internet connection

Don’t  get confused with the two sensor types: B & S. The main difference is that the S is the direct replacement for the original DS1820, and has a 9 bit A/D converter. The other has a changeable A/D resolution from 9 to 12. As there is no difference in price, choose the B one.

Pins and wires:

We will use the following pins on the Raspberry Pi:

76219_gpioPIN1 (3.3V), PIN7 (GPIO4), PIN9 (GND)

This is a bit different layout as to what other projects use. I prefer to use only one PIN line, this way PIN6 (GND), PIN8 (TX), PIN10 (RX) can be used for a serial connection with a nokia CA-42 cable.

On the DS18B20 we will use all three pins as the following picture shows:


Warning: please double check the wires and pins, because if you mix 1&3 your sensor will heat up (it will survive), but the GPIO pin will get fried and you won’t be able to use it anymore. (In the /boot/config.txt file you can change the w1 comm line to any other GPIO)

The communication goes through on the middle pin, and by default it is on “high”. This can be achieved by putting a pull-up resistor (4k7Ω) between the PIN1 and PIN7 on the Pi. You might wonder how the communication is achieved. If you don’t want to read the datasheet, it can be summarized like this: the data line is shorted to gnd and then released to return to its high state. If the short was long, than a 0 is transferred, if it was fas than a 1 is transferred. The master (currently the pi) initializes the connection using a sensor address, and only that one sensor answers.

A sensor can run in parasite mode, when it’s 3.3V pin is connected to the ground. This way it will get its power from the data line (as we remember, it is by default on high [3.3V]). In this mode the precision will be worse and the 1-wire network not reliable.

2013-12-04 07.33.49Using a CD Audio cable from an old PC.

OS Level:

Before we can begin reading temperatures out, we need to load the one wire kernel modules:

sudo modprobe w1-gpio
sudo modprobe w1-therm

Or to make this load permanent the module names can be added to /etc/modules

If there is no problem the following lines will appear in /var/log/syslog

Driver for 1-wire Dallas network protocol.
w1_master_driver w1_bus_master1: Family 28 for 28.0000048d49a4.5e is not registered

This means that the sensor with it’s 64 bit unique address has been found. And the best part comes here. On a UNIX like system every device is shown as a file, so communicating with our sensor can be done as simple as reading a file:

$ cat /sys/bus/w1/devices/28-*/w1_slave
3c 01 4b 46 7f ff 04 10 40 : crc=40 YES
3c 01 4b 46 7f ff 04 10 40 t=19750

The first line is about error checking, the second line is the temperature in Celsius multiplied by 1000. You don’t have to work with low level communication, or conversion. This is done inside the kernel.

The number of sensors is limited to 10 by default. (49th line) To increase change the line and recompile the kernel.

Please check the /sys/bus/w1/ directory, there are some cool info stuff there!

What’s next?

  1. Munin Monitoring with Apache »
  2. Get things Salty
  3. Extension with a DHT22
  4. Extension with an ADXL345

From 1:


References: [link], [link], [link], Adafruit lesson, RPi Low Level, Logout.

UPDATE 2015: Kernel 3.18.* PROBLEM

The new Raspberry Pi kernel has switched to device trees, this will break the w1 module.


UPDATE 2015: Kernel 3.18.* FIX

Add the following lines to the end of /boot/config.txt

# 1-wire device tree

Author: Gajdos Tamás

A "barefoot physicist" with some IT skills in system administration.

2 thoughts on “Raspberry Pi – sensing temperature”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s