Updated on February 18, 2021 • 47 • 4.7
The Raspberry Pi opens the door to many home automation projects. One of the most basic applications in terms of home automation is knowing the temperature. In this tutorial, we will see how to read the ambient temperature using the DS18B20 sensor on Raspberry Pi.
The DS18B20 sensor is the most used on Raspberry Pi, particularly for its ease of use. We will also see that we can read multiple sensors at the same time to calculate averages, etc.
If you are interested, you can then follow this tutorial, which shows how to create temperature graphs in Python.
To complete this tutorial, you will need:
Once you have all these elements, you can proceed directly to wiring!
The DS18B20 sensor is wired following the diagram below. For testing, using a breadboard is particularly convenient. Later, once everything works, it is preferable to solder the resistor with a soldering iron for a cleaner setup.
Once the sensor is properly wired, you can read the temperature using the Raspberry Pi console. Specifically, the DS18B20 sensor transmits its data via 1-Wire bus. This bus also allows easy connection of multiple sensors in parallel (we will see this later in the tutorial). When a sensor is connected, a folder is added in the path /sys/bus/w1/devices. This folder contains a file with the information provided by the sensor, including temperature.
The goal is therefore to read this file. Here are the Unix commands that allow it:
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices/
ls
cd 28-xxxxxxxxxx
cat w1_slave
ℹ️ Make sure to replace the x with the value given by ls
🔴If this is the first time you enter these commands, the sensor may not be detected, go to the "sensor not detected" box below to solve the problem
By executing these commands, you get a result similar to the image below. Here, we actually read a temperature of 6.875 °C (yes, it's precise).

Sometimes the Raspberry may not detect the temperature sensor. For it to detect it, you need to modify two system files:
Add the following lines to the file located in /etc/modules:
w1-therm
w1-gpio pullup=1
Add the following line to the file located in /boot/config.txt:
dtoverlay=w1-gpio
Restart the Raspberry Pi.
To modify a file, type in the console:
sudo nano /.../../...
To modify the file located in /etc/modules, type:
sudo nano /etc/modules
All these commands are nice, but we've seen better in terms of practicality! That's why we will use a Python script that formats all this, to get the temperature with a simple command.

This program will read the w1_slave file, extract the temperature, and finally display it on standard output.
We now know how to get the sensor temperature using the Python script. We can now use this in many applications. For example, save this temperature to an external file.
To do this, we can add a line to a file at each temperature reading with the date, followed by the temperature.
To change the reading interval, modify the time.sleep() which is in seconds. Here, we record the temperature every minute.
It can be interesting to read the temperature from multiple sensors simultaneously. To do this, nothing too complicated. You need to wire the sensors in parallel as shown in the following diagram.
With this new sensor, if we re-list the files contained in /sys/bus/w1/devices, we find two files, one for each sensor.

Then just modify (a tiny bit) the programs given previously.
This program takes into account the addition of other sensors, without even having to touch it. With two sensors, you get something like this:
Now that we know how to read multiple temperature sensors simultaneously, we can calculate an average of the two values obtained. Just add the following two lines to the previously written Python program.
There you go, the tutorial is complete, we now know how to read one or more DS18B20 temperature sensors on Raspberry Pi.
Choose and use a camera on Raspberry Pi
Control a Servo motor in Python
Raspberry Pi 5 (8 GB version)
94.48 €
Raspberry Pi camera 5MP 1080p
12.95 €




4.7/5 | 54 votes
