How to Connect a DS1820 One‑Wire Temperature Sensor to a Raspberry Pi Using GPIO
Monitoring indoor or outdoor temperature with a Raspberry Pi can be done with a minimal setup. This guide shows how to wire a DS18S20 (DS1820) 1‑Wire sensor directly to a GPIO pin, read temperatures in software, and log the data with RRDtool.
Raspberry Pi and 1‑Wire
The DS18S20, DS18B20, and DS1822 are all 1‑Wire temperature sensors that fit in a TO‑92 package and combine a sensor, ADC, and serial interface. While they share the same pinout and software interface, they differ in cost and accuracy – the DS18S20 offers ±0.5 °C accuracy, the DS18B20 ±0.5 °C, and the DS1822 ±0.5 °C as well, but the latter is cheaper.
Each device has three pins: Ground (GND, pin 1), Data (DQ, pin 2), and Power (VDD, pin 3). A parasitic power supply (3–5 V) allows the sensor to be powered solely from the data line using a pull‑up resistor.
Only One Resistor
Connect the sensor’s DQ pin to GPIO 4 on the Pi. Tie GND to the Pi’s ground and VDD to GND, enabling parasitic power. Place a 4.7 kΩ pull‑up between 3.3 V and GPIO 4. The small breadboard or a right‑angle header keeps the wiring tidy and fits within the Pi’s official case.
1‑Wire Software Drivers
On Raspbian (or any Debian‑based distro) load the kernel modules with:
sudo modprobe w1-gpio pullup=1 sudo modprobe w1-therm
The pullup=1 flag tells the driver that the sensor is powered through the pull‑up resistor. After loading, each sensor appears as a subdirectory under /sys/bus/w1/devices. The directory name consists of the family code and the sensor’s unique 64‑bit identifier: DS18S20/DS1820 use family code 10, DS18B20 uses 28, and DS1822 uses 22.
Inside each directory is w1_slave, which contains two lines. The first shows the raw register dump and a CRC check. The second ends with t=xxxx, the temperature in millidegrees Celsius. Example:
cd /sys/bus/w1/devices cd 10-000801b5* cat w1_slave 0f 00 4b 46 ff ff 06 10 0c : crc=0c YES 0f 00 4b 46 ff ff 06 10 0c t=7375
The value 7375 corresponds to 7.375 °C. Note that the datasheet specifies an accuracy of ±0.5 °C, so the true temperature is within ±0.5 °C of the reading.
To ensure the modules load at boot, add them to /etc/modules:
# /etc/modules w1-gpio pullup=1 w1-therm
Round‑Robin Database (RRDtool)
For long‑term storage and visualisation, RRDtool is ideal. Install it with:
sudo apt-get install rrdtool python-rrdtool
Define a database that records a value every 15 minutes and keeps daily aggregates for ten years:
rrdtool create temperature.rrd --step 900 \ DS:temp0:GAUGE:1200:-40:80 \ DS:temp1:GAUGE:1200:-40:80 \ RRA:AVERAGE:0.5:1:960 \ RRA:MIN:0.5:96:3600 \ RRA:MAX:0.5:96:3600 \ RRA:AVERAGE:0.5:96:3600
Data Acquisition with Python
Below is a minimal Python script that reads w1_slave and inserts the value into the RRDtool database. Adjust the sensor paths and database file name as needed.
import os
import rrdtool
W1_PATH = '/sys/bus/w1/devices'
DB_FILE = 'temperature.rrd'
def read_temp(sensor_id):
with open(f'{W1_PATH}/{sensor_id}/w1_slave') as f:
lines = f.readlines()
if lines[1].strip().endswith('YES'):
temp_milli = int(lines[1].strip().split('t=')[-1])
return temp_milli / 1000.0
return None
# Example for two sensors
temps = {
'temp0': read_temp('10-000801b5'),
'temp1': read_temp('28-0000045e')
}
rrdtool.update(DB_FILE, ','.join(str(t) for t in temps.values()))
Run the script periodically (e.g., via cron) to keep the database updated.
Manufacturing process
- 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
- TMP006 Infrared Temperature Sensor with Raspberry Pi: Python Library & Setup Guide
- Build a Remote Temperature Sensor with Raspberry Pi and Python – Step‑by‑Step Guide
- Connecting HC‑SR04 Ultrasonic Sensor to Raspberry Pi 3 – A Complete Guide
- How to Connect a DS18B20 One‑Wire Digital Thermometer to a Raspberry Pi – A Step‑by‑Step Guide
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Master Raspberry Pi GPIO: Interfacing a PIR Motion Sensor on B+/Model 2
- How to Connect, Calibrate, and Program the HC‑SR501 PIR Motion Sensor with a Raspberry Pi