Raspberry Pi Temperature Sensor using the DS18B20

In this tutorial, I will be going through the steps on how to set up your very own Raspberry Pi temperature sensor.

Raspberry Pi Temperature Sensor

Like most of the sensor tutorials, the process of setting up this sensor is pretty straightforward and consists of a basic circuit and some code.

I will be using a ds18B20 waterproof sensor, and this probe can provide temperatures over a one-wire interface. Even better this is waterproof making it perfect if you need to use the sensor in a wet environment.

There are many other temperature sensors that you can use with your Raspberry Pi but this tutorial we will be focusing on the ds18b20.

You will find the ds18b20 temperature sensor handy in a lot of situations, especially when it comes to monitoring the temperatures of surfaces and liquids.

Equipment

The equipment that you will need for this Raspberry Pi temperature sensor is listed below.

Keep in mind the breadboard, and the breadboard wire is optional, but I highly recommend investing in these as they may make working with circuitry a lot easier.

Recommended

Optional

Video

If you want to see how to put together the circuit and a rundown of the code, then be sure to check out my video below.

I go through everything there is to know about this cool little circuit in the video. You can also find the full written tutorial right under the video.

Adblock removing the video? Subscribe to premium for no-ads.

Building the Raspberry Pi ds18b20 Circuit

The circuit that we will need to build is pretty straightforward as we only need a resistor and the temperature sensor.

The sensor that I am using in this tutorial is a waterproof version of the DS18B20 sensor. It simply looks like a very long cord with a thick part on one end. If you have just a plain version without wiring and waterproofing, then it looks exactly like a transistor.

This sensor is pretty accurate being within 0.05°C of the actual temperature. It can handle temperatures of up to 125°C (260°F), but it’s recommended you keep it below 100°C (210°F). This device also has an onboard analog to digital converter so we’re able to easily hook it up to a digital GPIO pin on the Pi.

Keep in mind that not all temperature sensors are the same. Something like the TMP36 will not simply replace the ds18b20 in this tutorial.

The TMP36 is an analogue device making it slightly harder to integrate with the Pi. The TMP36 has basically the same problem we had with the light sensor, and this is because the Raspberry Pi doesn’t have any analogue pins.

Putting this circuit together is super simple, I will quickly go through some basic instructions below. If you’re having trouble following them, then please refer to the video or diagram.

If you need more information on the GPIO pins, then be sure to check out my guide to getting started with the Raspberry Pi GPIO pins.

1. First, connect the 3v3 pin from the Pi to the positive rail and a ground pin to the ground rail on the breadboard.

2. Now place the DS18B20 sensor onto the breadboard.

3. Place a 4.7k resistor between the positive lead and the output lead of the sensor.

4. Place a wire from the positive lead to the positive 3v3 rail.

5. Place a wire from the output lead back to pin #4 (Pin #7 if using physical numbering) of the Raspberry Pi.

6. Place a wire from the ground lead to the ground rail.

Once done, your circuit should look similar to the diagram below.  Keep in mind some versions of the temperature sensor may have more wires than just three. Refer to the datasheet for more information for each of the wires.

Raspberry Pi Temperature Sensor Diagram

The Raspberry Pi Temperature Sensor Code

The code for setting up the temperature sensor is a little more complicated than the circuit itself. This complexity is just because of the way we need to handle the data that comes from the sensor.

As we’re using Python, it will be worth learning some of the basics, so I highly recommend checking out our Python beginner’s guide.

Before we make the Python script, we first need to setup the Pi so it can read data from the sensor. To do this, we need to add OneWire support.

1. 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. I like to use the nano text editor.

sudo nano /boot/config.txt

2. At the bottom of this file enter the following.

dtoverlay=w1-gpio

3. Once done save and exit by pressing CTRL + X and then Y. Now reboot the Pi by running the following command.

sudo reboot

4. You can skip to downloading the code onto the Pi or follow the next few steps to check that the sensor is actually working.

5. Once the Raspberry Pi has booted back up, we need to run modprobe so we can load the correct modules.

sudo modprobe w1-gpio
sudo modprobe w1-therm

6. 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

