While searching for a simple way to measure temperature using my Raspberry Pi I came across the DS18B20 1-wire digital temperature sensor. This promised an accurate way of measuring temperature with a few wires and almost no external components.
The device only cost a few pounds and it seemed too good to be true …
So I bought one. Within a few minutes I was measuring the temperature with it. It is so simple to use and the perfect starting point for creating a Raspberry Pi based temperature data logger.
The diagram on the right shows the DS18B20 device. It has three pins and comes in a TO-92 package which means it looks similar to other devices you may have used such as transistors.
Pin 1 is Ground. Pin 2 is the data pin and Pin 3 is the power pin. The only external component required is a single 4.7Kohm resistor.
In my testing I didn’t have one of these so I used 2 x 2.2Kohm resistors in series. This worked fine.
I used a small piece of breadboard and some jumper cables to connect it to the GPIO header on my Raspberry Pi.
Pin 1 was connected to P1-06 (Ground)
Pin 2 was connected to P1-07 (GPIO4)
Pin 3 was connected to P1-01 (3.3V)
A 4.7Kohm resistor was placed between Pin 2 and Pin 3.
It is important to double check that you don’t confuse Pin 1 and Pin 3 on the device otherwise the power will be applied the wrong way round!
Once you have connected everything together you can power up your Raspberry Pi.
It’s always best to tackle new projects with an updated SD card. I tend to use the latest Raspbian image from the RaspberryPi.org download page and then update it from time to time using the following two commands :
In order to configure the sensor you just need to make a small change to the config.txt file using :
sudo nano /boot/config.txt
add the following line to the bottom :
You can save the file using CTRL-X, Y then RETURN. The device is setup to report its temperature via GPIO4.
For the changes to take effect you will need to reboot using :
Use the commands below to go to the directory that contains the detected 1-wire devices :
For more detail: Raspberry Pi 1- Wire Digital Thermometer Sensor (DS18B20)