DS18B20 Temperature Measurement Module

Adding a digital temperature sensor to the Raspberry PI is easy, especially with the DS18B20 1-Wire Digital Temperature Sensor Module. Each module has an unique address that allows them to be chained together on the same control line. Also, they can be attached directly to your controller without the need for external resistors and can operate off of 3.3VDC or 5VDC. Schematics and a data sheet is included at the end of this article.

ds18b20_module

Connecting the Hardware:

The w1-gpio kernel driver installed on the unit requires the use of GPIO 4 (pin 7) for 1-wire communications. A great interactive Raspberry Pi Pinout is posted at Gadgetoid. Here are the connections needed:

DS18B20 PI (MODEL B2) NOTES
VDD 3.3V (pin 1 or 17) OR 5V (pin 2 or 4) USE ONE PIN ONLY!
DQ GPIO 4 (pin 7)
GND Ground (pin 6, 9, 14, 20 or 25) USE ONE PIN ONLY!

Enabling Device Drivers Under Raspbian “wheezy”:

On some Raspberry Pi software distributions, SPI and I2C kernel modules are explicitly set not to be loaded (blacklisted). We will need to edit the ‘/etc/modprobe.d/raspi-blacklist.conf’ file to remove these restrictions. Place a comment (#)  at the front of the lines containing spi-bcm2708 and i2c-bcm2708. A reboot is needed.

sudo nano /etc/modprobe.d/raspi-blacklist.conf

#blacklist spi and i2c by default (many users don’t need them)
#blacklist spi-bcm2708
#blacklist i2c-bcm2708

sudo reboot

Communicate with the Installed Sensors:

Open a terminal program (like PuTTY) and connect to your unit.

Before we can poll the DS18B20, we have to register the new sensors and add temperature support.

sudo modprobe w1-gpio
sudo modprobe w1-therm

Find the sensor address you want to poll. The /sys/bus/w1/devices directory contains all the found DS18B20; addresses will start with 28-.

cd /sys/bus/w1/devices
ls -l

Poll the sensor with its unique serial number.

cat 28-xxxxxxxxxxxx/w1_slave

Important: replace 28- xxxxxxxxxxxx with your units address; example 28-001414b09cff!

ds18b20_putty

A valid response will contain two lines. At the end of the first line, a YES or NO will be displayed. If yes, the temperature will be at the end of the second line in 1/1000 degrees Celsius. Google has a Celsius to Fahrenheit Converter if needed.

Simple DS18B20 Script:

To simplify the process of polling all the modules installed, you can use the following script:

ds18b20_putty_script

The script depends on: bc – An arbitrary precision calculator language.

sudo apt-get install bc

Important: remember to set the file permissions on the script!

References:

DS18B20 – Data Sheet
DS18B20 – Temperature Measurement Module Schematics

Shaun Bennett has written 6 articles

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">