7. Now run the following command, change the numbering after cd to what has appeared in your directory by using the ls command. (If you have multiple sensors there will be more than one directory)

cd 28-000007602ffa

8. Now run the following cat command.

cat w1_slave

9. This 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, Celsius you simply divide by 1000.

temperature output

Now it’s time to move onto the Python script.

I’ll briefly explain the code below if you want to learn more about it. This code was sourced from the tutorial over at Adafruit. If you want to download it, you can simply download it at the Pi My Life Up GIT page or by using the following command.

git clone https://github.com/pimylifeup/temperature_sensor.git

To begin the Python script, we import three packages, OS, Glob and time.

Next, we run the modprobe commands, and these commands are the same as what we used before.

We declare three different variables that will point to the location of our sensor data. You shouldn’t have to change any of these.

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'

In the read_temp_raw function we open up the file that contains our temperature output. We read all the lines from this and then return it so the code that has called this function can use it. In this case the read_temp function calls this function.

def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines

In the read_temp function we process the data from the read_temp_raw function. We first make sure that the first line contains YES. This means there will be a line with a temperature in it.

If there is a temperature we then find the line with t= by using lines[1].find('t='). Lines[1] means we’re looking at the 2nd element in the array, in this case, the 2nd line.

Once we have the line we simply get all the numbers that are after the t= and this is done at lines[1][equals_pos+2:]. Equals_pos is the start position of the temperature (t), and we add 2 to the position, so we only get the actual temperature numbers.

We then convert the number into both a Celsius and Fahrenheit temperature. We return both of these to the code that called this function. This function is the print located in the while loop.

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

The while loop is always true so it will run forever until the program is interrupted by an error or by the user cancelling the script. It simply calls the read_temp within the print function. The print function allows us to see the output on our screen. The script is then put to sleep for 1 second every time it has read the sensor.

while True:
	print(read_temp())	
	time.sleep(1)

Testing the Code

Once you have either downloaded or finished writing up the code and you also have set up the circuit correctly, we can now call the Python script. To call the Python script simply run the following command.

sudo python thermometer_sensor.py

You should now have an output of temperatures in both Fahrenheit and Celsius. You can alter this just to display your preferred temperature scale.

Temperature Sensor Readings

That’s everything you need to know for getting your Raspberry Pi ds18b20 temperature sensor up and running.

If you’re looking to extend this one step further, then be sure to check out my tips below. I will be looking at incorporating this sensor into future projects so stay tuned.

Possible Further Work

There are quite a few more things you’re able to do with this Raspberry Pi temperature sensor. I will quickly mention a couple of ideas, and some of them I may cover in the future.

You can have the Python script connect to a database such as MYSQL and store the data that way. If you add a timestamp to the data, you will be able to look back on data in the future and compare any changes.

This next tip would work great with the one above. You can use the data stored in the MYSQL database to make nice pretty graphs to show to temperature over the course of a day, month or even year. You could make it plot a graph in real time too.

Alternatively, using influxDB along with Grafana is a great way of visually displaying your temperature statistics.

I hope you have been able to build and get this Raspberry Pi temperature sensor working.

If you have come across any problems, have feedback or anything else then please feel free to leave a comment below.

Leave a Reply

Your email address will not be published. Required fields are marked *

