Buzzer Reference
A piezo buzzer that produces sound when driven with a square wave — typically via the
built-in tone() function.
Pin names
| Name | Description |
|---|---|
| 1 | Positive (+) |
| 2 | Negative (−) |
Attributes
This component has no configurable attributes.
Usage
Connect pin 1 to a GPIO pin and pin 2 to GND. Use tone(pin, frequency) to play a
note, and noTone(pin) to stop.
const int BUZZER_PIN = 8;
void setup() {
// no pinMode needed — tone() configures the pin
}
void loop() {
tone(BUZZER_PIN, 440); // A4
delay(300);
noTone(BUZZER_PIN);
delay(300);
}
Play a short melody using tone() with a duration argument:
const int BUZZER_PIN = 8;
int notes[] = {262, 294, 330, 349, 392}; // C D E F G
void setup() {
for (int n : notes) {
tone(BUZZER_PIN, n, 200);
delay(250);
}
}
void loop() {}
Examples
| Description | Attrs |
|---|---|
| Default buzzer | {} |
See also
- Servo — another common single-pin PWM-driven actuator