Mission 20 · Stage 2

Mini Alarm

armed and disarmed if tree

Arm or disarm an alarm with a button; motion triggers the buzzer when armed.

Mini Alarm circuit diagram

Pin connections

Part 1Part 2

Arduino

pin 2

Arm button

pin 1

Arm button

pin 2

Arduino

GND

Motion sensor

VCC

Arduino

5V

Motion sensor

GND

Arduino

GND

Motion sensor

OUT

Arduino

pin 7

Arduino

pin 8

Buzzer

pin 1 (+)

Buzzer

pin 2 (-)

Arduino

GND

Arduino

pin 13

Resistor

pin 1

Resistor

pin 2

Status LED

anode (+)

Status LED

cathode (-)

Arduino

GND

See it

Let's build a motion alarm!

Arm it with a button, then any movement makes the buzzer scream.

Real home alarms work this way — you turn them on, and then motion sets them off.

The story

The problem

We need TWO things to be true before the alarm rings: it is armed AND there is motion.

Think of it like

Like turning on the house alarm before you leave — only then does motion matter.

Meet the parts

It checks the button and the motion sensor.

Arduino

The brain

Loading part…

Press it to turn the alarm on or off.

Arm button

The arm button

Loading part…

It notices when something moves nearby.

Motion sensor

The motion sensor

Loading part…

It screams when armed and there is motion.

Buzzer

The alarm sound

Loading part…

Keeps the status light safe.

Resistor

The protector

Loading part…

It glows when the alarm is on.

Status LED

The status light

Loading part…

How it works

1

Turn the alarm on or off

A button press flips the alarm on or off, and the status light shows it.

armed = !armed;
2

Is there motion?

Both must be true — armed AND motion — before the alarm sounds.

if (armed && digitalRead(PIR_PIN) == HIGH)
3

Scream or stay quiet

The buzzer only rings when armed and there is motion. Otherwise it stays silent.

tone(BUZZER, 880) / noTone(BUZZER)

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 — ready to build!

    Loading part…
  2. 2

    Place Arm button

    Place the Arm button (btn1) on the breadboard.

    Loading part…
  3. 3

    Place Motion sensor

    Place the Motion sensor (pir1) on the breadboard.

    Loading part…
  4. 4

    Place Buzzer

    Place the Buzzer (bz1) on the breadboard.

    Loading part…
  5. 5

    Place Resistor

    Place the Resistor (r1) on the breadboard.

    Loading part…
  6. 6

    Place Status LED

    Place the Status LED (led1) on the breadboard.

    Loading part…
  7. 7

    Connect Arduino pin 2 to Arm button (btn1) 1.l

    Connect Arduino pin 2 to Arm button (btn1) 1.l.

  8. 8

    Connect Arm button (btn1) 2.l to Arduino GND

    Connect Arm button (btn1) 2.l to Arduino GND.

  9. 9

    Connect Motion sensor (pir1) VCC to Arduino pin 5

    Connect Motion sensor (pir1) VCC to Arduino pin 5.

  10. 10

    Connect Motion sensor (pir1) GND to Arduino GND

    Connect Motion sensor (pir1) GND to Arduino GND.

  11. 11

    Connect Motion sensor (pir1) OUT to Arduino pin 7

    Connect Motion sensor (pir1) OUT to Arduino pin 7.

  12. 12

    Connect Arduino pin 8 to Buzzer (bz1) 1

    Connect Arduino pin 8 to Buzzer (bz1) 1.

  13. 13

    Connect Buzzer (bz1) 2 to Arduino GND

    Connect Buzzer (bz1) 2 to Arduino GND.

  14. 14

    Connect Arduino pin 13 to Resistor (r1) 1

    Connect Arduino pin 13 to Resistor (r1) 1.

  15. 15

    Connect Resistor (r1) 2 to Status LED (led1) anode (+)

    Connect Resistor (r1) 2 to Status LED (led1) anode (+).

  16. 16

    Connect Status LED (led1) cathode (-) to Arduino GND

    Connect Status LED (led1) cathode (-) to Arduino GND.

Try it

  • Press the button to arm — the status light turns on.
  • Make motion while armed — the buzzer should sound. Disarm to stop it.

Peek at code

Arm or disarm