34 Comments

  1. Avatar for Simon Bingham
    Simon Bingham on

    Good pages, I’m here because I’m looking to replace a legacy central heating controller with Rasberry PI. The thermal sensor that are in my system
    “DS1820” models. I wonder if this code will work for them ?
    Only one way to find out I guess.

  2. Avatar for Lars Lindehaven
    Lars Lindehaven on

    A great tutorial! I used it to get started with my own temperature project on a Raspberry Pi Zero W with Maria DB and Apache web server. See https://github.com/lindehaven/Raspberry

  3. Avatar for zeddi
    zeddi on

    how do you convert those temperature readings into a graphical format ?

  4. Avatar for Co Crocker
    Co Crocker on

    I would like to limit the output in both “C” and “F” to one decimal place. ie. 96.8.
    Can anyone help?

  5. Avatar for Edmilton (Ed) Silva
    Edmilton (Ed) Silva on

    Hi,
    That’s an outstanding tutorial! I am astonished with the simplicity you´ve gone over all stuff in an understandable way. Congratulations!

  6. Avatar for Darren Tiu
    Darren Tiu on

    Hi,
    Mine can’t seem to work the message keeps telling “modprobe: FATAL: Module w1-gpio not found” what should I do ?

  7. Avatar for Peter
    Peter on

    Thanks for posting this, great tips and tutorials

  8. Avatar for Adithya Nayabu
    Adithya Nayabu on

    I have followed the instructions, but my sensor turn too hot as soon as my circuit is turned on. Any help ?

    1. Avatar for Gus
      Gus on
      Editor

      Hi Adithya,

      Check the connections to the sensor and make sure the positive and ground leads are connected correctly.

    2. Avatar for Silverfox52z
      Silverfox52z on

      Check your wiring. If you get the wiring incorrect, the 18B20 will turn into a heating element. Reference the DS18B20 Data Sheet for the correct pin out. Be sure to take note that the 3 Pin TO-92 package is shown in the data sheet looking from the bottom.

  9. Avatar for KC
    KC on

    I got it – rocky mistake – I have used a 470K resistance instead 4.7K!!!!

  10. Avatar for KC
    KC on

    Using raspberry pi Zero with the latest software. Everything is working fine.

    I don’t see w1-slave file. the folders start with 00-… and it changes names almost every time I type ls 00*

    1. Avatar for Nils
      Nils on

      had the same problem, make sure the resistor is wired correctly

  11. Avatar for Dave Rose
    Dave Rose on

    Hi, this was a great tutorial among many I have trawled through to get the 18B20 working on the Pi. There was just one major problem. According to your circuit drawing, you have the + and – the wrong way round on the sensor, contrary to all the other tutorials for this sensor. Like this it does not work and gets too hot to touch. Rewired the correct way round it works fine now.

    I have been trying to find other programs to better use/translate the data from the sensor, like getting it to display on the 16 * 2 display. So far without success.

    Cheers.

    1. Avatar for Gus
      Gus on
      Editor

      Thanks for that Dave! I have fixed the circuit diagram, the software I use tells me the “correct” way is the “wrong” way and I trusted it since I used a pre-wired (Red, white,black) version in my tutorial. :/ All good now tho!

  12. Avatar for Ivaylo
    Ivaylo on

    Hello ,

    here is the code modification if you want to connect your temperature value to a database (you need to install library MySQLdb):

    import os
    import glob
    import time
    import MySQLdb
     
    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'
     
    conn = MySQLdb.connect(host="localhost", user="root", passwd="", db="nolinka")
    
    cursor = conn.cursor()
     
    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
    	
    while True:
    	print(read_temp())
    	mytemp = read_temp()
    	loggit = "UPDATE temperature SET Value=%s WHERE ID=1"
    	cursor.execute(loggit, (mytemp))
    	conn.commit()
    	
    	time.sleep(5)
    1. Avatar for Gus
      Gus on
      Editor

      Awesome work Ivaylo! Thanks for sharing 🙂

    2. Avatar for Edmilton (Ed) Silva
      Edmilton (Ed) Silva on

      Awsome code sharing, Ivaylo! It helped a lot on my project.
      Thanks for your great contribution!

  13. Avatar for vince
    vince on

    import os
    imprt glob
    import time
    do you run these commands?

    1. Avatar for Gus
      Gus on
      Editor

      These commands are in the script, you will need them in order for the script to run correctly.

  14. Avatar for Jerry
    Jerry on

    It works great on my raspberry pi 3 B. I’m new to the pi and to programming in python (only a Fortran class in college 40 years ago). I want to measure two temperatures, compare them, and energize a relay when the temperature differential is more than or less than desired amounts. Can you demonstrate how to add a second temperature sensor?

    1. Avatar for Gus
      Gus on
      Editor

      Hi Jerry,

      To add a 2nd sensor you should be able to wire it up almost exactly the same as the first sensor. Since it uses a one-wire interface just hook it up to the same input pin as the first sensor (pin#4 or pin #7 if using physical numbering).

      It should then appear in the steps from step 5 on wards.

      I haven’t been able to test this as I only have 1 sensor at the moment.

    2. Avatar for Mark
      Mark on

      I was able to get python to read two probes, wired in tandem with the following Python script:

      import os
      import glob
      import time
      
      os.system('modprobe w1-gpio')
      os.system('modprobe w1-therm')
      
      base_dir = '/sys/bus/w1/devices/'
      device_folder_0 = glob.glob(base_dir + '28-0120627ab4fa')[0]
      device_file_0 = device_folder_0 + '/w1_slave'
      
      def read_temp_raw_0():
          f = open(device_file_0, 'r')
          lines = f.readlines()
          f.close()
          return lines
      
      def read_temp_0():
          lines = read_temp_raw_0()
          while lines[0].strip()[-3:] != 'YES':
              time.sleep(0.2)
              lines = read_temp_raw_0()
          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_f
      
      
      base_dir = '/sys/bus/w1/devices/'
      device_folder_1 = glob.glob(base_dir + '28-0120627ed3dc')[0]
      device_file_1 = device_folder_1 + '/w1_slave'
      
      def read_temp_raw_1():
          f = open(device_file_1, 'r')
          lines = f.readlines()
          f.close()
          return lines
      
      def read_temp_1():
          lines = read_temp_raw_1()
          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_f
      
      while True:
              print(read_temp_0(),read_temp_1())
              time.sleep(1)
  15. Avatar for Big Bear
    Big Bear on

    This is a very helpful page thanks, the only thing is i am new to python and your code in line 12 gives me the error can you help me out here please
    line 12
    device_folder = glob.glob(base_dir + ’28*’)[0]
    IndexError: list index out of range

    1. Avatar for Gus
      Gus on
      Editor

      Hi Big Bear,

      This typically happens when there is no device folder. Can you confirm that a folder with a name similar to 28-000007602ffa is displaying when you run the following commands.

      cd /sys/bus/w1/devices
      ls

      If it isn’t then it’s likely the temperature sensor hasn’t been connected to the Pi correctly.

  16. Avatar for Pablo Rodriguez
    Pablo Rodriguez on

    Couldn’t get it to work on Pi 3B, so far I see that I don’t have the w1-therm module, only the w1-gpio… any ideas?

    1. Avatar for Gus
      Gus on
      Editor

      Hi Pablo,

      I will set this up on my Pi 3 and see if I get the same results.

  17. Avatar for Tshala
    Tshala on

    A brilliant tutorial. I followed it step-by-step and I got my Temperature Sensor working as expected.

    Thank you for sharing!

  18. Avatar for Cal
    Cal on

    Does not work on Pi 2 Model B. Even tried a second sensor

    Instructions very clear but when I finish step 6 the sensor does not show up, only w1_bus_master1

    Any ideas?

    1. Avatar for Tshala
      Tshala on

      Cal,
      I am using Raspberry Pi 2 model B and it works nicely.
      The only thing which comes to my mind that can cause you troubles is the connection/wiring.
      Please double check the connection/wiring and try using a different GPIO pin to see if it helps.

      Best luck!

    2. Avatar for Mikkel Holm Olsen
      Mikkel Holm Olsen on

      I have been struggling with this problem for a couple of weeks. This morning I succeeded getting it to work by using a level converter and powering the DS18B20 from 5V. This is the level converter I used: https://www.sparkfun.com/products/12009

      I bought the sensors off of Aliexpress, and I wonder if they are genuine. Maybe that is the reason they do not work reliably at 3.3V? Sensor was soldered directly to a header, so connection should be good and no wire length to cause problems.

  19. Avatar for Jim Knopick
    Jim Knopick on

    Could you show how to read 2 sensors and display the output as I would like to use the result with biofeedback.
    Thanks.

  20. Avatar for Joop Terwijn
    Joop Terwijn on

    I just starting to play with a pi, I must say nice content! I use an iPad to watch YouTube content. You mention the possibility to leave comments, well I can’t (using the iPad) the option is not showing with your content.
    I follow several channels and on other channels I can (and do) comment, so I suspect there is a mark missing while uploading 🙂