MicroPython alarm siren on a passive buzzer, on an ESP32-DevKitC V4
MicroPython on an ESP32: while the button on GPIO 4 is held, a passive buzzer behind an S8050 on GPIO 13 wails between 1500 and 2500 Hz. The whole build, an ESP32-DevKitC V4 and 6 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 $10.75. On Mokxi they're free.
What you'll learn
- Passive buzzers need a frequency
- Switching a load with a transistor
- PWM for sound
- Changing pitch smoothly with a sine wave
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: ESP32-DevKitC V4
- Parts: 9
How it works
A passive buzzer makes sound only when it is fed a changing signal. Here GPIO 13 sends a square wave through a 1k resistor to the base of a transistor. The transistor switches the buzzer, which sits between the 5 V supply and the transistor's collector.
The button on GPIO 4 has a 10k resistor to 3.3 V, so the pin reads 1 until you press it. While you hold the button, the program sweeps the pitch up and down around 2000 Hz in a smooth wave, about 500 Hz either way. That makes the siren sound. Let go and the sound stops.
Parts list
9 parts, plus the jumper wires. They retail for about $10.75; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Resistor, 1k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Transistor (NPN or PNP)In LED Cube Kit.
- 2 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 1 × Power, 5 V
- 1 × Power, 3.3 V
Kits that can build it
These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.
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 pin | Connects to |
|---|---|
| Resistor, 1k Ω pin 1 | ESP32-DevKitC V4 pin 13 |
| Resistor, 10k Ω (2) pin 1 | ESP32-DevKitC V4 pin 4 |
| Piezo speaker pin 1 | 5 V |
| Transistor (NPN or PNP) pin C | Piezo speaker pin 2 |
| Transistor (NPN or PNP) pin B | Resistor, 1k Ω pin 2 |
| Transistor (NPN or PNP) pin E | Ground |
| Resistor, 10k Ω (1) pin 1 | 3.3 V |
| Pushbutton pin 1a | Resistor, 10k Ω (1) pin 2 |
| Resistor, 10k Ω (2) pin 2 | Resistor, 10k Ω (1) pin 2 |
| Pushbutton pin 2a | Ground |
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.
# Alarm: while the button on GPIO 4 is held, the passive buzzer on
# GPIO 13 wails. Its pitch follows a sine wave around 2000 Hz.
from machine import Pin, PWM
import math
import time
button = Pin(4, Pin.IN, Pin.PULL_UP)
buzzer = PWM(Pin(13), freq=2000, duty=0)
angle = 0
while True:
if button.value() == 0:
angle = (angle + 10) % 360
buzzer.freq(2000 + int(500 * math.sin(math.radians(angle))))
buzzer.duty(512)
else:
buzzer.duty(0)
time.sleep_ms(10)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Silence when the button is held This project
Check the transistor's pins: base to the 1k resistor, collector to the buzzer, emitter to ground. - A steady tone instead of a siren This project
Make sure this is a passive buzzer. The pitch comes from the program. - 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 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. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.