PyRaspBeta

DS18B20 thermometer

The DS18B20 is a digital thermometer in a TO-92 package for $4. It reports −55 to +125 °C with up to 0.0625 °C resolution over Maxim's 1-Wire bus, and every chip carries a unique 64-bit ROM code, so several probes can share one wire. It needs only a 4.7 kΩ pull-up, and the Raspberry Pi's kernel driver publishes each probe under /sys/bus/w1/devices — no Python library required. The DS18B20 module kit is the probe with its pull-up fitted.

DS18B20 thermometer

Pins

PinWhat it isType
GNDGround (flat face, left)ground
DQData, 1-Wire (middle)io
VDDSupply 3.0–5.5 V (right)power
SettingTypeRangeDefault
resolutionBitsenum9, 10, 11, 1212

In circuit.py:

Wired in the flat face towards you, the pins run GND, DQ, VDD. The resolutionBits setting picks 9–12 bits: fewer bits give the same accuracy but a shorter conversion, useful when you read often.

Wiring it in circuit.py

circuit.py
from wiring import *

pi = Pi()

probe = DS18B20()

pi.V3_3 >> probe.vdd
probe.dq >> pi.GPIO4                 # the kernel's default 1-Wire pin
pi.V3_3 >> Resistor(4700) >> probe.dq
probe.gnd >> pi.GND

On a bench you would first add dtoverlay=w1-gpio to /boot/config.txt (or raspi-config) to enable the 1-Wire master on GPIO4. In the simulator the bus is already up. The pull-up can go from DQ to either VDD or 3V3; 4.7 kΩ is the value the bus expects.

Driving it in main.py

main.py
import glob
from time import sleep

probe = glob.glob("/sys/bus/w1/devices/28-*/w1_slave")[0]

while True:
    lines = open(probe).read().splitlines()
    if lines[0].endswith("YES"):
        celsius = int(lines[1].split("t=")[1]) / 1000
        print(f"{celsius:.2f} C")
    else:
        print("CRC error, reading again")
    sleep(1)

w1_slave holds the 9-byte scratchpad, the CRC verdict, and the temperature in millidegrees. Opening the file takes the sensor's conversion time — 750 ms at the default 12 bits — so each pass through the loop blocks that long. Always check the YES before trusting the number: a bad CRC (crc=.. NO) means read again.

Electrical notes

  • Supply is 3.0–5.5 V; conversion draws about 1.5 mA and standby 1 µA. It can also be powered parasitically from the data line, but the simulator models the normal three-wire wiring.
  • Conversion time scales with resolution: 93.75 ms (9 bit), 187.5 ms (10), 375 ms (11) and 750 ms (12).
  • Resolution is 0.0625 °C at 12 bits; accuracy is ±0.5 °C only between −10 and +85 °C.
  • The scratchpad read is protected by a Dallas CRC-8. The kernel prints crc=.. NO when the checksum fails.
  • After power-on, before the first conversion, the scratchpad holds 85 °C (0x0550) — a classic "sensor stuck at 85" gotcha on real hardware.

Simulator vs real hardware

  • The ROM code (28-…) is drawn from the part's seed: each unit gets a stable, unique-looking address for the run, so the glob-based code above works with several probes on the bus.
  • The w1_slave contents are generated on open, and the read genuinely takes the conversion time shown above. A CRC error knob (1 % by default) occasionally flips the checksum, so robust code sees NO now and then just like on a Pi.
  • The temperature comes from the feed for the probe's zone. If the feed has no value the model returns the 0x0550 word — 85 °C — exactly like an unconverted part.
  • The kernel's /sys/bus/w1/devices/28-…/temperature shortcut and the name file exist too; only /sys/class/thermal/thermal_zone0/temp is the CPU, not a probe.

Going deeper

Further reading