HowTo: Build your own temperature logger with Raspberry Pi (DS18B20)

Last week I received my fourth Raspberry Pi. Yes, I really love this tiny machines. I already build a media center (xbmc), a smart home light switch + android app, a remote backup system using openvpn, samba and rsync, security camera using motion and kinect and a temperature logger.

Since some people wanted to know how to build it, i will make a “short” tutorial.
chart of temperature logging

1. Go Shopping:
1x DS18B20 (Datasheet / Amazon US / Amazon UK / Amazon DE  )

1x Resistor 4,7 k Ohm ( Amazon US / Amazon DE )
4700 Ohm Resistor

(optional) 1x Breadboard + Wires (Amazon US / Amazon DE)

2. Circuit

First of all we have a look at the sensor, the Dallas 18b20. On the illustration you can see the function of each pin.
(Source: Datasheet of DS18B20)

Since the raspberry pi is not able to work with analog signals it seem to be strange that the sensor is working. The answer is: It is a one wire bus sensor. It outputs the temperature in binary code, e.g.: 0000 0001 1001 0001 = 25 C°
Each sensor has a UNIQUE number which allows us to use even multiple sensors with one wire !

Ok now it’s time to wire all together.  Since i have no more red and black wires its bit colorful  🙂 sorry for that.

wiring raspberry pi gpio

btw.: i guess the circuit is also in the datasheet… anyway:
Now connect it to your breadboard:

breadboard temperature

That’s it ! Ok let’s check if you did it correct.

Step 3: “installing” the sensor end write a bashscript to read the temperature in °C.

After starting you Raspberry Pi, type:

sudo apt-get install bc
sudo modprobe w1-gpio
sudo modprobe w1-therm

This allows us to use the one wire transmission. Now we double check whether the raspberry pi recognized the sensor.

cd /sys/bus/w1/devices/

Bildschirmfoto 2014-01-10 um 14.37.50

There it is! 28-0000054bb784 is the id of mine. To read from the sensor we can type:

cat  /sys/bus/w1/devices/28-0000053bb784/w1_slave

Bildschirmfoto 2014-01-10 um 17.46.14

Now we can write our bash script to read the temperature. Since i have no idea of regex it was hard for me. So if someone has a better solution to get the value behind ‘t=’ it would be nice if you could write it in the comments. So “nano” and copy/paste:

catresult=`cat  /sys/bus/w1/devices/28-0000053bb784/w1_slave | tail -n 1| egrep -o ‘.{5}$’`
temperature=`echo “scale=2; $catresult / 1000” | bc`
echo $temperature

Bildschirmfoto 2014-01-10 um 18.13.46

Bildschirmfoto 2014-01-10 um 18.16.50


Step 4: Installing postgres

First of all install postgres and other stuff we will need later.

sudo apt-get install postgresql libpq-dev python-dev python-setuptools easy_install pip

login as postgres user and create new user :

sudo su – postgres
createuser tempuser -P

Enter a password and pack it superuser. Start psql and add this new db:

create database temperature;

CRTL+D and type “psql temperature” to log into and create new table:

(if you had problems with tempuser try: )
(ALTER USER tempuser with password ‘12345678’; )

CREATE TABLE temp_record(
temp_id    serial primary key,
temperature       double not null,
stamptime        TIMESTAMP

press CTRL+D to exit psql. Type “exit” to logout as postgres user.
If you like to reach the DB from other machines in your network, we need to configure that.

sudo nano /etc/postgresql/9.1/main/pg_hba.conf

add this line:

host    all             all           trust


sudo nano /etc/postgresql/9.1/main/postgresql.conf

change listen_addresses to this line:

listen_addresses = ‘*’

restart the service:

sudo service postgresql restart

Step 5: A little python script

To use python with postgresql a special library is necessary

pip install psycopg2

No create new file with “nano” and copy/paste this:
(Mine Pi’s IP: change it to yours Pi’s IP)

import psycopg2
import commands
import datetime

call = commands.getoutput(“bash /home/pi/”)

print call

    conn = psycopg2.connect(“dbname=’temperature’ user=’tempuser’ host=’′ port=’5432′ password=’12345678′”)
print “I am unable to connect to the database”

cur = conn.cursor()
cur.execute(“INSERT INTO temp_record (temperature,stamptime) VALUES (“+str(call)+”,NOW()); “)


Save and next Step ! 😉

Step 6: add cron job:

to temporarily add a cron job write:

crontap -e

5 0 0 0 0 python /home/pi/


Posted on January 10, 2014, in Uncategorized. Bookmark the permalink. Leave a comment.

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: