This tutorial we will demonstrate how to detect motion from PIR Sensor using Arduino, ones the PIR detect a motion it will trigger the LED as the output.
Electric Parts Require
- 1x Arduino
- 1x PIR Motion Sensor
- 1x LED
- 1x Jumper Cable
Please follow this schematics below to setup this lab testing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* PIR Motion Sensor with Arudino Demo Code */ int led = 13; // Define the LED as Pin 13 int sensor = 4; // Define the Sensor Connected to Pin 4 int state = LOW; // Motion Detection int val = 0; // Store the value of sensor void setup() { pinMode(led, OUTPUT); // initialize the LED as the output pinMode(sensor, INPUT); // initialize the sensor as the input Serial.begin(9600); // Define the serial communication } void loop(){ val = digitalRead(sensor); // Reading the sensor value if (val == HIGH) { // if sensor is high digitalWrite(led, HIGH); // switch on the LED delay(100); // 100 milliseconds delay if (state == LOW) { Serial.println("Motion was detected"); state = HIGH; // Update the variable state in to HIGH } } else { digitalWrite(led, LOW); // Turning off the LED delay(200); // 200 milliseconds delay if (state == HIGH){ Serial.println("Motion stopped!"); state = LOW; // update the variable state into LOW } } } |
Working on PIR Motion Sensor with Arudino