Security Motion Sensor
Code
int inputPin = 8; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int buzz = 10;
int red = 3;
int green = 6;
int blue = 5;
void setup() {
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(buzz, OUTPUT);
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(9600);
}
// function for changing the colour of the RGB LED
void setColour(int r, int g, int b)
{
analogWrite(red, r);
analogWrite(green, g);
analogWrite(blue, b);
}
// function to make the buzzer beep as an alarm
void alarm()
{
analogWrite(buzz, 500);
delay(100);
analogWrite(buzz, 0);
delay(1000);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) // check if the input is HIGH
{
setColour(255, 0, 0); // change the colour of the LED
alarm(); // call the alarm fun
if (pirState == LOW)
{
Serial.println("Motion detected!"); // print on output change
pirState = HIGH;
}
}
else
{
setColour(0, 0, 255); // call the colour function to the LED's colour
if (pirState == HIGH)
{
Serial.println("Motion ended!"); // print on output change
pirState = LOW;
}
}
}
How I made the project
1. connected the PIR sensor to a switch and the arduino
2. connect the three RGB pins of the RGB LED to the arduino and the common anode pin to the other switch through a 330 ohm resistor
3. connect the piezo buzzer to the same switch as the PIR sensor and the arduino
4. connect the 5v pin and the GND pin to the circuit from the arduino
5. Upload the code the arduino
What does this project do?
This project can be used as a security system. Whether it is for home or at work. The PIR sensor is what senses the motion. When motion is detected the RGB LED will turn blue and the piezo buzzer will start to beep loudly. When there is no motion detected the RGB LED will be red.
Downloads
Supplies
1. Breadboard
2. Arduino
3. PIR sensor
4. RGB LED
5.piezo buzzer
6. 2 switches
7. 330 ohm resistor