Raspberry Pi - Temperature Sensor

This tutorial instructs you how to use Raspberry Pi to read temperature from the waterproof 1-wire DS18B20 temperature sensor. This sensor is cost-effective, simple to use and has an attractive appearance.

Hardware Preparation

1×Raspberry Pi 4 Model B
1×DS18B20 Temperature Sensor (WITH Adapter)
1×DS18B20 Temperature Sensor (WITHOUT Adapter)
1×4.7 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Block Shield for Raspberry Pi
1×(Optional) USB-C Power Cable with On/Off Switch for Raspberry Pi 4B
1×(Optional) Plastic Case and Cooling Fan for Raspberry Pi 4B
1×(Optional) HDMI Touch Screen Monitor for Raspberry Pi

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Buy Note: Many DS18B20 sensors available in the market are unreliable. We strongly recommend buying the sensor from the DIYables brand using the link provided above. We tested it, and it worked reliably.

Overview of One Wire Temperature Sensor - DS18B20

The Temperature Sensor Pinout

The DS18B20 temperature sensor has three pins:

  • GND pin: which needs to be connected to GND (0V)
  • VCC pin: which needs to be connected to VCC (5V or 3.3V)
  • DATA pin: which is the 1-wire data bus and should be connected to a digital pin on Raspberry Pi.

The sensor is typically available in two forms: TO-92 package (which resembles a transistor) and a waterproof probe. For this tutorial, we will be using the waterproof probe form.

DS18B20 temperature sensor pinout

Connecting a DS18B20 temperature sensor to a Raspberry Pi requires a pull-up resistor, which can be a bit of a hassle. Fortunately, some manufacturers simplify the process by offering a wiring adapter that includes a built-in pull-up resistor and a screw terminal block.

Wiring Diagram

  • Wiring diagram using a breadboard.
The wiring diagram between Raspberry Pi and temperature sensor

This image is created using Fritzing. Click to enlarge image

  • Wiring diagram using a the wiring adapter (recommended).
The wiring diagram between Raspberry Pi and DS18B20

This image is created using Fritzing. Click to enlarge image

To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

Raspberry Pi Screw Terminal Block Shield

We recommend buying a DS18B20 sensor along with its accompanying wiring adapter for a seamless setup. This adapter includes an integrated resistor, removing the need for an additional resistor in the wiring. We also tested it and worked well.

Raspberry Pi Code

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Prior to utilizing the DS18B20 temperature sensor with a Raspberry Pi, we need to enable 1-Wire interface on Raspberry Pi. See How to enable 1-Wire interface on Raspberry Pi
  • Install the library for DS18B20 temperature sensor by running the following command:
pip install w1thermsensor
  • Create a Python script file DS18B20.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-temperature-sensor """ from w1thermsensor import W1ThermSensor import time # Find the connected DS18B20 sensor def find_ds18b20_sensor(): for sensor in W1ThermSensor.get_available_sensors(): if sensor.type == W1ThermSensor.THERM_SENSOR_DS18B20: return sensor return None # Read temperature from the sensor def read_temperature(sensor): temperature_celsius = sensor.get_temperature() temperature_fahrenheit = sensor.get_temperature(W1ThermSensor.DEGREES_F) return temperature_celsius, temperature_fahrenheit # Find DS18B20 sensor ds18b20_sensor = find_ds18b20_sensor() if ds18b20_sensor is not None: print(f"DS18B20 Sensor found: {ds18b20_sensor.id}") try: while True: # Read temperature temperature_c, temperature_f = read_temperature(ds18b20_sensor) print(f"Temperature: {temperature_c:.2f}°C | {temperature_f:.2f}°F") # Wait for a moment before reading again time.sleep(2) except KeyboardInterrupt: print("Program terminated by user.") else: print("DS18B20 Sensor not found.")
  • Save the file and run the Python script by executing the following command in the Terminal:
python3 sensors.py
  • Place the sensor in hot and cold water, or hold it in your hand.
  • Check the output on the Terminal.
PuTTY - Raspberry Pi
Temperature: 26.31°C | 79.36°F Temperature: 26.44°C | 79.59°F Temperature: 26.50°C | 79.70°F Temperature: 26.56°C | 79.81°F Temperature: 27.06°C | 80.71°F Temperature: 27.75°C | 81.95°F Temperature: 28.37°C | 83.07°F Temperature: 29.00°C | 84.20°F Temperature: 29.56°C | 85.21°F Temperature: 30.00°C | 86.00°F Temperature: 30.31°C | 86.56°F Temperature: 30.62°C | 87.12°F Temperature: 30.87°C | 87.57°F

The script runs in an infinite loop continuously until you press Ctrl + C in the Terminal.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!