If you haven’t check out part 1 of this article where I talk about installing your first raspberry pi disk image, please do that first!
Now that you have your Raspberry Pi up and running it’s time to start reading temperatures. We will be using the bash prompt for this part of the tutorial. In the next part of this series, I will show you how to read temperatures using Python.
I am going to assume from now on you’ve wired up one or more DS18B20(s) to Raspberry Pi. So boot it up and wait for the command line.
Reading Temperatures on the Command Line
The best thing about using DS18B20 temperature probes (and other 1-wire communication devices) in the Raspian kernel is that each device is represented by a directory on the file system. So the easiest way to test the sensor is to cat the value directly from the command line.
Load the Kernel Modules
The first thing we need to do is load the kernel modules for the 1-wire system. These come installed on the Raspbian image, but aren’t loaded by default. Type the following:
sudo modprobe w1_gpio && sudo modprobe w1_therm
This will load the w1_gpio and w1_therm modules temporarily for your current session. If you reboot your Raspberry Pi, you will have to reload them. In order to load them automatically, you can add 2 lines to /etc/modules:
sudo sh -c "echo 'w1_gpio\nw1_therm\n' >> /etc/modules"
Now that your 1-wire system is running, we can get some temperature readings.
As I said earlier, each 1-wire device on the 1-wire bus will appear as a directory. You can find these devices in the following directory:
On my fermentation Raspberry Pi, I have the following files in that directory:
pi@raspberrypi ~ $ ls -l /sys/bus/w1/devices/
lrwxrwxrwx 1 root root 0 Jan 6 16:43 28-00000449e4f6 -> ../../../devices/w1_bus_master1/28-00000449e4f6
lrwxrwxrwx 1 root root 0 Jan 6 16:43 28-00000449ef31 -> ../../../devices/w1_bus_master1/28-00000449ef31
lrwxrwxrwx 1 root root 0 Jan 6 16:43 28-0000044a00b2 -> ../../../devices/w1_bus_master1/28-0000044a00b2
lrwxrwxrwx 1 root root 0 Jan 13 13:20 w1_bus_master1 -> ../../../devices/w1_bus_master1
The w1_bus_master1 file is a special file that will always appear, but each sensor will appear as 28-00000xxxxxxx. Since each 1-wire device has a hard-coded address, each found sensor will always show up with the same directory name. This is very convenient!
Read the Temperature
Each one of these 28-xxx directories will contain a w1_slave file. In order to get that sensor’s temperature reading, we just cat it!
pi@raspberrypi ~ $ cat /sys/bus/w1/devices/28-00000449e4f6/w1_slave
0b 00 4b 46 7f ff 05 10 95 : crc=95 YES
0b 00 4b 46 7f ff 05 10 95 t=21687
Success! The temperatures come out as the actual temperature times 1000, so we just divide by 1000 and we get 21.687° celcius.
Since the 1-wire system is software based in the kernel, we will sometimes get a CRC=NO:
ff ff ff ff ff ff ff ff ff : crc=c9 NO
08 00 4b 46 7f ff 08 10 d9 t=-62
When this happens, you should ignore the value and re-request it. Fortunately, this happens quite infrequently.
Check back for Part 3 of this series where I will talk about reading these temperature values in Python.