I have gone through some tutorials for interfacing with the DS18B20, In those, They have given an explanation of how to interface the DS18B20 using Arduino Platforms Only. The least countable people explained how to interface with DS18B20 With the Raspberry using Python, in that they have interfaced only One single DS18B20. Here in this project, I am Explaining How To Interface Nine(9) DS18B20 Temperature sensors with Raspberry Pi 4 B.
For one of the projects, I needed to interface with 9 Waterproof Temperature sensors and Relay and Proximity sensors. Using Python Programming to read the temperature From the DS18B20 was not easy initially. After a lot of workouts In the final, I read multiple DS18B20 temperatures Data. In this project, I have used Raspberry Pi 4 B and Raspbian os Bullseye.
DS18B20
DS18B20 Sensor Specifications
- Programmable Digital Temperature Sensor
- Communicates using the 1-Wire method
- Operating voltage: 3V to 5V
- Temperature Range: -55°C to +125°C
- Accuracy: ±0.5°C
- Output Resolution: 9-bit to 12-bit (programmable)
A unique 64-bit address enables multiplexing
- Conversion time: 750ms at 12-bit
- Programmable alarm options
- Available as To-92, SOP, and even as a waterproof sensor
Where to use DS18B20 Sensor
The DS18B20 is a 1-wire programmable Temperature sensor from Maxim integrated. It is widely used to measure temperature in hard environments like in chemical solutions, mines or soil, etc. The constriction of the sensor is rugged and also can be purchased with a waterproof option making the mounting process easy. It can measure a wide range of temperatures from -55°C to +125° with a decent accuracy of ±0.5°C. Each sensor has a unique address and requires only one pin of the MCU to transfer data so it is a very good choice for measuring the temperature at multiple points without compromising many of your digital pins on the microcontroller.
How to use the DS18B20 Sensor
The sensor works with the method of 1-Wire communication. It requires only the data pin connected to the microcontroller with a pull-up resistor and the other two pins are used for power as shown below.
The pull-up resistor is used to keep the line in a high state when the bus is not in use. The temperature value measured by the sensor will be stored in a 2-byte register inside the sensor. This data can be read using the 1-wire method by sending in a sequence of data. There are two types of commands that are to be sent to read the values, one is a ROM command and the other is a function command.
Configuring The Raspberry Pi to Communicate with DS18B20 Using One- wire Communication
In the raspberry pi, We need to enable the Interfaces after the fresh installation of Raspbian OS.
To enable the one-wire interface use :
sudo raspi-config
Scroll to the interfacing option and click on it and then enable the One-wire Interface.
To add support for OneWire, we first need to open up the boot config file, and this can be done by running the following command. to do that I have used a nano text editor, you can use Terminal for that by entering the below command
sudo nano /boot/config.txt
Next At the bottom of this file enter the following.
dtoverlay=w1-gpio
Once done save and exit by pressing CTRL + X and entering Yes(Y). Now reboot the Pi by running the following command.
sudo reboot
Once the Raspberry Pi has booted back up, we need to run modprobe so we can load the correct modules.
sudo modprobe w1-gpio
Then
sudo modprobe w1-therm
Now change into the devices directory and use the ls command to see the folders and files in the directory.
cd /sys/bus/w1/devices
ls
List the file and a 28-xxxxxxxxxxxx device directory (e.g. here is 28-00000674869d) will be found. This is the ROM of DS18B20. If more than one DS18B20 is connected, you will find a certain directory of more than one.
cd 28-00000674869d
Now run the following cat command.
cat w1_slave
The above command should output data but as you will notice it is not very user-friendly.
The first line should have a YES or NO at the end of it. If it is YES, then a second line with the temperature should appear. This line will look similar to something like t=12323, and you will need to do a bit of math to make this a usable temperature that we can understand easily. For example, in Celsius, you simply divide by 1000.
The YES in the first line indicates CRC check success (Data Valid ). The number following t= is the temperature, 28750 stands for 28.7 (C).
Graphical Representation of Interfacing of Multiple DS18B20 Temperature Sensors with Raspberry Pi.
The output of the Project contains 9 DS18B20 Temperature Data in Celcius and Fahrenheit
Comments