Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsFlask turns the Pi into a tiny web server. The page shows whether the load is on and has one button that toggles the relay. Open the Pi's address with port 8080 on any phone at home. It is the simplest way to learn how web apps control hardware.
Switch only low-voltage loads. Mains wiring needs an electrician and a proper enclosure.
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.
web_switch.py# Pi web switch: a phone page that turns a low-voltage load on and off through a relay
from flask import Flask, redirect
from gpiozero import OutputDevice
relay = OutputDevice(17, active_high=False, initial_value=False) # most relay boards switch on with a LOW signal
app = Flask(__name__)
PAGE = """<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1"><title>Switch</title></head>
<body style="font-family:sans-serif;text-align:center;padding-top:60px;background:#0d093d;color:#fff">
<p style="font-size:28px">Lamp is {state}</p>
<form action="/toggle" method="post"><button style="font-size:32px;padding:20px 50px;border-radius:12px">Switch {other}</button></form></body></html>"""
@app.route("/")
def home():
on = relay.value == 1
return PAGE.format(state="ON" if on else "OFF", other="OFF" if on else "ON")
@app.route("/toggle", methods=["POST"])
def toggle():
relay.toggle()
return redirect("/")
app.run(host="0.0.0.0", port=8080)
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 Zero 2 W, Raspberry Pi Micro USB 5.1V 2.5A Power Supply, Official Raspberry Pi 32GB A2 Class Micro SD Card 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 1–2 hours. The build on this page has 4 steps.
No. This page has the complete, tested code (web_switch.py). Install this library first: Flask (sudo apt install python3-flask).
About ₹3,910 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: Pico W web switch and Wi-Fi relay switch. 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.