PIR Motion Sensor And Arduino UNO - 2022

Introduction

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

Connection diagram

PIR sensor and Arduino UNO on Wokwi Arduino Simulator
  • Pin 2 of Arduino goes to Digital Output pin of PIR sensor
  • + Pin of PIR sensor goes to 5 V on the UNO
  • - Pin goes to GND 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 LEDint inputPin = 2; // choose the input pin (for PIR sensor)int pirState = LOW; // we start, assuming no motion detectedint val = 0; // variable for reading the pin statusvoid setup() {pinMode(ledPin, OUTPUT); // declare LED as outputpinMode(inputPin, INPUT); // declare sensor as inputSerial.begin(9600);}void loop() {val = digitalRead(inputPin); // read input valueif (val == HIGH) { // check if the input is HIGHdigitalWrite(ledPin, HIGH); // turn LED ONif (pirState == LOW) {// we have just turned onSerial.println("Motion detected!");// We only want to print on the output change, not statepirState = HIGH;}} else {digitalWrite(ledPin, LOW); // turn LED OFFif (pirState == HIGH) {// we have just turned ofSerial.println("Motion ended!");// We only want to print on the output change, not statepirState = 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

Tag » Arduino Ir Motion Sensor Detection