How to Connect a DS18B20 One‑Wire Digital Thermometer to a Raspberry Pi – A Step‑by‑Step Guide
While searching for a simple way to measure temperature with a Raspberry Pi, I discovered the DS18B20 1‑wire digital temperature sensor. Its accuracy—±0.5 °C at 0 °C—and minimal wiring make it ideal for beginners and seasoned makers alike.
The sensor is inexpensive, requires only a single 4.7 kΩ pull‑up resistor, and fits easily into a breadboard or a TO‑92 package that resembles common transistors.
Below is a quick wiring diagram and the steps to get your Pi talking to the DS18B20.
Pinout (TO‑92)
- Pin 1: GND
- Pin 2: DQ (data)
- Pin 3: VDD (3.3 V)
Connect the sensor to the Pi as follows:
- Pin 1 → P1‑06 (Ground)
- Pin 2 → P1‑07 (GPIO 4)
- Pin 3 → P1‑01 (3.3 V)
A 4.7 kΩ resistor must be placed between the data line (Pin 2) and the 3.3 V supply (Pin 3). If you don’t have a 4.7 kΩ resistor, two 2.2 kΩ resistors in series will work fine.
After wiring, power up the Pi. It’s a good idea to use a fresh SD card with the latest Raspberry Pi OS (formerly Raspbian). Download it from the official Raspberry Pi Software page.
Keep the system updated with:
sudo apt-get update && sudo apt-get upgrade
Next, enable the 1‑Wire interface by editing /boot/config.txt:
sudo nano /boot/config.txt
Add the following line at the end:
dtoverlay=w1-gpio,gpiopin=4
Save the file (Ctrl + X, Y, Enter) and reboot:
sudo reboot
Once the Pi restarts, the DS18B20 will appear in the /sys/bus/w1/devices directory. To read the temperature, use:
cat /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave
Replace 28-xxxxxxxxxxxx with the sensor’s unique ID.
For detailed documentation, refer to the DS18B20 datasheet and the official Raspberry Pi GPIO guide.
Manufacturing process
- The Thermometer: From Galileo’s Thermoscope to Modern Digital Sensors
- DS18B20 Temperature Sensor – Precise 1‑Wire Digital Thermometer for Industrial & Consumer Use
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- Connect Multiple DS18B20 1‑Wire Sensors to a Raspberry Pi for Accurate Temperature Monitoring
- How to Read Temperature with a DS18B20 on Raspberry Pi 2
- Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
- Reading a Linear Hall Sensor with Raspberry Pi: Java Guide & ADC0832 Insights
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- DS18B20 1-Wire Digital Temperature Sensor: Pinout, Key Features & Practical Applications