The DS18B20 is the go-to sensor for water tanks, aquariums and freezers: sealed, accurate to ±0.5 °C, and many of them share one GPIO. MicroPython for the Pico W ships the onewire and ds18x20 drivers, so this is a single short file.
Wire the probe to 3V3, GP22 and GND — three neighbouring pins on the right-hand header — with a 4.7 kΩ resistor from DQ to 3V3.
Wiring
| Raspberry Pi Pico W | DS18B20 (1-Wire) | Wire | Notes |
|---|---|---|---|
| 3V3 | VDD | red | Red wire on the probe. 3V3 is physical pin 36. |
| GP22 | DQ | yellow | Yellow (or white) wire. GP22 is physical pin 29. |
| 3V3 | DQ | red | Via 4.7 kΩ — The 1-Wire bus needs this pull-up — waterproof probes too. Some breakout boards already have it. |
| GND | GND | black | Black wire on the probe. GND pin 28 sits right next to GP22. |
3.0–5.5 V part, 3.3 V board. Power it from your board's logic voltage (3.3 V on ESP32/Pico, 5 V on Uno) so DQ never goes above the GPIO limit. DQ needs a 4.7 kΩ pull-up to that same supply.
Board quirks
onewireandds18x20are frozen into MicroPython for the Pico W — nothing to copy to the board.- Several probes can hang on the same three wires;
scan()finds each one by its 64-bit ROM code, printed at start-up so you can label them. - Use 3V3, not VBUS: with a 5 V supply the pull-up would put 5 V on GP22, and the Pico’s pins are not 5 V tolerant.
- For long runs (water tank, compost pit) use 3-core cable and drop the pull-up to 2.2 kΩ beyond about 5 m.
Code
# DS18B20 temperature probe(s) on a Raspberry Pi Pico W (MicroPython, built-in onewire + ds18x20).
# Wiring: VDD (red) -> 3V3 (pin 36), DQ (yellow) -> GP22 (pin 29), GND (black) -> GND,
# plus a 4.7 kOhm pull-up from DQ to 3V3. Several probes can share the same three wires.
from machine import Pin
import onewire
import ds18x20
import time
bus = ds18x20.DS18X20(onewire.OneWire(Pin(22)))
roms = bus.scan()
print("Found {} DS18B20 sensor(s)".format(len(roms)))
for rom in roms:
print(" ROM", rom.hex())
while True:
if not roms:
print("No sensor - check the 4.7 kOhm pull-up and the DQ wire on GP22")
time.sleep(5)
roms = bus.scan()
continue
bus.convert_temp()
time.sleep_ms(750) # 12-bit conversion time
for i, rom in enumerate(roms):
t = bus.read_temp(rom)
if t == 85.0:
print("Sensor {}: 85.0 C = power-on value, conversion not finished".format(i))
else:
print("Sensor {}: {:.2f} C".format(i, t))
time.sleep(2)# DS18B20 temperature probe(s) on a Raspberry Pi Pico W (MicroPython, built-in onewire + ds18x20).
# Wiring: VDD (red) -> 3V3 (pin 36), DQ (yellow) -> GP22 (pin 29), GND (black) -> GND,
# plus a 4.7 kOhm pull-up from DQ to 3V3. Several probes can share the same three wires.
from machine import Pin
import onewire
import ds18x20
import time
bus = ds18x20.DS18X20(onewire.OneWire(Pin(22)))
roms = bus.scan()
print("Found {} DS18B20 sensor(s)".format(len(roms)))
for rom in roms:
print(" ROM", rom.hex())
while True:
if not roms:
print("No sensor - check the 4.7 kOhm pull-up and the DQ wire on GP22")
time.sleep(5)
roms = bus.scan()
continue
bus.convert_temp()
time.sleep_ms(750) # 12-bit conversion time
for i, rom in enumerate(roms):
t = bus.read_temp(rom)
if t == 85.0:
print("Sensor {}: 85.0 C = power-on value, conversion not finished".format(i))
else:
print("Sensor {}: {:.2f} C".format(i, t))
time.sleep(2)Expected serial output
Found 2 DS18B20 sensor(s)
ROM 28ff6a1d61160452
ROM 28ff3c7a6016043e
Sensor 0: 27.44 C
Sensor 1: 18.06 CFound 2 DS18B20 sensor(s) ROM 28ff6a1d61160452 ROM 28ff3c7a6016043e Sensor 0: 27.44 C Sensor 1: 18.06 C
Common errors
Found 0 DS18B20 sensor(s)
No pull-up, or DQ is on the wrong pin. Check the 4.7 kΩ from DQ to 3V3 and that the yellow wire goes to GP22 (physical pin 29).
Reads 85.0 C
85 °C is the power-on value: the conversion did not finish. Keep the 750 ms wait after convert_temp() and check the supply.
onewire.OneWireError
The bus is shorted or a probe dropped off mid-read — usually a loose screw terminal or water in a cheap probe’s joint.