Mission 12 · Stage 2

Push Button Buzzer

if statement branches output

Play a buzzer tone when the button is pressed using an if statement.

Push Button Buzzer circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Button

pin 1

Button

pin 2

Arduino

GND

Buzzer

pin 1 (+)

Arduino

pin 8

Buzzer

pin 2 (-)

Arduino

GND

See it

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 story

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.”

Meet the parts

Runs your sketch

Arduino

Brain

Loading part…

Triggers the if branch

Button

Input

Loading part…

Beeps when pressed

Buzzer

Sound

Loading part…

How it works

1

Check button

The if condition is true only while you hold the button.

if (digitalRead(BUTTON_PIN) == LOW)
2

Play tone

Inside the if block — buzzer sounds at 440 Hz.

tone(BUZZER_PIN, 440);
3

Otherwise stop

When not pressed, noTone() keeps the buzzer quiet.

else { noTone(BUZZER_PIN); }

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed!

    Loading part…
  2. 2

    Place Button

    Place the Button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Buzzer

    Place the Buzzer (bz1) on the breadboard.

    Loading part…
  4. 4

    Connect Arduino pin 2 to Button (btn1) 1.l

    Connect Arduino pin 2 to Button (btn1) 1.l.

  5. 5

    Connect Button (btn1) 2.l to Arduino GND

    Connect Button (btn1) 2.l to Arduino GND.

  6. 6

    Connect Buzzer (bz1) 1 to Arduino pin 8

    Connect Buzzer (bz1) 1 to Arduino pin 8.

  7. 7

    Connect Buzzer (bz1) 2 to Arduino GND

    Connect Buzzer (bz1) 2 to Arduino GND.

Try it

  • Press and hold — you should hear a steady beep.
  • Release — silence! That is if/else in action.

Peek at code

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.

Show full sketch (pushbutton-buzzer.ino)
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);
}

Quick quiz

Q1. Where does repeating work belong?

  • A. loop()
  • B. setup()
  • C. pinMode only
Why: Correct—loop() runs again and again.

Q2. What runs when the button is NOT pressed?

  • A. The else block with noTone()
  • B. setup() again
  • C. tone() forever
Why: Correct — else calls noTone() to stop the buzzer.

Code lab — try on your own

  1. Change the tone pitch — try tone(BUZZER_PIN, 880) instead of 440.

    Hint: Inside the if block, line 9.

  2. Add a comment above the if line: // buzzer when pressed

    Hint: Line 8.

Code walkthrough

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);