void loop() {
  bool btn = digitalRead(ARM_BTN);
  if (lastBtn == HIGH && btn == LOW) {
    armed = !armed;
    digitalWrite(STATUS_LED, armed ? HIGH : LOW);
    Serial.println(armed ? "ARMED" : "DISARMED");
    noTone(BUZZER);
  }
  lastBtn = btn;

A fresh button press flips the alarm on or off and prints ARMED or DISARMED.

The AND check

  if (armed && digitalRead(PIR_PIN) == HIGH) {
    tone(BUZZER, 880);
  } else {
    noTone(BUZZER);
  }
  delay(20);

armed AND motion both have to be true for the alarm to ring — that is real alarm logic.

Show full sketch (mini-alarm.ino)
const int ARM_BTN = 2;
const int PIR_PIN = 7;
const int BUZZER = 8;
const int STATUS_LED = 13;
bool armed = false;
bool lastBtn = HIGH;
void setup() {
  pinMode(ARM_BTN, INPUT_PULLUP);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(STATUS_LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mini Alarm — press button to arm/disarm");
}
void loop() {
  bool btn = digitalRead(ARM_BTN);
  if (lastBtn == HIGH && btn == LOW) {
    armed = !armed;
    digitalWrite(STATUS_LED, armed ? HIGH : LOW);
    Serial.println(armed ? "ARMED" : "DISARMED");
    noTone(BUZZER);
  }
  lastBtn = btn;
  if (armed && digitalRead(PIR_PIN) == HIGH) {
    tone(BUZZER, 880);
  } else {
    noTone(BUZZER);
  }
  delay(20);
}

Quick quiz

Q1. Where does work that repeats go?

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

Q2. When does the buzzer sound?

  • A. When the alarm is armed AND there is motion
  • B. Whenever there is motion
  • C. Only in setup()
Why: Yes! It needs armed AND motion before it rings.

Code lab — try on your own

  1. Change the alarm sound — use tone(BUZZER, 660) instead of 880.

    Hint: That's line 25.

  2. Add a note (comment) on line 24 that says it needs armed AND motion.

    Hint: That's the if (armed && ...) line.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robot Gurukul Studio.

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

A button turns the alarm on or off. When it is on, motion makes the buzzer sound.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

const int ARM_BTN = 2;
const int PIR_PIN = 7;
const int BUZZER = 8;
const int STATUS_LED = 13;
bool armed = false;
bool lastBtn = HIGH;

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It gets the button, motion sensor, buzzer, and status light ready.

Why here

Things we do only once go inside setup().

void setup() {
  pinMode(ARM_BTN, INPUT_PULLUP);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(STATUS_LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mini Alarm — press button to arm/disarm");
}

loop()

Big idea

loop() runs again and again, forever.

In this project

It checks the button to arm or disarm, then watches for motion.

Why here

Things that repeat go inside loop().

void loop() {
  bool btn = digitalRead(ARM_BTN);
  if (lastBtn == HIGH && btn == LOW) {
    armed = !armed;
    digitalWrite(STATUS_LED, armed ? HIGH : LOW);
    Serial.println(armed ? "ARMED" : "DISARMED");
    noTone(BUZZER);
  }
  lastBtn = btn;
  if (armed && digitalRead(PIR_PIN) == HIGH) {
    tone(BUZZER, 880);
  } else {
    noTone(BUZZER);
  }
  delay(20);
}

Try this: Change a number inside loop(), then press Run to see what happens.

pinMode

Big idea

pinMode tells a pin if it will listen or push power out.

In this project

It sets the button and motion sensor to listen, and the buzzer and light to push power out.

Why here

It goes in setup() because we only set it once.

  pinMode(ARM_BTN, INPUT_PULLUP);

digitalWrite

Big idea

digitalWrite turns a pin ON or OFF.

In this project

It turns the status light on when the alarm is armed.

Why here

It goes in loop() when you arm or disarm.

    digitalWrite(STATUS_LED, armed ? HIGH : LOW);

digitalRead

Big idea

digitalRead checks if a pin is ON or OFF.

In this project

It reads the button to see if you want to arm or disarm.

Why here

It goes in loop() so we can catch your press.

  bool btn = digitalRead(ARM_BTN);

tone

Big idea

tone makes a buzzer play a beep at a chosen pitch.

In this project

It sounds the alarm when armed and motion is found.

Why here

It goes in loop() when the alarm should ring.

    tone(BUZZER, 880);

noTone

Big idea

noTone stops the buzzer so it goes quiet.

In this project

It keeps the buzzer silent when there is no danger.

Why here

It goes in loop() when the alarm should not ring.

    noTone(BUZZER);

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

A tiny wait keeps the checks steady each loop.

Why here

At the end of loop().

  delay(20);

begin

Big idea

Serial.begin opens a way for the board to send words to your computer screen.

In this project

It lets the program print ARMED or DISARMED.

Why here

It goes in setup() once, before we print anything.

  Serial.begin(9600);

println

Big idea

println sends a line of words to the screen.

In this project

It prints the start message and the alarm state.

Why here

In setup() so you see the start message.

  Serial.println("Mini Alarm — press button to arm/disarm");