Runs your sketch
Arduino
Brain
if statement branches output
Play a buzzer tone when the button is pressed using an if statement.

Pin connections
| Part 1 | Part 2 | |
|---|---|---|
Arduino pin 2 | → | Button pin 1 |
Button pin 2 | → | Arduino GND |
Buzzer pin 1 (+) | → | Arduino pin 8 |
Buzzer pin 2 (-) | → | Arduino GND |
if — your first decision!
Press the button and the buzzer plays; release and it stops.
if statements choose what code runs — in alarms, games, and robots.
The problem
You want different outputs depending on whether the button is pressed.
Think of it like
Like saying “If it is raining, take an umbrella.”
Runs your sketch
Arduino
Brain
Triggers the if branch
Button
Input
Beeps when pressed
Buzzer
Sound
Check button
The if condition is true only while you hold the button.
if (digitalRead(BUTTON_PIN) == LOW)
Play tone
Inside the if block — buzzer sounds at 440 Hz.
tone(BUZZER_PIN, 440);
Otherwise stop
When not pressed, noTone() keeps the buzzer quiet.
else { noTone(BUZZER_PIN); }Then loop back to step 1
Follow these steps in order. Match the wires to the colors shown.
Place Arduino
Place the Arduino (uno) on the breadboard.
Arduino placed!
Place Button
Place the Button (btn1) on the breadboard.
Place Buzzer
Place the Buzzer (bz1) on the breadboard.
Connect Arduino pin 2 to Button (btn1) 1.l
Connect Arduino pin 2 to Button (btn1) 1.l.
Connect Button (btn1) 2.l to Arduino GND
Connect Button (btn1) 2.l to Arduino GND.
Connect Buzzer (bz1) 1 to Arduino pin 8
Connect Buzzer (bz1) 1 to Arduino pin 8.
Connect Buzzer (bz1) 2 to Arduino GND
Connect Buzzer (bz1) 2 to Arduino GND.
Setup input and output
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}Button is an input with pull-up; buzzer pin is an output.
if / else branch
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
tone(BUZZER_PIN, 440);
} else {
noTone(BUZZER_PIN);
}
delay(20);
}One branch plays tone(), the other calls noTone() — classic if decision.
const int BUTTON_PIN = 2;
const int BUZZER_PIN = 8;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
tone(BUZZER_PIN, 440);
} else {
noTone(BUZZER_PIN);
}
delay(20);
}
Q1. Where does repeating work belong?
Q2. What runs when the button is NOT pressed?
Change the tone pitch — try tone(BUZZER_PIN, 880) instead of 440.
Hint: Inside the if block, line 9.
Add a comment above the if line: // buzzer when pressed
Hint: Line 8.
A line-by-line tour of the sketch — the same steps as in Robo Gurukul Studio.
Program overview
Technical
Sketches have globals, then setup() once, then loop() forever.
In this project
Play a buzzer tone when the button is pressed using an if statement.
Why here
Read from top to bottom. Hover words or lines for help!
const int BUTTON_PIN = 2; const int BUZZER_PIN = 8;
setup()
Technical
Runs one time when the board turns on.
In this project
Sets up pins and libraries for Push Button Buzzer.
Why here
One-time setup belongs here—not in loop().
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(BUZZER_PIN, OUTPUT);
}loop()
Technical
Runs again and again after setup() is done.
In this project
This is the main action you see in Push Button Buzzer.
Why here
Repeating work (blink, read sensors) goes here.
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) {
tone(BUZZER_PIN, 440);
} else {
noTone(BUZZER_PIN);
}
delay(20);
}
Try this: Change numbers in loop(), then compile and run the simulator.
pinMode
Technical
Tells a pin if it listens or drives something.
In this project
Gets the Push Button Buzzer circuit ready in the simulator.
Why here
Goes in setup() because we only set pins once at the start.
pinMode(BUTTON_PIN, INPUT_PULLUP);
digitalRead
Technical
Checks if a pin is ON or OFF.
In this project
Reads buttons or sensors in Push Button Buzzer.
Why here
Goes in loop() so we can react when something changes.
if (digitalRead(BUTTON_PIN) == LOW) {tone
Technical
Plays a beep on a buzzer pin at a chosen pitch.
In this project
Makes sounds in Push Button Buzzer.
Why here
Goes in loop() when you want notes or alarms.
tone(BUZZER_PIN, 440);
noTone
Technical
Stops the buzzer sound on that pin.
In this project
Turns off the sound in Push Button Buzzer.
Why here
After tone() or before a pause so the buzzer is silent.
noTone(BUZZER_PIN);
delay
Technical
Waits for some time. Nothing else runs during the wait.
In this project
Controls speed so you can see Push Button Buzzer in the simulator.
Why here
Right after an action that should stay the same for a moment.
delay(20);