Learn how to connect a DS18B20 temperature sensor to your Raspberry Pi and read the temperature data using Python.

This article will guide you through connecting a DS18B20 temperature sensor to your Raspberry Pi and reading the temperature data using Python programming language in 5 simple steps. DS18B20 is a popu …


Updated September 15, 2023

Need help with your Raspberry Pi?
Contact Me!

Do you love silly Raspberry Pi Projects?
Check out my this YouTube Channel!


This article will guide you through connecting a DS18B20 temperature sensor to your Raspberry Pi and reading the temperature data using Python programming language in 5 simple steps. DS18B20 is a popular digital temperature sensor that can measure temperatures from -55°C to +125°C with an accuracy of ±0.5°C.

  1. Wiring Before connecting the DS18B20 to your Raspberry Pi, you will need a few components:

    • DS18B20 temperature sensor
    • Breadboard
    • Jumper wires (male-to-male and male-to-female)
    • Resistor (4.7kΩ)
    • Power supply (3.3V or 5V)

    To connect the DS18B20 to your Raspberry Pi, follow these steps:

    • Connect the Vcc pin of the sensor to a 3.3V or 5V power supply (depending on the sensor model you have).
    • Connect the GND pin of the sensor to the ground pin of the Raspberry Pi using a jumper wire.
    • Connect the data pin of the sensor (DQ) to GPIO4 (pin 7) of the Raspberry Pi using a female-to-male jumper wire.
    • Place a 4.7kΩ resistor between the Vcc and DQ pins of the sensor. The other end of the resistor should be connected to the ground pin of the Raspberry Pi.
  2. Activate 1-Wire Protocol To use the DS18B20, you need to enable the 1-Wire protocol on your Raspberry Pi. Open the config.txt file located in the boot partition of your SD card and add the following line at the end:

    dtoverlay=w1-gpio,gpiopin=4
    

    Save the changes and reboot your Raspberry Pi.

  3. Installing Required Packages To read temperature data from the DS18B20 sensor, you will need to install some packages on your Raspberry Pi. Open a terminal window and run:

    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install python3-pip
    sudo pip3 install w1thermsensor
    
  4. Reading Temperature Data Now that everything is set up, you can read temperature data from the DS18B20 using Python. Create a new Python file (e.g., read_temp.py) and add the following code:

    import w1thermsensor
    
    sensor = w1thermsensor.W1ThermSensor()
    temp_c = sensor.get_temperature()
    print(f"Temperature: {temp_c} °C")
    

    Save the file and run it using python3 read_temp.py. You should see the current temperature in Celsius displayed on your screen.

  5. Additional Functionality If you want to add more functionality, such as logging the temperature data or sending an alert when a certain threshold is reached, you can modify the Python script accordingly. For example, to log the temperature data every minute:

    import time
    import w1thermsensor
    
    while True:
        sensor = w1thermsensor.W1ThermSensor()
        temp_c = sensor.get_temperature()
        print(f"Temperature: {temp_c} °C")
    
        with open("temperature_log.txt", "a") as logfile:
            logfile.write(f"{time.strftime('%Y-%m-%d %H:%M:%S')}: {temp_c} °C\n")
    
        time.sleep(60)  # wait for 1 minute before reading the temperature again
    

    This script will write the temperature data to a file called temperature_log.txt in your current directory every minute. You can adjust the time interval as needed by changing the value passed to the time.sleep() function.

Now you have successfully connected a DS18B20 temperature sensor to your Raspberry Pi and read the temperature data using Python! Feel free to customize the script for your specific needs or explore other ways to use this sensor with your Raspberry Pi.