DEV Community

Cover image for Arduino Mega - Piezo Buzzer
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino Mega - Piezo Buzzer

A piezo buzzer is one of the cheapest ways to give an Arduino Mega a voice. It turns electrical signals into sound, so you can add beeps, alarms, or melodies to a project without needing a speaker, amplifier, or audio circuit. This guide covers the two main types of buzzer, how they're wired to the Mega, and how the code turns a pin into a sound source.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino Mega - Piezo Buzzer

What you need

Wiring

Arduino Mega - Piezo Buzzer wiring diagram

A piezo buzzer has two terminals: a negative/ground pin and a positive/signal pin. The negative pin goes to the Arduino Mega's GND, and the positive pin connects to a digital output pin, such as pin 2 or 4. On wired buzzers the black or shorter wire is usually ground and the red or longer wire is signal; on PCB-mounted modules these are marked with - and +. For buzzers in the 3-5V range you can drive them directly from an Arduino digital pin with no extra driver circuitry needed.

How it works

Buzzers split into active and passive types. An active buzzer has its own internal oscillator, so applying a steady HIGH signal makes it produce a fixed tone (typically 2-4 kHz) automatically, just like turning on an LED - no frequency control needed. A passive buzzer has no oscillator; it stays silent under a plain digitalWrite(HIGH) and needs an alternating signal to vibrate the piezo element and produce sound. Piezo buzzers themselves use a ceramic crystal that flexes when voltage is applied, giving a wide frequency range, very low current draw, and no moving parts to wear out.

Code

For an active buzzer, the code is just digital on/off control: digitalWrite(buzzerPin, HIGH) starts a continuous tone and digitalWrite(buzzerPin, LOW) silences it. Because the tone's pitch is fixed by the buzzer's internal oscillator, you shape the sound by timing the pulses instead, for example looping short HIGH/LOW delays to produce a sequence of beeps rather than one long tone.

arduino-mega-piezo-buzzer.ino

// Simple continuous beep
digitalWrite(buzzerPin, HIGH);  // Turn on
delay(1000);                    // Beep for 1 second
digitalWrite(buzzerPin, LOW);   // Turn off

// Beep pattern example
for (int i = 0; i < 3; i++) {   // Three short beeps
  digitalWrite(buzzerPin, HIGH);
  delay(100);
  digitalWrite(buzzerPin, LOW);
  delay(100);
}
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-mega/arduino-mega-piezo-buzzer

Top comments (0)