Built-in project · ESP32-DevKitC V4

MicroPython doorbell: a button sounds a buzzer, on an ESP32-DevKitC V4

MicroPython on an ESP32: hold the button on GPIO 4 and an active buzzer, switched through an S8050 transistor from GPIO 13, sounds. 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.

Beginner About 20 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.

The circuit runs here once the page has loaded.

What you'll learn

  • Switching an active buzzer with a transistor
  • Reading a button with a pull-up
  • Detecting a change in state
  • Why a GPIO pin should not drive a load directly
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: ESP32-DevKitC V4
  • Parts: 9

How it works

The button on GPIO 4 has a 10k resistor to 3.3 V. The pin reads 1 until you press, then reads 0. An active buzzer has its own tone generator, so it only needs power. A transistor switches that power: GPIO 13 drives its base through a 1k resistor.

The program checks the button every 10 ms. While you hold it, GPIO 13 goes to 1, the transistor turns on and the buzzer sounds. Let go and it goes quiet. The code also prints "ding" when the sound starts and "quiet" when it stops, but only when the state changes.

Parts list

9 parts, plus the jumper wires. They retail for about $10.75; on Mokxi they're free.

Kits that can build it

These 3 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 pinConnects to
Resistor, 1k Ω pin 1ESP32-DevKitC V4 pin 13
Resistor, 10k Ω (2) pin 1ESP32-DevKitC V4 pin 4
Buzzer pin 15 V
Transistor (NPN or PNP) pin CBuzzer pin 2
Transistor (NPN or PNP) pin BResistor, 1k Ω pin 2
Transistor (NPN or PNP) pin EGround
Resistor, 10k Ω (1) pin 13.3 V
Pushbutton pin 1aResistor, 10k Ω (1) pin 2
Resistor, 10k Ω (2) pin 2Resistor, 10k Ω (1) pin 2
Pushbutton pin 2aGround

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 · ESP32-DevKitC V4
# Doorbell: hold the button on GPIO 4 and the active buzzer sounds. The
# buzzer is switched by an S8050 transistor from GPIO 13.
from machine import Pin
import time

buzzer = Pin(13, Pin.OUT, value=0)
button = Pin(4, Pin.IN, Pin.PULL_UP)
was = None

while True:
    ringing = button.value() == 0
    buzzer.value(1 if ringing else 0)
    if ringing != was:
        print("ding" if ringing else "quiet")
        was = ringing
    time.sleep_ms(10)

Troubleshooting

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

  • The buzzer never sounds This project
    Check the transistor wiring: GPIO 13 through the 1k resistor to the base, the buzzer to the collector, and the emitter to ground.
  • It sounds without pressing, or never stops This project
    Check the 10k resistor from the button line to 3.3 V, and that the button's other side goes to ground.
  • The buzzer makes a constant tone, or you cannot change its pitch. Buzzer
    An active buzzer has its own oscillator, so it only beeps on and off. A passive buzzer needs tone() to set pitch. Check which type you have.
  • The buzzer is silent or very quiet. Buzzer
    Check polarity: the plus mark goes to the pin or supply. For loud buzzers, drive them through a transistor instead of straight from a pin.
  • 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.