Built-in project · Raspberry Pi Pico W

Pico W alarm siren and lamp on a slide switch

MicroPython on a Pico W: a slide switch on GP17 turns on a siren, a passive buzzer on GP15 sweeping 130 to 800 Hz with an LED on GP16 pulsing in step. The whole build, a Raspberry Pi Pico W and 8 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $7. On Mokxi they're free.

Pico W alarm siren and lamp on a slide switch · Raspberry Pi Pico Wlive0.000 s 0.00x
Press Run, then slide the switch
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Reading a slide switch as a digital input
  • Sweeping a sound's pitch with PWM
  • Switching a speaker with a transistor
  • Fading an LED with PWM duty
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Raspberry Pi Pico W
  • Parts: 11

How it works

A slide switch connects pin GP17 to 3.3 V or to ground. A 10k resistor and a 100 nF capacitor to ground steady the signal. The program watches that pin.

When the pin is high, a siren starts. The pitch of the piezo speaker on GP15 sweeps up and down, and the red LED on GP16 brightens and dims in step. The speaker is switched by a transistor with a 1k resistor on its base.

The sweep goes up through 50 steps and back down, 10 ms each. When the switch goes to ground, the sweep stops, the sound and the LED turn off, and the program prints a message.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Resistor, 1k Ω pin 1Raspberry Pi Pico W pin GP15
Slide switch pin 2Raspberry Pi Pico W pin GP17
Resistor, 10k Ω pin 1Raspberry Pi Pico W pin GP17
Capacitor, 100 nF pin 1Raspberry Pi Pico W pin GP17
Resistor, 220 Ω pin 1Raspberry Pi Pico W pin GP16
Piezo speaker pin 13.3 V
Transistor (NPN or PNP) pin CPiezo speaker pin 2
Resistor, 1k Ω pin 2Transistor (NPN or PNP) pin B
Transistor (NPN or PNP) pin EGround
Slide switch pin 13.3 V
Slide switch pin 3Ground
Resistor, 10k Ω pin 2Ground
Capacitor, 100 nF pin 2Ground
LED, red pin AResistor, 220 Ω pin 2
LED, red pin CGround

The code

The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.

main.py · Raspberry Pi Pico W
# Alarm siren lamp: when the slide switch puts GP17 on 3.3 V, the
# buzzer on GP15 sweeps up and down like a siren and the LED on GP16
# brightens and dims in step. On ground, both stop.
from machine import Pin, PWM
import time

switch = Pin(17, Pin.IN)
buzzer = PWM(Pin(15))
lamp = PWM(Pin(16))
lamp.freq(1000)
buzzer.duty_u16(0)
on = False

while True:
    if switch.value() != on:
        on = switch.value() == 1
        print("siren on" if on else "siren off")
    if on:
        for step in list(range(0, 50)) + list(range(50, 0, -1)):
            if not switch.value():
                break
            buzzer.freq(130 + step * 14)
            buzzer.duty_u16(30000)
            lamp.duty_u16(step * 1300)
            time.sleep_ms(10)
    else:
        buzzer.duty_u16(0)
        lamp.duty_u16(0)
        time.sleep_ms(20)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The siren never starts. This project
    Check the switch's outer pins go to 3.3 V and ground, and its middle pin goes to GP17.
  • The speaker is silent but the LED works. This project
    Check the transistor: the collector goes to the speaker, the emitter to ground, and the base to GP15 through the 1k resistor.
  • A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
    A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead.
  • Sound is very quiet or the pin seems overloaded. Piezo speaker
    Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds.
  • The load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
    Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong.
  • The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
    Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.