Skip to content

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.

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)

PWM Diagram

  • Your tinyCore ESP32-S3
  • A passive buzzer (the kind that needs a signal to make sound)
  • Some jumper wires
  • A breadboard (optional)

This is super simple - just two wires!

  1. Buzzer positive (+) wireGPIO pin 12 on tinyCore
  2. Buzzer negative (-) wireGND on tinyCore

Buzzer Wiring

Let’s start with a simple beep to make sure everything works:

Upload this code and you should hear regular beeping! 🎵

Now let’s get musical! Here’s code that plays a simple scale:

Let’s play “Twinkle, Twinkle, Little Star”:

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!

Let’s break down the key functions:

PWM Setup:

ledcSetup(pwmChannel, 2000, resolution); // Configure PWM channel
ledcAttachPin(buzzerPin, pwmChannel); // Connect channel to GPIO pin

Playing tones:

ledcWriteTone(pwmChannel, frequency); // Play a frequency
ledcWriteTone(pwmChannel, 0); // Stop sound (frequency = 0)

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 of ledcWriteTone()
  • 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

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!