Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsPlants suffer when a small greenhouse bakes in the afternoon or turns muggy and invites mould. The DHT22 reports the air, the relay switches a 12 V fan, and a gap between the on and off levels stops the relay chattering.
Switch only a low-voltage 12 V fan with the relay. Mains fans need an electrician and a proper contactor.
Keep to low-voltage loads unless a qualified electrician wires the mains side in an earthed, closed enclosure.
Tick anything you already have and the total updates.
Measures air temperature (±0.5 °C) and relative humidity (±2–5 %) on a single data wire.
An electrically isolated switch that lets a 3.3/5 V pin turn a 12 V (or mains) load on and off.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico greenhouse fan controller with a DHT22 (MicroPython)
from machine import Pin
import dht, time
sensor = dht.DHT22(Pin(15))
relay = Pin(14, Pin.OUT)
RELAY_ON = 0 # most relay modules switch on when the pin is LOW: change to 1 if yours is different
relay.value(1 - RELAY_ON)
ON_C, OFF_C = 32, 29 # fan on above 32 C, off below 29 C
ON_H = 85 # or on when humidity goes over 85 percent
fan_on = False
while True:
try:
sensor.measure()
t, h = sensor.temperature(), sensor.humidity()
except OSError:
print("sensor error")
time.sleep(2)
continue
if not fan_on and (t > ON_C or h > ON_H):
fan_on = True
elif fan_on and t < OFF_C and h < ON_H - 5:
fan_on = False
relay.value(RELAY_ON if fan_on else 1 - RELAY_ON)
print("%.1f C %d %% fan %s" % (t, h, "ON" if fan_on else "off"))
time.sleep(5)
Check the DATA wire and the pull-up. The DHT22 needs at least 2 seconds between reads, so do not poll it faster.
Load must be on COM and NO, with its own supply in series.
Your module may be active-HIGH. Set RELAY_ACTIVE_LOW to false.
Some 5 V relay boards do not fully switch off with 3.3 V logic. Remove the JD-VCC jumper, feed JD-VCC from 5 V and VCC from 3.3 V, or use a relay board made for 3.3 V input.
The relay is rated for mains, but mains wiring is dangerous. Keep this build on low-voltage loads (12 V pumps, fans, LED strips), or have a qualified electrician fit the relay inside an earthed, closed enclosure.
6 parts: DHT22 Temperature & Humidity Sensor, 5V Relay Module with Optocoupler (1 channel), Hobby DC Motor (130 size), Raspberry Pi Pico, Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 45 minutes. The build on this page has 4 steps.
No. This page has the complete, tested code (main.py).
About ₹717 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Isolation, relays and switching higher-power loads; Single-wire digital sensors and why some sensors need slow polling.
Ideas that reuse this build: Polyhouse fan and pad controller and Temperature fan controller. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.