How to Build a Raspberry Pi Temperature Sensor

Note: If you are just unpacking your Raspberry Pi, check out my previous post on how to get up and running via a headless setup (no external keyboard or mouse required).

In this post, I'll show how you can build a temperature monitor using a Raspberry Pi and the DS18B20 one-wire digital sensor. The project build time is about 5 - 10 minutes. We will start off by putting together the actual circuit, enable the modules via SSH and finally get our temperature readings with some Python code. When you're all said and done, you should end up with something similar to that below.


1. Requirements

Screen Shot 2018-03-10 at 12.20.51 pm.png

2. The Circuit

  1. Connect a Female-to-Male Jumper Wire (red) from the 3.3V to the +ve rail.
  2. Connect a Female-to-Male Jumper Wire (yellow) from the GND to the -ve rail.
  3. Connect a Female-to-Male Jumper Wire (green) from GPIO4 to Pin 15F.
  4. Connect a Male-to-Male Jumper Wire (red) from the +ve rail to Pin 14F.
  5. Connect a Male-to-Male Jumper Wire (yellow) from the -ve rail to Pin 13F.
  6. Connect the 4.7k Ω resistor to pins 14H and 15H (i.e. between +ve and data).
  7. Connect the DS18B20 sensor to the breakout board:
    1. Yellow (GND) to Pin 13J.
    2. Red (VCC) to Pin 14J.
    3. Green (DATA) to Pin 15J.


  • The colours on the output leads of your DS18B20 may vary. For example, another version of the sensor might have Black (GND), Red (VCC) and Green (DATA). 
  • The colours of the jumper wires is not important. I have simply remained consistent with the colours of the output leads from the DS18B20 to make the circuit diagram easy to follow.

3. Add Support for the DS18B20

Now that our circuit is wired up, plug in your micro USB cable to power up the Raspberry Pi so we can execute the following commands via SSH.

1. Open the boot config file.

sudo nano /boot/config.txt

2. Navigate to the bottom of the file and enter the following. Once done, to save and exit press: Ctrl+X, Y, Enter.


3. Reboot.

sudo reboot

4. Test the Temperature Sensor

Once the Raspberry Pi is back online, SSH back in and execute the following commands.

1. Enable the probe.

sudo modprobe w1-gpio
sudo modprobe w1-therm

2. Change directories.

cd /sys/bus/w1/devices

3. Type ls to list the contents of the directory. You should find a folder similar to mine (28-011620f167ee), this is your device ID.

Screen Shot 2018-03-10 at 12.10.52 pm.png

4. Change into this directory. Note: Your device ID will differ.

cd 28-011620f167ee

5. Run the following command.

cat w1_slave

If successful you should see output similar to what I have below. The second line contains our temperature t=22937 (i.e. 22.937° Celsius).

    Screen Shot 2018-03-10 at 12.13.52 pm.png

    5. Programmatic Access via Python

    Below is some sample code you can copy and paste to programatically get temperature readings and output to console. Note: You will need to update the code for your unique device ID.

    import os
    import time
    os.system('modprobe w1-gpio')
    os.system('modprobe w1-therm')
    base_dir = '/sys/bus/w1/devices/'
    device_id = 'YOUR_DEVICE_ID'
    device_file = base_dir + device_id + '/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
            return temp_c
    while True:
            print read_temp()

    6. Video

    That's it! In a future post I will be looking to bring all this together to explore some of IoT capabilities within Azure, so stay tuned...