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.
Pins
| Pin | What it is | Type |
|---|---|---|
| GND | Ground (flat face, left) | ground |
| DQ | Data, 1-Wire (middle) | io |
| VDD | Supply 3.0–5.5 V (right) | power |
| Setting | Type | Range | Default |
|---|---|---|---|
resolutionBits | enum | 9, 10, 11, 12 | 12 |
In circuit.py:
DS18B20()— DS18B20 thermometer — $4- Pins: 3
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
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.GNDOn 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
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=.. NOwhen 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_slavecontents 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 seesNOnow 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-…/temperatureshortcut and thenamefile exist too; only/sys/class/thermal/thermal_zone0/tempis the CPU, not a probe.
Going deeper
- Primer: I²C, SPI, UART and 1-Wire — what makes 1-Wire different.
- The pull-up: resistor; the ready-made version: DS18B20 module.
- Recipe: the thermostat reads one and switches a relay.
- A cousin with a protocol of its own: DHT22 temp & humidity.
Further reading
- DS18B20 datasheet (Maxim/Analog Devices) — commands, the scratchpad and the CRC.
- Adafruit: DS18B20 with a Raspberry Pi — enabling 1-Wire and reading sysfs on a bench.