Skip navigation.

Using a Raspberry Pi to Read A DS-18B20 Temperature Sensor

There are a number of ways to read a 1-wire DS-18B20 temperature sensor via a Raspberry Pi.  As of December 2012, the Wheezy OS contains a kernel loadable module that allows you to directly read them.  At the moment, I won't go into how to do this, as you can read some other peoples in-depth web pages on how they have done this:

Here are a few notes on my test implementation.  I first updated my RPi OS. After the upgrade reboot, I could then load the required kernel modules:   sudo modprobe w1-gpio  and:  sudo modprobe w1-therm 

To permanently load the 1-wire kernel modules, you can add the lines below into this file:  /etc/modules  

  • w1-gpio
  • w1-therm

From there, I could see the attached devices that I had plugged into GPIO #4:

 ls -1 /sys/bus/w1/devices/
28-000000a2e3fe@
28-000000a30215@
w1_bus_master1@

and I could simply read the temperature from the command line:

 cat /sys/bus/w1/devices/w1_slave
fd 01 4b 46 7f ff 03 10 b6 : crc=b6 YES
fd 01 4b 46 7f ff 03 10 b6 t=31812

the text above in bold is the current temperature this afternoon in my computer room. Note, it's read in an integer form, so just divide the reading by 1000.

From the second link (Adafruit) listed above, I used the python script to output a Celsius temperature every 60 seconds:

 python read_temperature.py
31.312
31.312
32.062
31.75
31.312
31.437

Note, this will only read the first sensor it finds.  It's now time to run some extra Cat-5 network cable around the house, so I can record various air temperatures

For a future project, I want to make up some water-proofed DS-18B20 sensors and log how the ground temperature changes at various depths over the course of a few years.