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

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)

Raspberry Pi DS18B20 Schematic
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
Raspberry Pi DS18B20 Open Config

Add the below line to the end of this file as on below screenshot

Raspberry Pi DS18B20 Edit Config

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.


Now you will get a code starting a number like 28-0000… as on below screenshot.

Raspberry Pi DS18B20 Device ID

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
Raspberry Pi DS18B20 Cat Command

Now you should see an output like the below screenshot.

Raspberry Pi DS18B20 Device Read Values

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()
    return lines
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        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:

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.

Raspberry Pi DS18B20 Reading Temperature

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.

Comments Area
Add your questions and comments here. Comments will be published after the admin approval


Similar Projects

Go Top