Digital Thermometer
Build a waterproof-probe thermometer and overnight temperature logger with a Raspberry Pi Pico, then test how accurate it really is.
What you’ll make
A digital thermometer with a waterproof probe that reports the temperature every few seconds, then a logger that records it to a file all night. You’ll check its accuracy with an ice bath, compare it with a trusted reference, and learn what its readings can and can’t tell you. It’s the Stage 5 challenge.
What you need
- A Raspberry Pi Pico with its header pins already fitted (Pico H or Pico WH), a data-capable USB cable, and Thonny with MicroPython (Lesson 24)
- A DS18B20 waterproof temperature probe (a sensor sealed in a steel tube on a cable, usually 1 m long)
- A 4.7 kΩ resistor (bands: yellow, violet, red; five-band: yellow, violet, black, brown, brown)
- A breadboard and jumper wires
- For testing: a glass, crushed ice and cold water, and a trusted thermometer
Safety numbers for this build
| Power | 5 V from USB; the sensor runs from the Pico’s 3.3 V |
|---|---|
| Sensor current | about 1 mA while measuring |
| Supervision | An adult nearby for the setup |
Build it
- Unplug the USB cable. Place the Pico across the breadboard’s centre gap.
- Finding the pins: with the USB connector pointing away from you, pin 1 is top left. On the right side, counting down from the USB end: 40 VBUS, 39 VSYS, 38 GND, 37 3V3_EN, 36 3V3(OUT).
- Connect the Pico’s 3V3(OUT) pin (physical pin 36) to the red (+) rail, and a GND pin (physical pin 38) to the blue (−) rail. Be careful: pin 36 is right beside 3V3_EN (pin 37), and pins 39 and 40 are VSYS and VBUS, which carry up to 5 V. Use only pin 36 for +.
- The probe has three wires. Most probes use red = +, black = − and yellow (or white) = data, but check the seller’s information, because colours vary.
- Connect red to the + rail, black to the − rail, and the data wire to GP22 (physical pin 29). Probe wires are often thin and floppy; twist each onto a jumper wire’s pin or use a screw terminal adapter.
- Plug the data jumper into the same breadboard row as pin 29, then connect the 4.7 kΩ resistor from another hole in that row to the + rail. This pull-up resistor is essential: without it the sensor may not be found, or may give errors.
- Double-check every connection, then plug in the USB cable.
Program it
The DS18B20 driver is built into MicroPython for the Pico, so there’s nothing extra to install.
from machine import Pin
import onewire, ds18x20, time
sensor = ds18x20.DS18X20(onewire.OneWire(Pin(22))) # data wire on GP22
roms = sensor.scan()
if not roms:
print("No sensor found: check the wiring and the 4.7 kilohm resistor.")
else:
print("Sensor found. Reading every 2 seconds...")
while True:
sensor.convert_temp()
time.sleep_ms(750) # wait for the reading to finish
t = sensor.read_temp(roms[0])
print("{:.1f} C".format(t))
time.sleep(2)Run it in Thonny. Hold the probe in your hand and watch the temperature rise.
Test it: the ice bath
- Pack a glass full of crushed ice, then add cold water to just below the top of the ice. Stir and wait 3 to 5 minutes.
- Keep the glass on a tray away from the Pico and the computer. Put the probe in the middle of the ice slurry, not touching the glass. Stir gently and wait a minute for the reading to settle.
- It should read close to 0 °C (32°F). The DS18B20 is specified to ±0.5 °C (0.9°F) over this range, so anything from −0.5 to 0.5 °C (31°F to 33°F) is within spec. Write down your reading: that’s your sensor’s offset at 0 °C (32°F). (Many low-cost probes use copy chips that may not meet this spec, which is one more reason to test.) Dry the probe and your hands before touching the breadboard again.
- Now compare it with a trusted thermometer in the same place indoors, side by side, after both have settled for 10 minutes. Record both readings.
Log it overnight
This version saves a reading every minute to a file named temps.csv on the Pico (Lesson 30).
from machine import Pin
import onewire, ds18x20, time, os
sensor = ds18x20.DS18X20(onewire.OneWire(Pin(22)))
roms = sensor.scan()
INTERVAL = 60 # seconds between readings
if not roms:
print("No sensor found: check the wiring.")
else:
if "temps.csv" not in os.listdir():
with open("temps.csv", "w") as f:
f.write("seconds,temp_c\n")
start = time.ticks_ms()
while True:
seconds = time.ticks_diff(time.ticks_ms(), start) // 1000
try:
sensor.convert_temp()
time.sleep_ms(750)
t = "{:.1f}".format(sensor.read_temp(roms[0]))
except Exception:
t = "" # failed reading: leave the gap blank
with open("temps.csv", "a") as f:
f.write("{},{}\n".format(seconds, t))
print(seconds, t)
time.sleep(INTERVAL - 1)Save it to the Pico as main.py so it starts by itself, write down the time you plug it in, and power it from a USB battery pack. First test the pack for an hour: many switch off when the current is as small as a Pico’s. If yours does, use one with a low-current or “always on” mode. Next day, plug it into the computer, click Stop in Thonny, and use Thonny’s Files view to download temps.csv. Open it in a spreadsheet and make a line graph. If a reading fails, the line is left with a blank temperature rather than a made-up number. Each time it starts, the seconds start again from 0, so a drop back to 0 shows where a new run begins. To start fresh, delete the old file first.
It doesn’t work?
- “No sensor found”: check the 4.7 kΩ pull-up resistor, the data wire on GP22 (pin 29), and the probe’s wire colours.
- It always reads 85.0: the reading isn’t finishing. Check that the program waits 750 ms after
convert_temp(), and check the + and − wires. - Readings jump around or the sensor sometimes vanishes: a loose probe wire. Twist the connections more firmly or use a screw terminal.
- The probe or Pico gets warm: unplug immediately: + and − are probably reversed on the probe. Check the wiring before trying again.
What your thermometer can and can’t tell you
Your thermometer is an educational instrument. It can show real patterns: how fast a drink cools, how much colder the garage is than the house, how quickly temperature drops after sunset. Its readings aren’t official observations: it isn’t calibrated against a certified standard, and outdoors its reading depends heavily on shading and placement (Lesson 29). Say how and where you measured whenever you share your results.
For teachers and parents
Open the teacher guide
Time: 45 minutes to build and test; the overnight logging runs at home or in the classroom.
Learners will: wire a digital sensor with a pull-up resistor; run and read a program; test accuracy with an ice bath; compare with a reference; log, graph and interpret data; describe measurement limitations.
Risk and supervision: low. USB power only, ice water only. Check that + goes to pin 36, not pins 39 or 40.
Common misconceptions: “more decimal places means more accurate”; “a reading in the sun is the air temperature”; “a missing reading is zero.”
Worksheet: 1. What does the 4.7 kΩ resistor do? 2. Your sensor reads 0.3 °C (33°F) in the ice bath. Is it within spec? 3. Your log shows a line with a blank temperature. What should you do with it on the graph? 4. Name two things that make an outdoor reading too warm.
Answers: 1. It’s a pull-up that holds the data line high so the sensor can signal reliably. 2. Yes: the spec is ±0.5 °C (0.9°F). 3. Leave a gap; don’t fill it with 0 or a guess. 4. Any two of: direct sun, a nearby wall or pavement, no air flow, being too close to the house.
Discussion questions: Why does the reading take a minute to settle in the ice bath? Why might two good thermometers still disagree? What would an official station do differently?
Extensions: log indoor and outdoor temperature with two probes on the same wire (each DS18B20 has its own address, found by scan()); add the frost alarm; compare the overnight low with the Lethbridge Weather live station.
Learn more
- MicroPython: Raspberry Pi Pico quick reference: official, including the DS18B20 sensor
- Environment and Climate Change Canada: measuring temperature: how official climate stations place thermometers
- Lesson 30: Data logging
- Lesson 29: Electronic weather instruments
Electronics School is part of the Discovery Zone. Every activity here runs on small batteries or a USB cable: never wall outlets. The one plug-in tool is the optional soldering iron, set up by an adult.