Difficulty Level: Intermediate
In this tutorial, we’ll create a simple alarm system using Arduino, a PIR motion sensor, a buzzer, and an LED. When motion is detected, the alarm system will activate the buzzer and LED as a visual and audible alert.
Follow the wiring instructions below:
The following code detects motion and triggers the alarm system:
#define PIR_PIN 2 // PIR sensor input pin
#define BUZZER_PIN 8 // Buzzer output pin
#define LED_PIN 7 // LED output pin
void setup() {
pinMode(PIR_PIN, INPUT); // Set PIR sensor as input
pinMode(BUZZER_PIN, OUTPUT); // Set buzzer as output
pinMode(LED_PIN, OUTPUT); // Set LED as output
Serial.begin(9600); // Initialize serial monitor
}
void loop() {
int pirState = digitalRead(PIR_PIN); // Read PIR sensor
if (pirState == HIGH) { // If motion is detected
digitalWrite(BUZZER_PIN, HIGH); // Activate the buzzer
digitalWrite(LED_PIN, HIGH); // Turn on the LED
Serial.println("Motion detected!");
} else {
digitalWrite(BUZZER_PIN, LOW); // Turn off the buzzer
digitalWrite(LED_PIN, LOW); // Turn off the LED
}
delay(500); // Delay between readings
}
You've successfully created a simple alarm system using a PIR sensor, buzzer, and LED. This project can be expanded by adding a keypad to deactivate the alarm, a display to show information, or even connecting it to a Wi-Fi module for remote monitoring.