Professional Raspberry Pi Temperature Monitoring with DS18B20
Overview
The Raspberry Pi is a compact single‑board computer (SBC) engineered by the Raspberry Pi Foundation in the UK. It runs various ARM‑based Linux distributions and exposes a set of GPIO pins that enable connection to a wide range of external components such as sensors and actuators. Because the Pi lacks an on‑board Analog‑to‑Digital Converter, analog temperature sensors like the TMP36 cannot be used directly. Instead, a digital temperature sensor such as the DS18B20 is the recommended choice for accurate, serial‑based temperature measurement.
Parts
- Raspberry Pi (any recent model)
- DS18B20 Digital Temperature Sensor
- 4.7 kΩ resistor (standard value)
- Prototyping board (breadboard or Pi‑Cobbler)
- Half‑size breadboard (optional for extended wiring)
- Jumper wires (male‑to‑male or male‑to‑female as needed)
Basic Design
Hardware Setup
The core of the setup consists of the Raspberry Pi, DS18B20, a 4.7 kΩ pull‑up resistor, a Pi‑Cobbler (or direct breadboard wiring), and jumper wires. All components are inserted into the breadboard for quick prototyping and easy debugging.
Wiring Diagram
The DS18B20 uses a single‑wire serial interface, allowing multiple sensors to share the same data line. For Raspberry Pi’s Raspbian (or derivatives), the sensor appears as a serial device once the appropriate kernel modules are loaded. The sensor must be connected to GPIO pin 4 (BCM 4), the only pin that supports the 1‑Wire protocol. A visual reference is available in Adafruit’s DS18B20 setup guide.
Choosing the DS18B20
The DS18B20 is available in three variants:
- Standard DIP package (for prototyping)
- Waterproof with an attached cable (suitable for environmental monitoring)
- High‑temperature model (up to 125 °C)
All variants include the 1‑Wire interface and temperature conversion logic, delivering digital readings directly to the Pi. Modern Raspbian images include the necessary drivers, enabling straightforward access via the file system.
Command‑Line Verification
After wiring, verify the sensor from the terminal:
sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls
cd 28-xxxxxxxxxxxx # replace with your sensor’s serial number
cat w1_slave
The output will contain a line ending with YES followed by a temperature value in thousandths of a degree Celsius on the next line.
Software – Basic Logger
The following Python script reads the temperature every second and prints the values to standard output. Save it as temp_logger_basic.py and run it with root privileges.
#!/usr/bin/env python3
import os
import glob
import time
# Ensure the 1‑Wire modules are loaded
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
BASE_DIR = '/sys/bus/w1/devices/'
DEVICE_FOLDER = glob.glob(BASE_DIR + '28*')[0]
DEVICE_FILE = f'{DEVICE_FOLDER}/w1_slave'
def read_temp_raw():
with open(DEVICE_FILE, 'r') as f:
return f.readlines()
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
Running the script will produce output similar to:
(18.875, 65.975)
(18.875, 65.975)
(18.875, 65.975)
…
Ensure the script is executed with sudo python3 temp_logger_basic.py because the sensor files reside in a protected system directory.
Advanced Design
For production use, a soldered PCB shield can replace the breadboard, providing a more robust and portable solution. I have fabricated a custom shield that plugs directly into the Pi, enclosed in a compact black case. The software logs readings hourly and stores them in a MySQL database. Data is then visualised with Google Charts, offering clear trend analysis.
Advanced Hardware
All components from the basic setup are retained, plus:
- Adafruit Raspberry Pi prototyping shield (or equivalent custom PCB)
- Optional enclosures and mounting hardware
For a detailed guide, refer to the DS18B20 temperature sensor guide and the Raspberry Pi documentation on 1‑Wire support.
Manufacturing process
- Build a Raspberry Pi Temperature Logger with a $5 I2C Sensor
- How to Read Temperature with a DS18B20 on Raspberry Pi 2
- Measuring Temperature on Raspberry Pi with Maxim 1‑Wire Sensors and DS2482 I2C Bridge
- Accurate Temperature Monitoring in a Server Closet with Raspberry Pi
- Remote Weather Monitoring with Raspberry Pi 3 and PubNub
- Build a Multi‑Sensor Temperature & Light Monitoring System with Raspberry Pi & DS18B20
- Build a Raspberry Pi Home Temperature Monitor with MCP9808, InfluxDB & Grafana
- Using an MCP3008 ADC to Read Analog Sensors with Raspberry Pi
- Smart Health Monitoring Station: Arduino, Bluetooth, and Sensors
- Smartphone-Based Temperature Monitoring System with Arduino and Bluetooth