Although the DS18B20 just looks like a regular transistor, there is actually quite a lot going on inside.

The chip includes the special 1-wire serial interface as well as control logic and the temperature sensor itself.

Its output pin sends digital messages and Raspbian/Occidentalis includes an interface to read those messages. You can experiment with the device from the command line or over SSH (see Lesson 6), before we run the full program.

Add OneWire support

Start by adding the following line to /boot/config.txt

You can edit that file with nano by running sudo nano /boot/config.txt and then scrolling to the bottom and typing it there

Download: file

Then reboot with sudo reboot. When the Pi is back up and you're logged in again, type the commands you see below into a terminal window. When you are in the 'devices' directory, the directory starting '28-' may have a different name, so cd to the name of whatever directory is there.

Download: file
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
cd 28-xxxx (change this to match what serial number pops up)
cat w1_slave

The interface is a little unreliable, but fortunately it tells us if there is a valid temperature to read. It's like a file, so all we have to do is read

The response will either have YES or NO at the end of the first line. If it is yes, then the temperature will be at the end of the second line, in 1/000 degrees C. So, in the example above, the temperature is actually read as 20.687 and then 26.125 degrees C.

If you have more than one Sensor connected, you'll see multiple 28-xxx files. Each one will have the unique serial number so you may want to plug one in at a time, look at what file is created, and label the sensor!

This guide was first published on Jan 28, 2013. It was last updated on Jan 28, 2013. This page (DS18B20) was last updated on May 04, 2015.