Passive Infrared Sensor (PIR sensors) find their applications in many fields. The PIR sensor is used to detect motion. Some of the applications of motion detection are automatic lights control (saves Power, gives more lifetime to the LEDs), Burglar detector/intruder detector - which is a basic safety mechanism to detect movement in unattended houses. It is also used in fencing. For examples. pets are not allowed to travel very far from the garden or house.
In this tutorial we will use a PIR sensor to detect motion. Once the motion is detected, we will turn on an LED for a simple demo. You can expand the project based on your needs further.
Components used- Arduino UNO
- PIR sensor
- LED
Pin 2
of Arduino goes toDigital Output
pin of PIR sensor+ Pin
of PIR sensor goes to5 V
on the UNO- Pin
goes toGND
pin of the UNO
you can use the PIR motion sensor and Arduino UNO project page to directly interact with the code, modify it and test it online. No hardware is needed. It is very easy to share the code as well. More information on the PIR sensor is documented on Wokwi Docs page.
The code for PIR sensor and Arduino UNO interface/*
PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop() {
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
Feedback and questions?If you have any questions, please leave a comment here. To discuss with other enthusiast community, I welcome you to Wokwi Discord channel. Also, let me know what would you like to simulate on the Arduino simulator next? Created a project:? - Please share your project here for your fellow readers 😀
Share your interesting projects and browse through several curious projects from fellow developers and makers on Facebook Wokwi Group!
Stay Safe!
Don't stop learning!
#wokwiMakes
Comments
Please log in or sign up to comment.