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

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.

7 Responses

  1. I tried this example using the raspberry pi 3b+ but at the part :
    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 — HERE APPEAR ERROR
    00-400000000000 00-c00000000000 w1_bus_master1
    SO IF i TRY DOES DO THE “CAT” COMMAND( cat /sys/bus/w1/devices/00-200000000000/w1_slave) ALWAYS PRESENT ERROR. WHAT MUST BE WRONG???

    • I am using Raspbian buster and found the device path for 1-wire has changed to /sys/devices/w1_bus_master1/28-xxxxxxxxxxxx

  2. How do you connect series of sensors and read both temperature and humidity from each one, connected at the same time?

  3. hi, so i have a little question.
    I am a beginner in Raspberry pi and need some help with this temp reader.
    When i do this command:
    cd /sys/bus/w1/devices/
    i find 2 sometimes 3 adresses that pop up like this:
    And if i try to slave them some times some work and sometimes some don’t.
    but the problem is that they keep changing and i don’t know why.
    Could someone help me with this?

    • Also forgot to say that the 1 time in 5000 try’s it works i dont get any temp, it just says 0 everywhere.

  4. Hi,
    The DS18B20 sensor family should have folders named “28-………”
    If you see folders named “00-………” instead, then you probably have unreliable electrical connections or you connected sensor’s terminals the opposite way (GND to 3.3V and 3.3V to GND).

  5. I was under the impression that when using 3 wires (non parasite mode) the system could send the command to have the ds18b20 start reading their temperature all at the same time. Thus, reading all the ds18b20 can be done without any further delay. Right now, there are a significant delay for each read. That delay was the price to pay if you wanted to use only 2 wires. Any idea how to read them all without that delay?


Leave a Comment

Your email address will not be published.

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