Measuring Temperature with a Raspberry Pi Temperature Sensor (1-Wire)

7. November 2017

One of the many applications of a Raspberry Pi is measuring the temperature. In projects like weather stations you want to know the exact value. Using a 1-Wire Raspberry Pi temperature sensor makes it very easy to read it out and you will only need one GPIO pin.

In this tutorial it is described how to use the DS18B20 Raspberry Pi temperature sensor and what software you will need. In other projects, you could use this sensor even as a trigger for cooling your Raspberry Pi case and turning a fan on.



For this tutorial I have used the following parts:

The temperature sensor DS18B20 is also available as a waterproof version.

If you want a more advanced sensor, which can measure also humidity, you should take a look at the DHT11 or DHT22 sensor, which measures both – temperature and humidity.


Description of the 1-Wire Raspberry Pi Temperature Sensor

The temperature sensor returns a 9 to 12 bit long value which can be read and converted. However, this is (so far) only possible on GPIO pin 4 (pin 7). But you can also switch several sensors in series (because it is based on the 1-Wire protocol).

Wiring of the Temperature Sensor

Shown is the wiring of or DS18B20 temperature sensor, including a resistor. I have taken a 4,7k Ω pre-resistor:

Raspberry Pi Temperature Sensor DS18B20 1 Wire Setup


If everything is wired accordingly, we can enable the 1-Wire protocol with it:

sudo modprobe w1-gpio
sudo modprobe w1-therm

We can find out if it worked by entering the following:


The modules should now be listed, if not a GPIO pin other than 4 is used or an error occurred while activating.

So that the modules are loaded at every start, we enter them into the file /etc/modules:

sudo nano /etc/modules

and last add the following two lines:


For the next step, we first need the ID of the sensor. If you plan to connect several in series, it is best to test each one individually and make a note of the ID so that you do not confuse it later.

We change the directory and print the files

cd /sys/bus/w1/devices/

One of the files is called 10-000802b4ba0e (different for you) and is the ID we use to query the sensor (please adjust ID):

cat /sys/bus/w1/devices/10-000802b4ba0e/w1_slave

In the output we see the last indication of the temperature (in “milli degree”)

31 00 4b 46 ff ff 05 10 1c : crc=1c YES
31 00 4b 46 ff ff 05 10 1c t=24437

Divided by 1000, makes it 24,437 ° C.


Script for extracing the temperature value of the DS18B20

To read the temperature easier (e.g. using it in smart home projects like openHAB), I wrote a little script. If you later want to get the output by simply typing in temperature, you should put it in /usr/bin/:

sudo nano /usr/bin/temperature

The content:

Now give appropriate rights:

sudo chmod +x /usr/bin/temperature

and now if you go to the console and type in


then, the currently measured temperature appears. If an error message comes, then you have to install bc.

sudo apt-get install bc

Finally, a little tip: If you want to measure with the sensor outside (for example, hanging from the window) and you can not use large wires through the window sea, a ribbon cable is suitable, too (old IDE or floppy drive cable). Well packaged in a cling bag, the whole is then weatherproof (let air out and glue well, then it affects the measurement even minimal). Or you just buy a waterproof sensor, then you could e.g. also measure the temperature of your water boiler.

Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!