A buzzer on Arduino Uno
Steps done: 0 of 5
Guess the answer and press Run
D8, Hz0time, s0.0
Lesson steps
What does tone() do?
The line tone(buzzer, 262). Pick an answer before you run it.
Sketch
// Passive buzzer: "+" through a 220 Ω resistor to D8, "-" to GND
const int buzzer = 8;
int notes[] = {262, 330, 392, 523}; // note frequencies, Hz
int beats[] = {1, 1, 1, 2}; // durations in beats
int tempoMs = 200; // one beat, ms
int pauseMs = 1000; // silence after the melody, ms
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 4; i++) {
int ms = beats[i] * tempoMs;
Serial.println(notes[i]);
tone(buzzer, notes[i]);
delay(ms);
noTone(buzzer);
delay(40);
}
delay(pauseMs);
}
Tap a line or an orange number
Serial monitor
Serial, 9600 baudboard time 0.0 s
About the lesson
A 12 mm passive buzzer: the plus through a 220 Ω resistor to D8, the minus to GND. A four-note melody sits in the notes and beats arrays. The sound is off by default, an icon on the scene turns it on. About 12 minutes, no sign-up. Five steps: guess, run, read the code, change the numbers and do a task with a check.
Build it for real
Parts
- Arduino Uno R3
- Mini breadboard
- 12 mm passive buzzer
- 220 Ω resistor
- 2 male-to-male jumper wires
Wires
| From | To |
|---|---|
| D8 | 220 Ω resistor, then the buzzer "+" |
| buzzer "−" | GND |
Take a passive buzzer: you can see the board on its underside, and from a battery it only clicks. An active one beeps by itself from a constant voltage, and tone() turns its beep into a rasp.
Open the schematic in the builderPinout of the lesson boardBuzzer pinout
The sketch below goes to the board with the default numbers: notes[0] 262, notes[1] 330, notes[2] 392, notes[3] 523, beats[0] 1, beats[1] 1, beats[2] 1, beats[3] 2, tempoMs 200, pauseMs 1000. The numbers you changed in the lesson are passed to the flasher by the To the board link above the code.
The whole sketch
// Passive buzzer: "+" through a 220 Ω resistor to D8, "-" to GND
const int buzzer = 8;
int notes[] = {262, 330, 392, 523}; // note frequencies, Hz
int beats[] = {1, 1, 1, 2}; // durations in beats
int tempoMs = 200; // one beat, ms
int pauseMs = 1000; // silence after the melody, ms
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i < 4; i++) {
int ms = beats[i] * tempoMs;
Serial.println(notes[i]);
tone(buzzer, notes[i]);
delay(ms);
noTone(buzzer);
delay(40);
}
delay(pauseMs);
}
What to keep in mind on a real board
A passive buzzer coil is 16-42 Ω. Straight from a pin it would draw up to 100 mA with 40 mA allowed, so the lesson uses a 220 Ω resistor. It is louder through an S8050 or BC547 transistor with a diode in parallel with the buzzer.
tone() takes Timer2: while a note sounds, analogWrite() on pins 3 and 11 does not work. tone() plays on only one pin at a time.
The sketch takes 3668 bytes of flash and 225 bytes of RAM.
Licenses of the lesson code
The avr8js 0.21.1 emulator, © 2019-2025 Uri Shaked, MIT license. The CodeMirror 6 editor, © Marijn Haverbeke and contributors, MIT license. The three.js scene, © three.js authors, MIT license.
The lesson firmware is built with the Arduino AVR core (LGPL 2.1), sources: ArduinoCore-avr. The lesson sketch and scene are made for this site.
MIT license text: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.