Buzzer and Sound Feedback
Anand RamaswamiShare
Welcome Back, Maker!
You have made things light up. You have made things spin. Now it is time to make some noise.
Sound is one of the most powerful ways a device can talk to you. Think about it — your phone buzzes when you get a message. Your microwave beeps when your food is ready. Your fire alarm screams when there is smoke. All of that is a tiny component doing one simple job: turning electricity into sound.
That component is called a buzzer. And you already have one on your NanoMake Pro.
What Is a Buzzer?
A buzzer is an output device that converts electrical signals into sound.
It is one of the simplest components in electronics — send it a signal and it makes noise. Stop the signal and it goes quiet. That is all it does.
The buzzer on the NanoMake Pro is connected to pin D4. You control it exactly the same way you controlled your LEDs — using digitalWrite(). The only difference is that instead of seeing light, you hear sound.
How Does a Buzzer Actually Make Sound?
Inside the buzzer is a tiny piezoelectric disc. When electricity is applied to it, the disc bends very slightly. When electricity is removed, it bends back. Do this thousands of times per second and the disc vibrates — and that vibration pushes the air around it. That is exactly what sound is.
The faster it vibrates, the higher the pitch. The slower it vibrates, the lower the pitch. This is how you can play different tones using the same tiny buzzer.
The Two Ways to Control a Buzzer
You can control the buzzer in two ways and they give you very different results.
The first way is digitalWrite(). This turns the buzzer fully ON or fully OFF — giving you a constant tone at one fixed pitch. Simple, loud, and instant.
The second way is tone(). This is a special Arduino command built specifically for buzzers. It lets you tell the buzzer exactly which frequency to play — which means you can play different musical notes, build melodies, and create real audio feedback.
Let's start with the simple one first.
Project 1: Simple Beep Alert
This is the buzzer version of the blink project — your very first sound project. The buzzer beeps on for one second and off for one second, forever. Simple, satisfying, and a perfect way to hear the buzzer working for the first time.
Code:
|
void setup() { pinMode(4, OUTPUT); } void loop() { digitalWrite(4, HIGH); // Buzzer ON delay(1000); digitalWrite(4, LOW); // Buzzer OFF delay(1000); } |
Explanation:
pinMode(4, OUTPUT) tells the Arduino that pin 4 will send signals out. The buzzer is an output device — just like an LED.
digitalWrite(4, HIGH) sends 5V to the buzzer. The piezoelectric disc inside bends and vibrates and you hear a tone.
delay(1000) keeps the buzzer ON for one full second before the next line runs.
digitalWrite(4, LOW) cuts the voltage. The disc stops vibrating. Silence.
delay(1000) waits one more second before the loop repeats.
The buzzer keeps beeping on and off as long as the board is powered. Try changing the delay values — make it 100 instead of 1000 and listen to how a shorter ON time gives a sharper, quicker beep. The timing of a sound completely changes how it feels.
Output:
Now Let's Make It React to Something
A buzzer that beeps forever is not very useful. What makes a buzzer powerful is when it only makes noise because something actually happened.
Every alarm you have ever heard works this way. A fire alarm only goes off when smoke is detected. A car alarm only triggers when someone tries to break in. A doorbell only rings when someone presses the button. Something happens — the buzzer reacts.
Let's build that.
Project 2: Alarm System Using Button
In this project pressing the button triggers an alarm — the buzzer beeps rapidly three times as an alert. This is the foundation of almost every real alarm system you will ever build.
Code:
|
void setup() { pinMode(2, INPUT); pinMode(4, OUTPUT); } void loop() { int buttonState = digitalRead(2); if (buttonState == HIGH) { for (int i = 0; i < 3; i++) { digitalWrite(4, HIGH); delay(100); digitalWrite(4, LOW); delay(100); } delay(500); } } |
Explanation:
pinMode(2, INPUT) sets pin 2 as a digital input. On the NanoMake Pro the buttons have external pull-down resistors already on the board — so when the button is not pressed the pin reads LOW, and when it is pressed the pin reads HIGH. This is why we use INPUT and not INPUT_PULLUP here.
pinMode(4, OUTPUT) sets pin 4 as an output so we can send signals to the buzzer.
digitalRead(2) reads the current state of Button 1 and stores it in buttonState. It will be either HIGH or LOW.
if (buttonState == HIGH) checks whether the button is being pressed. If it is, everything inside the curly braces runs.
for (int i = 0; i < 3; i++) is a loop that runs three times. Each time it runs, it beeps the buzzer once. i starts at 0 and increases by 1 each time. When i reaches 3 the loop stops.
Inside the loop, digitalWrite(4, HIGH) turns the buzzer ON for 100 milliseconds. Then digitalWrite(4, LOW) turns it OFF for another 100 milliseconds. That is one beep. The loop repeats that two more times — giving you three short rapid beeps every time the button is pressed.
delay(500) adds a short pause after the three beeps finish. Without this the Arduino might trigger another round of beeps before you have even lifted your finger off the button.
Press the button and count — three quick beeps, then silence. This triple-beep pattern already feels like a real alert. The exact same logic runs in burglar alarms, smoke detectors, and factory warning systems. A sensor detects something, a condition becomes true, the buzzer fires.
Output:
What About Playing Actual Music?
So far the buzzer only knows one pitch — whatever tone it naturally produces when you send it HIGH. But the tone() command changes everything.
By playing different frequencies one after another and controlling how long each one lasts, you can play recognizable melodies. The buzzer does not know it is playing music. It is just vibrating at whatever frequency you tell it to. But your ears hear the pattern and the brain fills in the rest.
The Command: tone()
tone() tells the buzzer to vibrate at a specific frequency — producing a specific musical pitch.
|
tone(pin, frequency); tone(pin, frequency, duration); |
Parameters:
- pin → the pin the buzzer is connected to (pin 4 on the NanoMake Pro)
- frequency → how many times per second the buzzer vibrates, measured in Hz
- duration → optional — how long to play the tone in milliseconds before it stops automatically
To stop the tone manually:
| noTone(pin); |
Every musical note has a frequency. Here are the ones we will use:
|
Note |
Frequency |
|
C4 (Middle C) |
262 Hz |
|
D4 |
294 Hz |
|
E4 |
330 Hz |
|
F4 |
349 Hz |
|
G4 |
392 Hz |
|
A4 |
440 Hz |
|
B4 |
494 Hz |
|
C5 |
523 Hz |
Higher Hz means higher pitch. Lower Hz means lower pitch. That is all there is to it.
Project 3: Play a Simple Melody
In this project the buzzer plays a short four-note melody using tone() — a clean rising sequence that you will recognize immediately when you hear it.
Code:
|
#define NOTE_C4 262 #define NOTE_E4 330 #define NOTE_G4 392 #define NOTE_C5 523 void setup() { pinMode(4, OUTPUT); } void loop() { tone(4, NOTE_C4, 300); delay(350); tone(4, NOTE_E4, 300); delay(350); tone(4, NOTE_G4, 300); delay(350); tone(4, NOTE_C5, 500); delay(600); noTone(4); delay(1000); } |
Explanation:
#define NOTE_C4 262 gives the frequency a name. Instead of writing 262 every time, you write NOTE_C4. It makes the code much easier to read — especially when writing longer melodies. The Arduino replaces NOTE_C4 with 262 automatically when it compiles the code. This is not a variable — it is a shortcut name.
pinMode(4, OUTPUT) sets pin 4 as an output so the buzzer can receive signals from the Arduino.
tone(4, NOTE_C4, 300) tells pin 4 to play the note C4 — 262 Hz — for 300 milliseconds. The third number is the duration. After 300ms the note stops automatically.
delay(350) gives a short gap after the note. You might wonder — the note is already set to last 300ms, so why the extra delay? Because without a gap between notes they blur together into one continuous sound. The extra 50ms of silence is what makes each note sound clean and separate.
tone(4, NOTE_E4, 300) plays E4 — a slightly higher pitch than C4. Then delay(350) again for the gap.
tone(4, NOTE_G4, 300) plays G4 — higher still. The melody is rising step by step.
tone(4, NOTE_C5, 500) plays C5 — the final note. It is held for 500ms instead of 300ms because the last note of a melody naturally feels better when it lingers a little longer. This is a small detail but it makes a real difference to how the tune feels.
delay(600) gives the last note space to breathe after it finishes.
noTone(4) stops any remaining tone that might still be running. Always call this after your melody ends. It is good practice and prevents a faint hum from hanging in the air.
delay(1000) pauses for a full second before the loop repeats and the melody plays again.
Upload the code and listen. C, E, G, C — a clean rising four-note phrase. Once it is working try swapping the notes, changing the durations, or adding more tone() lines after the last one. You can compose your own tune just by changing the numbers.
Output:
Buzzer Sound Is Everywhere
Once you start noticing it you will hear buzzer-style feedback in almost everything around you.
When you press a button on a microwave, the small beep confirming your press is a tiny buzzer firing for a fraction of a second. When a truck reverses and you hear that steady warning beep, that is a buzzer circuit detecting the reverse gear signal. When a hospital monitor beeps in a steady rhythm to show a patient is okay — and then changes its pattern when something is wrong — that is exactly the same logic you built in Project 2. A condition changes, the buzzer changes, the sound tells you something.
Sound is information. And now you know how to generate it.
Final Thoughts
You started this blog with a buzzer that could only go on and off. Now you have made it beep as an alert, respond to a button press like an alarm, and play a melody using real musical frequencies.
The buzzer is one of those components that feels almost too simple at first. But the moment it starts making the right sound at the right time — a beep when someone presses a button, an alert when a sensor triggers — your project suddenly feels alive.
That is what feedback is. Your device is talking back to you.
Keep experimenting, keep building, and keep making.