An active buzzer is the simplest way to give a project a voice: tank-full alarms, door-open reminders, gas alerts and keypad clicks. It needs one digital pin; this guide uses D8.
The sketch plays three patterns in turn. Copy the beep() function into your own project and call it with the on time, off time and repeat count.
Wiring
| Arduino Nano | Active buzzer module (high-level trigger) | Wire | Notes |
|---|---|---|---|
| D8 | I/O | yellow | |
| 5V | VCC | red | |
| GND | GND | black |
3.3–5 V part, 5 V board. The I/O pin only drives the module's transistor, so a 3.3 V GPIO is enough even when the buzzer runs from 5 V. Check the label: "high-level trigger" beeps when I/O is HIGH; "low-level trigger" modules (with a PNP transistor) beep when I/O is LOW.
Board quirks
- An active buzzer has its own oscillator: HIGH = beep.
tone()chops the supply and makes it buzz badly. For melodies you need a passive buzzer. - The module’s transistor does the switching, so the Nano pin only supplies a few mA.
- Check the label: “low-level trigger” modules beep when I/O is LOW, and will beep all through the bootloader until
setup()runs. delay()-based beeps block the sketch. For an alarm that runs alongside sensor reading, switch to amillis()timer.
Code
// Active buzzer module on an Arduino Nano: three alert patterns you can reuse.
// I/O → D8, VCC → 5V, GND → GND. High-level trigger module (I/O HIGH = beep).
// An active buzzer has its own oscillator — use digitalWrite, not tone().
const uint8_t BUZZER_PIN = 8;
void beep(unsigned int onMs, unsigned int offMs, uint8_t times) {
for (uint8_t i = 0; i < times; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(onMs);
digitalWrite(BUZZER_PIN, LOW);
delay(offMs);
}
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
Serial.println(F("Pattern 1: single chirp (key press)"));
beep(60, 0, 1);
delay(2000);
Serial.println(F("Pattern 2: double beep (door open)"));
beep(150, 150, 2);
delay(2000);
Serial.println(F("Pattern 3: fast alarm (tank full / gas)"));
beep(100, 100, 10);
delay(4000);
}// Active buzzer module on an Arduino Nano: three alert patterns you can reuse.
// I/O → D8, VCC → 5V, GND → GND. High-level trigger module (I/O HIGH = beep).
// An active buzzer has its own oscillator — use digitalWrite, not tone().
const uint8_t BUZZER_PIN = 8;
void beep(unsigned int onMs, unsigned int offMs, uint8_t times) {
for (uint8_t i = 0; i < times; i++) {
digitalWrite(BUZZER_PIN, HIGH);
delay(onMs);
digitalWrite(BUZZER_PIN, LOW);
delay(offMs);
}
}
void setup() {
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(115200);
}
void loop() {
Serial.println(F("Pattern 1: single chirp (key press)"));
beep(60, 0, 1);
delay(2000);
Serial.println(F("Pattern 2: double beep (door open)"));
beep(150, 150, 2);
delay(2000);
Serial.println(F("Pattern 3: fast alarm (tank full / gas)"));
beep(100, 100, 10);
delay(4000);
}Expected serial output
Pattern 1: single chirp (key press)
Pattern 2: double beep (door open)
Pattern 3: fast alarm (tank full / gas)Pattern 1: single chirp (key press) Pattern 2: double beep (door open) Pattern 3: fast alarm (tank full / gas)
Common errors
Buzzer beeps continuously
A low-level trigger module, or I/O and VCC swapped. Invert the logic or check the pin labels.
Sound is a weak click
It is a passive buzzer. Use tone(8, 2000) for it, or buy an active one (sealed back, longer leg = +).