It checks the button and the motion sensor.
Arduino
The brain
armed and disarmed if tree
Arm or disarm an alarm with a button; motion triggers the buzzer when armed.

Pin connections
| Part 1 | Part 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 |
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 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.
It checks the button and the motion sensor.
Arduino
The brain
Press it to turn the alarm on or off.
Arm button
The arm button
It notices when something moves nearby.
Motion sensor
The motion sensor
It screams when armed and there is motion.
Buzzer
The alarm sound
Keeps the status light safe.
Resistor
The protector
It glows when the alarm is on.
Status LED
The status light
Turn the alarm on or off
A button press flips the alarm on or off, and the status light shows it.
armed = !armed;
Is there motion?
Both must be true — armed AND motion — before the alarm sounds.
if (armed && digitalRead(PIR_PIN) == HIGH)
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
Follow these steps in order. Match the wires to the colors shown.
Place Arduino
Place the Arduino (uno) on the breadboard.
Arduino placed — ready to build!
Place Arm button
Place the Arm button (btn1) on the breadboard.
Place Motion sensor
Place the Motion sensor (pir1) on the breadboard.
Place Buzzer
Place the Buzzer (bz1) on the breadboard.
Place Resistor
Place the Resistor (r1) on the breadboard.
Place Status LED
Place the Status LED (led1) on the breadboard.
Connect Arduino pin 2 to Arm button (btn1) 1.l
Connect Arduino pin 2 to Arm button (btn1) 1.l.
Connect Arm button (btn1) 2.l to Arduino GND
Connect Arm button (btn1) 2.l to Arduino GND.
Connect Motion sensor (pir1) VCC to Arduino pin 5
Connect Motion sensor (pir1) VCC to Arduino pin 5.
Connect Motion sensor (pir1) GND to Arduino GND
Connect Motion sensor (pir1) GND to Arduino GND.
Connect Motion sensor (pir1) OUT to Arduino pin 7
Connect Motion sensor (pir1) OUT to Arduino pin 7.
Connect Arduino pin 8 to Buzzer (bz1) 1
Connect Arduino pin 8 to Buzzer (bz1) 1.
Connect Buzzer (bz1) 2 to Arduino GND
Connect Buzzer (bz1) 2 to Arduino GND.
Connect Arduino pin 13 to Resistor (r1) 1
Connect Arduino pin 13 to Resistor (r1) 1.
Connect Resistor (r1) 2 to Status LED (led1) anode (+)
Connect Resistor (r1) 2 to Status LED (led1) anode (+).
Connect Status LED (led1) cathode (-) to Arduino GND
Connect Status LED (led1) cathode (-) to Arduino GND.
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.
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);
}
Q1. Where does work that repeats go?
Q2. When does the buzzer sound?
Change the alarm sound — use tone(BUZZER, 660) instead of 880.
Hint: That's line 25.
Add a note (comment) on line 24 that says it needs armed AND motion.
Hint: That's the if (armed && ...) line.
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");