My Weather Station
Design, build, test and document your own logging weather station with a Raspberry Pi Pico.
The challenge
Design, build, test and document your own small weather station. It measures outdoor temperature and light level, can count rain with an optional tipping-bucket gauge, logs everything every 5 minutes, handles failures honestly, and is compared against a trusted reference. This is the Electronics School capstone: it uses ideas from every stage.
Important: your station is an educational instrument. Its readings can show real local patterns, but they aren’t official, calibrated observations. Say so whenever you share them.
Step 1: Write your requirements
Before building, write down what your station must do (Lesson 39). Start from this list and change it to suit your project:
- Measure air temperature in the shade every 5 minutes, to within about 0.5 °C (0.9°F) after checking. (The DS18B20 is rated ±0.5 °C (0.9°F) only from −10 to +85 °C (185°F); below −10 °C (14°F) expect up to ±2 °C (3.6°F).)
- Measure relative light level (dark to bright sun).
- Optionally, count rainfall with a tipping-bucket gauge.
- Store readings in a CSV file, leaving gaps for failed readings.
- Restart itself if the program freezes.
- Keep the electronics indoors or in a protected box; only sensors outdoors.
What you need
- A Raspberry Pi Pico with headers fitted (Pico H, Pico WH, or a Pico 2 or Pico 2 W with headers fitted), USB cable, and Thonny with MicroPython (Lesson 24)
- A DS18B20 waterproof temperature probe and a 4.7 kΩ resistor (as in the Digital Thermometer), with a cable long enough to reach outdoors
- A light-dependent resistor (LDR) and a 10 kΩ resistor
- For the optional rain gauge: a 1 kΩ resistor, a 10 kΩ resistor and a 100 nF capacitor
- Optional: a replacement tipping-bucket rain gauge with a two-wire reed switch, sold as a spare part for home weather stations
- A breadboard (later, stripboard: Lesson 37) and jumper wires
- For the shield: white plastic plant saucers, a threaded rod or bolts, nuts and spacers
- A USB power source you have tested for low-current shut-off (see the Digital Thermometer)
Safety numbers for this build
| Power | 5 V from USB; sensors run from the Pico’s 3.3 V |
|---|---|
| Outdoors | Only the sensors and their cables go outside; keep the Pico and power dry |
| Mounting | An adult handles ladders, drills and anything on a roof or fence; never mount near power lines |
| Supervision | An adult nearby for the setup and outdoor installation |
Step 2: Build and test on the bench
- Temperature: wire the DS18B20 exactly as in the Digital Thermometer: red to 3V3(OUT) (pin 36) via the + rail, black to GND (pin 38) via the − rail, data to GP22 (pin 29), and the 4.7 kΩ pull-up from the data row to the + rail. Test it with the Digital Thermometer program first.
- Light: connect the LDR from the + rail to the row of GP26 (physical pin 31), and the 10 kΩ resistor from that row to the − rail. More light lowers the LDR’s resistance and raises the voltage at GP26. On an indoor breadboard it measures window light; to measure outdoor light, put the LDR on a two-wire cable in a clear, waterproof spot outside, or note in your report what it measures.
- Rain (optional): connect one gauge wire to GP16 (physical pin 21) through a 1 kΩ resistor, and the other to a GND pin (pin 23). Add a 10 kΩ pull-up from GP16 to the + rail and a 100 nF capacitor from GP16 to the − rail (a low-pass filter, Lesson 34) so a long outdoor wire doesn’t pick up false counts. Each tip briefly closes the switch. If you have no gauge, set
RAIN_GAUGE = Falsein the program. - Run the station program below with Thonny connected. Warm the probe in your hand, cover and uncover the LDR, and tip the bucket briskly by hand. Check that each change appears in the printed output.
Step 3: The program
This program reads the sensors, logs a line every 5 minutes, leaves blanks for failed readings, counts rain tips in the background using an interrupt (a function the Pico runs the instant the switch closes), and uses the watchdog timer (Lesson 39).
from machine import Pin, ADC, WDT
import machine, onewire, ds18x20, time, os
INTERVAL = 300 # seconds between log lines (5 minutes)
RAIN_GAUGE = True # set to False if no rain gauge is connected
temp_sensor = ds18x20.DS18X20(onewire.OneWire(Pin(22))) # probe on GP22
light = ADC(26) # light sensor divider on GP26
rain = Pin(16, Pin.IN, Pin.PULL_UP) # optional rain gauge switch on GP16
tips = 0
last_tip = 0
def on_tip(pin): # runs each time the bucket tips
global tips, last_tip
now = time.ticks_ms()
d = time.ticks_diff(now, last_tip)
if d > 200 or d < 0: # ignore bounce (d < 0 after a long dry spell)
tips += 1
last_tip = now
rain.irq(trigger=Pin.IRQ_FALLING, handler=on_tip)
def read_temp():
try:
roms = temp_sensor.scan()
if not roms:
return "" # no sensor: leave a gap
temp_sensor.convert_temp()
time.sleep_ms(750)
t = temp_sensor.read_temp(roms[0])
if t == 85.0 or t < -50 or t > 50: # power-up value or impossible
return ""
return "{:.1f}".format(t)
except Exception:
return "" # failed reading: leave a gap
if "station.csv" not in os.listdir():
with open("station.csv", "w") as f:
f.write("seconds,temp_c,light_pct,rain_tips\n")
time.sleep(10) # time to press Stop in Thonny
wdt = WDT(timeout=8000) # restart if the program freezes
start = time.time()
while True:
wdt.feed()
seconds = time.time() - start
t = read_temp()
light_pct = round(light.read_u16() * 100 / 65535)
state = machine.disable_irq() # read and reset the count safely
n, tips = tips, 0
rain_txt = str(n) if RAIN_GAUGE else ""
machine.enable_irq(state)
try:
with open("station.csv", "a") as f:
f.write("{},{},{},{}\n".format(seconds, t, light_pct, rain_txt))
except OSError:
print("log write failed: is the Pico's storage full?")
print(seconds, t, light_pct, rain_txt)
for i in range(INTERVAL - 1): # wait, feeding the watchdog
time.sleep(1)
wdt.feed()main.py runs again. That’s why the program waits 10 seconds before starting the watchdog: after plugging in, click Stop during those 10 seconds to regain control. While developing, you can put a # in front of the three wdt lines to switch it off.Save it to the Pico as main.py. The seconds column restarts at 0 whenever the Pico restarts, so a drop back to 0 in the file marks a restart. The file grows by roughly 6 KB a day; download it and start a fresh one every month or so. Each rain tip is worth the amount stated in your gauge’s documentation, often about 0.28 mm (0.011 inch); multiply in the spreadsheet afterwards rather than in the program, so the raw counts are kept.
Step 4: Build a radiation shield and site it
A temperature sensor in sunlight can read many degrees too warm. Official climate stations use a white, louvred shelter (Lesson 29). A common home version is a stack of white plastic plant saucers, spaced a couple of centimetres apart on a threaded rod, so air flows through but sun and rain can’t reach the probe.
- Put the probe inside the shield, not touching the saucers.
- Mount the shield about 1.25 to 2 m above the ground, over grass if possible.
- Keep it away from walls, fences, pavement, dryer vents and air conditioners, as far as your yard allows.
- Mount a rain gauge level, in the open, away from roofs and trees.
- Run the probe cable indoors through an existing opening or a flat window pass-through cable, with an adult’s help. Don’t pinch the cable in a closing window, and don’t drill holes without permission.
Step 5: Test it against a reference
- Before installing outdoors, repeat the ice-bath test from the Digital Thermometer and record the result.
- Run the whole station on the bench for at least two days (a burn-in). Check the CSV: are there gaps? Why?
- After installing it, compare your readings with Environment and Climate Change Canada’s official observations for your area, or with the Lethbridge Weather live station, at the same times over a week. Expect differences: your yard isn’t the airport. Look for a consistent offset, and for patterns, like your station warming faster in morning sun (a sign the shield needs work).
- Record your real results, including problems. Never adjust or fill in readings to make them look better.
Step 6: Document it
Write a short design report, as an engineer would. Include:
- Your requirements, and whether the station met each one.
- A block diagram and a labelled wiring diagram.
- A power budget (Lesson 27) and how you power it.
- Each sensor’s specified accuracy and your ice-bath and comparison results.
- Photos of the site, and how it compares with official siting guidelines. Don’t post photos, maps or descriptions that show where you live.
- How it handles failures, and a fault log of any problems you found (Lesson 38).
- A maintenance plan, and what you’d improve next.
Ideas to extend it
- Add a second DS18B20 on the same wire for indoor temperature: each has its own address from
scan(). - Calculate the 3-hour temperature change in a spreadsheet and look for chinook arrivals.
- Add a wind-speed anemometer with a reed switch, counting pulses like the rain gauge.
- Move the circuit to stripboard or design a PCB (Lesson 37).
- Use a Pico W to show readings on a web page on your home network (with an adult, and never sharing your location publicly).
It doesn’t work?
- Temperature column is always blank: test the probe with the Digital Thermometer program; check the pull-up and wire colours.
- Light is always 0 or 100: check the LDR and 10 kΩ divider on GP26 (pin 31); one part may be in the wrong row.
- Rain counts when it isn’t raining: wind can rock a loose gauge; mount it firmly. Very long wires can pick up interference; keep them short or twisted together with a ground wire.
- The Pico keeps restarting: the watchdog may be timing out; make sure nothing in the loop takes longer than a few seconds.
- Gaps at night: check whether the power bank switches off, and look for condensation (Lesson 38).
For teachers and parents
Open the teacher guide
Time: a multi-week project: about 2 hours to build and bench-test, a few days of burn-in, a week of outdoor comparison, and time to write the report.
Learners will: write requirements; integrate multiple sensors with a microcontroller; use interrupts and a watchdog; site instruments using official guidance; compare data with a reference honestly; document a system.
Risk and supervision: low for the electronics (USB power only). Outdoor installation needs an adult for any ladder, tool or mounting work, away from power lines. Students should not share the station’s exact location publicly.
Common misconceptions: “my station disagrees with the airport, so it’s broken” (local differences are real and expected); “more readings are always better”; “a blank reading should be filled in.”
Assessment ideas: grade the design report against the requirements, the quality of the comparison and fault log, and honesty about limitations, not just whether the station worked perfectly.
Worksheet: 1. Why does the program wait 10 seconds before starting the watchdog? 2. Why are rain tips stored as counts instead of millimetres? 3. Your station reads 2 °C (3.6°F) warmer than the reference every sunny afternoon but matches at night. What’s the likely cause? 4. Name two things in the program that protect data integrity.
Answers: 1. So you can click Stop in Thonny before the watchdog starts. 2. So the raw data is kept, and the conversion can be corrected later if the gauge’s value per tip is different. 3. Sunlight is heating the sensor: the radiation shield or siting needs improvement. 4. Any two of: blank values for failed readings, rejecting 85.0 °C (185°F) and impossible values, opening and closing the file for each line, the watchdog restart.
🧭 Weather learning path
From wondering about the weather to measuring it yourself. Any of these makes a great science fair project: measure, keep a record, and compare with a real station.
- 🌪 Weather Wonders
Chinooks, hail, tornadoes and more: what the weather can do - ✂ Make simple instruments
A pinwheel, wind vane and pop bottle rain gauge - 🔬 Hands-On Weather Science
Experiments to try at home and outside - 📡 Build your own weather station (you are here)
A real electronics science project - 🏫 Weather School: Observing the Weather
How real weather observers measure - 🌳 Forecaster Track
Read real weather maps and make your own forecast - 💻 Weather software
The programs behind a real station and forecast - 📈 Compare with a real station
Live readings from West Lethbridge Weather
Learn more
- Environment and Climate Change Canada: measuring temperature: how official climate stations place thermometers
- MicroPython: Raspberry Pi Pico quick reference: official, including the DS18B20 sensor
- Lesson 39: System design
- Lesson 29: Electronic weather instruments
- Hands-On Weather Science
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.