How to Read Temperature with a DS18B20 on Raspberry Pi 2
Welcome! This guide shows you how to connect a DS18B20 digital temperature sensor to a Raspberry Pi 2 and read the room temperature accurately.
What You’ll Need
- DS18B20 waterproof sensor (buy online)
- 4.7 kΩ pull‑up resistor (yellow‑violet‑red‑gold)
- Jumper wires
- Raspberry Pi 2 (any model with GPIO)
Step 1 – Wiring the Sensor
The DS18B20 uses a 1‑Wire interface. Connect it as follows:
- Red wire (VDD) → 3.3 V rail on the Pi
- Black wire (GND) → Ground rail on the Pi
- Yellow wire (DQ) → GPIO 4 (physical pin 7)
Place the 4.7 kΩ resistor between the 3.3 V line and the DQ line to act as a pull‑up. The resistor’s “3.3 V” end should touch the red (VDD) side.
Step 2 – Enable the 1‑Wire Interface
1. Edit the boot configuration file:
sudo nano /boot/config.txt
2. Add or uncomment the line:
dtoverlay=w1-gpio
3. Save and exit, then reboot:
sudo reboot
Step 3 – Load Kernel Modules
After the Pi restarts, load the 1‑Wire drivers:
sudo modprobe w1-gpio sudo modprobe w1_therm
Step 4 – Verify the Sensor is Detected
Check the device tree for the sensor ID:
ls -l /sys/bus/w1/devices/
You should see a folder beginning with 28-, e.g., 28-000006dfa76c.
Step 5 – Read the Temperature
Each sensor exposes its data through w1_slave. View it with:
cat /sys/bus/w1/devices/28-000006dfa76c/w1_slave
The output contains two lines; the second line ends with t=XXXXX, where XXXXX is the temperature in thousandths of a degree Celsius. For example, t=23450 equals 23.450 °C.
Optional – Parse the Value with a Script
Here’s a quick Python snippet to convert the raw reading into °C:
#!/usr/bin/env python3
import time, glob
sensor = glob.glob('/sys/bus/w1/devices/28-*/w1_slave')[0]
while True:
with open(sensor, 'r') as f:
lines = f.readlines()
if lines[0].strip().endswith('YES'):
temp_raw = lines[1].split('t=')[1]
temp_c = float(temp_raw) / 1000.0
print(f"Temperature: {temp_c:.3f} °C")
else:
print("Reading error")
time.sleep(2)
Save this as temp_read.py, make it executable, and run it to get live temperature updates.
By following these steps, you’ll have a reliable temperature monitor running on your Raspberry Pi 2.
For deeper insights, see the Adafruit DS18B20 guide.
Manufacturing process
- 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
- Professional Raspberry Pi Temperature Monitoring with DS18B20
- Measuring Temperature on Raspberry Pi with Maxim 1‑Wire Sensors and DS2482 I2C Bridge
- Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
- How to Connect a DS18B20 One‑Wire Digital Thermometer to a Raspberry Pi – A Step‑by‑Step Guide
- Build a ThingSpeak Temperature Monitor with Raspberry Pi & BrickPi
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Read Temperature & Humidity with DHT11 on Blynk – Step‑by‑Step Arduino Tutorial