You are on page 1of 1

/*

Arduino with PIR motion sensor


For complete project details, visit: http://RandomNerdTutorials.com/pirsenso
r
Modified by Rui Santos based on PIR sensor by Limor Fried
*/
int
int
int
int

led = 13;
sensor = 2;
state = LOW;
val = 0;

void setup() {
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
}
void loop(){
val = digitalRead(sensor);
if (val == HIGH) {
digitalWrite(led, HIGH);
delay(100);

//
//
//
//

the pin that the LED is atteched to


the pin that the sensor is atteched to
by default, no motion detected
variable to store the sensor status (value)

// initalize LED as an output


// initialize sensor as an input
// initialize serial

//
//
//
//

read sensor value


check if the sensor is HIGH
turn LED ON
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
}
}
}

You might also like