How to Control a Buzzer (Analog Output)
Ready to make some noise? Let’s learn how to control a buzzer with your tinyCore! This tutorial will teach you about PWM (Pulse Width Modulation) - a super important concept that lets you control not just buzzers, but motors, LED brightness, and tons of other analog devices.
What is PWM?
Section titled “What is PWM?”PWM stands for “Pulse Width Modulation” - think of it like rapidly turning a light switch on and off really fast. By changing how long it stays “on” vs “off”, you can control how bright an LED appears, how loud a buzzer sounds, or how fast a motor spins.
For buzzers, we care about two things:
-
Frequency - How fast we switch (determines the pitch/tone)
-
Duty Cycle - How long it stays “on” vs “off” (affects volume)
![]()
What you’ll need
Section titled “What you’ll need”- Your tinyCore ESP32-S3
- A passive buzzer (the kind that needs a signal to make sound)
- Some jumper wires
- A breadboard (optional)
Step 1: Wire it up
Section titled “Step 1: Wire it up”This is super simple - just two wires!
- Buzzer positive (+) wire → GPIO pin 12 on tinyCore
- Buzzer negative (-) wire → GND on tinyCore
![]()
Step 2: Basic beeping code
Section titled “Step 2: Basic beeping code”Let’s start with a simple beep to make sure everything works:
Upload this code and you should hear regular beeping! 🎵
Step 3: Playing different tones
Section titled “Step 3: Playing different tones”Now let’s get musical! Here’s code that plays a simple scale:
Step 4: Play a real song!
Section titled “Step 4: Play a real song!”Let’s play “Twinkle, Twinkle, Little Star”:
Step 5: Interactive buzzer
Section titled “Step 5: Interactive buzzer”Let’s make it respond to Serial input - type different letters to play different sounds:
Try typing different letters in the Serial Monitor and listen to the sounds!
Understanding the code
Section titled “Understanding the code”Let’s break down the key functions:
PWM Setup:
ledcSetup(pwmChannel, 2000, resolution); // Configure PWM channelledcAttachPin(buzzerPin, pwmChannel); // Connect channel to GPIO pinPlaying tones:
ledcWriteTone(pwmChannel, frequency); // Play a frequencyledcWriteTone(pwmChannel, 0); // Stop sound (frequency = 0)Common issues
Section titled “Common issues”No sound from buzzer
- Check you have a passive buzzer (not active)
- Verify wiring: positive to GPIO 2, negative to ground
- Try a different GPIO pin (any output pin works)
- Make sure the buzzer isn’t broken - test with 3.3V
Sound is too quiet
- Some buzzers are just quieter than others
- You can try adjusting the duty cycle with
ledcWrite(channel, 128)instead ofledcWriteTone() - Make sure connections are secure
Code won’t compile
- Make sure you’re using ESP32 board package version 3.x
- The functions changed in recent versions - this code is for the latest version
Buzzer makes noise even when stopped
- This can happen with some buzzers - try
ledcDetachPin(buzzerPin)to completely disconnect
What’s next?
Section titled “What’s next?”Now you understand PWM and analog output! This opens up tons of possibilities:
- Control LED brightness - Use PWM to dim/brighten LEDs smoothly
- Motor speed control - PWM controls how fast motors spin
- Servo control - Precise positioning of servo motors
- Sound effects - Create beeps, alarms, and musical instruments
- Combined projects - Add sound alerts to your motion detector
Want to learn more? Check out What is PWM? for the technical deep-dive, or try Reading a Button Press to learn about digital inputs!