Arduino Uno PIR sensor security detection system

Description of the Arduino Motion Detection Code

This Arduino code implements a simple motion detection system using a sensor connected to digital pin 2 and an LED connected to pin 13. The program initializes the LED as an output and the sensor as an input, allowing it to read motion status. When motion is detected (the sensor state is HIGH), the LED lights up, and a message is printed to the Serial Monitor indicating that motion has been detected. If motion stops (the sensor state is LOW), the LED turns off, and a message is printed to indicate that motion has ceased.

The code uses a variable state to track the current motion status, ensuring that messages are printed only when the state changes. This provides a clear indication of motion events without flooding the Serial Monitor with repeated messages during continuous motion detection.

This project is an excellent introduction to using Arduino for motion detection and LED control, making it suitable for beginners interested in basic electronics and programming.



schematic
SCHEMATIC:

CODE:

int led = 13; // the pin that the LED is atteched to
int sensor = 2; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)

void setup() {
pinMode(led, OUTPUT); // initalize LED as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}

void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(led, HIGH); // turn LED ON
delay(500); // delay 100 milliseconds

if (state == LOW) {
  Serial.println("Motion detected!"); 
  state = HIGH;       // update variable state to HIGH
}

}
else {
digitalWrite(led, LOW); // turn LED OFF
delay(500); // delay 200 milliseconds

  if (state == HIGH){
    Serial.println("Motion stopped!");
    state = LOW;       // update variable state to LOW
}

}
}

  • Arduino projects
  • Motion detection tutorial
  • LED control with Arduino
  • Digital sensor interfacing
  • Arduino programming for beginners
  • DIY electronics projects
  • Sensor-based projects
  • Serial communication with Arduino
  • Basic electronics projects
  • Motion sensor applications

Post a Comment

0 Comments