Enrg Tech
Published © Apache-2.0

How to make a simple Motion Sensor Led Using Arduino

Passive Infrared (PIR) sensors is good for detecting motion. It measures infrared light from objects within its field of view hence..

AdvancedFull instructions provided2 hours450
How to make a simple Motion Sensor Led Using Arduino

Story

Read more

Custom parts and enclosures

schematic_VdkUnbs8QQ.jpg

Schematics

schematic_1szhTI6y40.jpg

Code

Untitled file

C/C++
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
}
}
}

Credits

Enrg Tech
2 projects • 0 followers
Enrg Tech is UK’s leading distributor of electronic components. Has millions of electronic components and offers same day shipping.
Contact

Comments

Please log in or sign up to comment.