Sign in / Join
arduino-led

How to Make a Simple Motion Sensor Led using Arduino

PIR Motion Sensor

Passive Infrared (PIR) sensors is good for detecting motion. It measures infrared light from objects within its field of view hence it can detect any movements based on the changes in infrared light. It can be used to detect when someone moves in or out of the sensor range. A PIR sensor has two built-in potentiometers; one for adjusting the time delay and another for adjusting sensitivity. The PIR sensor also has three pins;

  • GND: Connects to the ground
  • OUT: Connects to an Arduino digital pin
  • 5V: Connects to 5V

Materials required

  • Arduino Uno
  • 1 LED
  • 1 PIR Motion Sensor (HC-SR501)
  • Jumper cables

Schematic

 

schematic

Connect the setup as shown in the schematic above and then load the program below;

 

/*
Arduino with PIR motion sensor
For complete project details, visit: http://RandomNerdTutorials.com/pirsensor
Modified by Rui Santos based on PIR sensor by Limor Fried
*/

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

void setup() {
pinMode(led, OUTPUT);      // initialize 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(100);                // 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(200);             // delay 200 milliseconds

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

Leave a reply