Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThe sound module's microphone output is measured over a 20 ms window and a jump above a threshold is a clap. The program waits for a second clap inside 0.9 second before toggling the lamp, which keeps it from reacting to speech or a door slam.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico clap switch: two claps toggle a lamp relay or LED (MicroPython)
from machine import Pin, ADC
import time
mic = ADC(26)
lamp = Pin(15, Pin.OUT)
THRESHOLD = 6000 # loudness jump that counts as a clap
WINDOW_MS = 900 # second clap must come within this time
def loud():
lo, hi = 65535, 0
end = time.ticks_add(time.ticks_ms(), 20)
while time.ticks_diff(end, time.ticks_ms()) > 0:
v = mic.read_u16()
lo, hi = min(lo, v), max(hi, v)
return hi - lo
first = None
while True:
if loud() > THRESHOLD:
now = time.ticks_ms()
if first is None:
first = now
elif time.ticks_diff(now, first) > 150: # ignore the echo of the same clap
if time.ticks_diff(now, first) < WINDOW_MS:
lamp.toggle()
print("lamp", lamp.value())
first = None
time.sleep_ms(120)
if first is not None and time.ticks_diff(time.ticks_ms(), first) > WINDOW_MS:
first = None
Turn the module trimmer to increase gain, or move the mic closer to the sound.
LED is reversed; flip it. Check the 220 Ω resistor is in series.
6 parts: Analog Sound Sensor Microphone Module, Through-Hole LED Pack (Pack of 10), Resistor Assortment Kit (30 Values), 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 beginner 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 ₹618 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.
Current-limiting resistors for LEDs; Sampling a waveform and measuring amplitude.
Ideas that reuse this build: Clap-controlled robot and Pico sound level ring. 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.