Temperature Monitoring with Raspberry Pi and DS18B20
Author : Dinesh Kumar Wickramasinghe
Introduction and Images
Hello friends, here we are going to another interesting experiment with Raspberry Pi. We are going to build a thermometer with the waterproof digital temperature sensor DS18B20 with Raspberry Pi. Later you can use this project with more experiments like IoT projects. Previously I posted a project about creating a thermometer with DSa8B20 and Arduino. You can also refer this project to learn more information about DS18B20 digital temperature sensor.
Here are some images of my completed project.
What you need?
- Raspberry Pi (I use Raspberry Pi3 Model B)
- 4.7 K Resistor (As a pull up resistor)
- Some hook up wires
Schematic
Please follow the below schematic to connect the DS18B20 digital thermometer to your Pi. Normally the black color wire is ground, red one is to 3.3v and here connect the yellow data wire to Raspberry Pi GPIO 4 (Pin no 7)
Steps to Configure Raspberry Pi
Before we write our Python code, we need to follow few steps to configure our Pi. This is because the DS18B20 digital temperature sensor communicates via ‘One Wire’ protocol. Raspberry Pi is not ready to read one wire data by default. So follow the steps below carefully to enable the one wire support for Pi.
Open a new terminal window and execute the below command
sudo nano /boot/config.txt
Add the below line to the end of this file as on below screenshot
dtoverlay=w1-gpio
To save this file, Press Ctrl + x Then press ‘Y’ and press Enter. Now you need to reboot your Pi to continue the next steps. You can use the below command to reboot the Pi.
sudo reboot
Once your Pi reboot, open a new terminal window and enter below commands one by one.
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
Now enter the below command and press enter.
ls
Now you will get a code starting a number like 28-0000… as on below screenshot.
For me, the code is 28-80000026aca2
Enter below command with the code you received.
cd 28-80000026aca2
Now enter below command
cat w1_slave
Now you should see an output like the below screenshot.
Please note that the number 30937 is my room temperature in Celsius format. Actually the value is 30.937 but is shows as 30937.
Now you have done the configuration part.
Python Source Code
Now let's write an automated python script to read the temperature and show us. Here is the code.
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
Save the above code as a Python code (with .py extension) My python file name is ds18b20.py
Now open a new terminal window and locate to the place where you saved your code and enter the below command to execute it.
sudo python ds18b20.py
If you are not familiar with writing and executing Python codes on Raspberry pi, please refer my LED blink tutorial. It has all the steps in details of writing and executing Python codes. Here I am not explaining them again in detail.
Now you will see an output like the below screenshot. It shows the temperature readings in both Celsius and Fahrenheit formats.
Hope you enjoyed this project. If you faced any issues or errors, put them as a comment. I will try my best to help you.