Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsA tiny web server in about 50 lines of MicroPython. The Pico W joins your Wi-Fi, prints its address, and serves a page with two buttons that switch the relay and the on-board LED.
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.
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 W web switch: a relay you control from a browser (MicroPython)
import network
import socket
import time
from machine import Pin
SSID = "YOUR_WIFI_NAME"
PASSWORD = "YOUR_WIFI_PASSWORD"
relay = Pin(16, Pin.OUT, value=1) # most relay boards are active-LOW: 1 = off
led = Pin("LED", Pin.OUT)
def page(on):
state = "ON" if on else "OFF"
return """<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Pico W switch</title><style>body{font-family:sans-serif;text-align:center;background:#0d093d;color:#fff;padding:30px}
a{display:inline-block;margin:10px;padding:16px 28px;border-radius:12px;background:#a9c5ea;color:#0d093d;text-decoration:none;font-size:20px}</style></head>
<body><h1>Load is %s</h1><a href="/on">Turn ON</a><a href="/off">Turn OFF</a></body></html>""" % state
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(20):
if wlan.isconnected():
break
time.sleep(1)
print("Open http://%s" % wlan.ifconfig()[0])
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
server = socket.socket()
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(addr)
server.listen(2)
while True:
client, _ = server.accept()
request = client.recv(1024).decode()
if "GET /on" in request:
relay.value(0)
elif "GET /off" in request:
relay.value(1)
is_on = relay.value() == 0
led.value(is_on)
client.send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
client.send(page(is_on))
client.close()
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.
5 parts: 5V Relay Module with Optocoupler (1 channel), Raspberry Pi Pico WH – RP2040 Dev Board with Wi-Fi & Headers, DC Power Adapter 12V 2A (5.5x2.1mm), 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 beginner project and takes about 1 hour. The build on this page has 5 steps.
No. This page has the complete, tested code (main.py).
About ₹1,139 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.
Ideas that reuse this build: ESP8266 version with live status. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
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.
Logs every movement in a room with a timestamp, ready for a spreadsheet.
Learning project. Check every circuit and the datasheet of each part before you power it up.