⭐⭐ Advanced beginner
⏱️ 20 minutes
Motion alarm
You build a burglar alarm. Does the motion sensor see someone? Then the buzzer sounds and the red LED blinks.
🧰 What do you need?
💡
1× LED (little light)
🔌
USB cable + Jumper wires🗺️ Wiring diagram
Follow the wires from the Arduino to each part. You can choose the wire colors yourself.
| From | To |
|---|---|
Arduino UNO · D2 |
Motion sensor (PIR) · OUT |
Arduino UNO · 5V |
Motion sensor (PIR) · VCC |
Arduino UNO · GND |
Motion sensor (PIR) · GND |
Arduino UNO · D8 |
Active buzzer · S |
Arduino UNO · 5V |
Active buzzer · + |
Arduino UNO · GND |
Active buzzer · - |
Arduino UNO · D13 |
Resistor 220 Ω · 1 |
Resistor 220 Ω · 2 |
LED (little light) · ANODE |
LED (little light) · CATHODE |
Arduino UNO · GND |
🛠️ This is how you build it
- Connect the PIR sensor: OUT to pin 2, VCC to 5V and GND to GND.
- Connect the buzzer: S to pin 8, + to 5V and − to GND.
- Put the red LED with a 220 Ω resistor on pin 13, with the short leg to GND.
💻 The code
// Motion alarm
// Is someone walking by? Then the red LED blinks and the buzzer sounds.
const int PIR_PIN = 2; // OUT from the motion sensor on pin 2
const int BUZZER_PIN = 8; // S from the buzzer on pin 8
const int LED_PIN = 13; // Red LED on pin 13 (through the resistor)
void setup() {
Serial.begin(9600);
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println(F("Sensor warming up... stand still!"));
delay(10000); // The sensor needs a moment to get used to the room
Serial.println(F("Alarm is on."));
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) { // Motion seen!
Serial.println(F("Motion! ALARM!"));
for (int i = 0; i < 5; i++) { // Beep and blink 5 times
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
delay(150);
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
delay(150);
}
}
delay(100);
}
🧠 What does the code do?
- The PIR sensor is on pin 2. If it sees motion, the pin is HIGH.
- When we start up, we wait 10 seconds. This lets the sensor get used to the room.
- When there is motion, we beep and blink 5 times.
🎯 Try this…
- Add a button to turn the alarm on and off
- Count how many times there was motion and show it in the serial monitor
- Make the alarm sound longer and louder the second time there is motion
🚀 Build this project with Codey: open codey.online/starter-kits/uno/projects/motion-alarm and click MAKE THIS. Then the code and the wiring diagram are ready right away.
Codey Online is a product of OTRONIC