Hi guys in this tutorial we will see how to use motion detection sensor in Arduino & interface with LED light. So lets get started.

PIR sensor

This is PIR sensor this is also called motion detection sensor. we can use this sensor In home automation like light off automatically when there is no one in the room & light ON when someone enters the room. It can be also used for counting people or object I will show you in the next tutorial for using PIR sensor as a counter. For this you will need

  1. PIR sensor (HC-SR501),
  2. Arduino,
  3. Breadboard, (Optional)
  4. Jumper Wire,
  5. LED.

To see which pin is for VCC & GND. Just remove the white cover you can see there is pin name & in some sensor it is printed on the bottom side of circuit so make sure which in VCC & GND and middle one is for output.

PIR has a trimpot which can be use for adjusting sensitivity & trigger time. If PIR sensor is not working or LED is continuously blinking or LED is ON so adjust the sensitivity & time trigger trim port.

Do connection as shown in diagram.

Sketch:
int ledPin = 13;                // choose the pin for the LED
int inputPin = 3;               // 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;
      delay(200);
    }
  } 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;
    }
  }
}
Explanation:

We will set the pin number 13 for LED, input pin which is our PIR sensor output pin. When Arduino start there is no motion therefore pirstate is low. Val which is variable for reading pin PIR sensor initially at zero that is low.

int ledPin = 13; // choose the pin for the LED
int inputPin = 3; // 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

Pinmode declare the LED as output, sensor as input. Serial.begin will initialize the communication between computer & Arduino.

Val value equal to sensor input value. If it is high LED light will on. If pirstate value is low. it will print motion detected. And set pirstate equal to high.

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;
delay(200);
}

To prevent printing motion detected or motion ended repetitively. We want only output when it is detected it will show on serial monitor. If val is LOW it will turn off light. It will print motion ended.

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;

Upload the sketch. Go to tool & serial monitor. When I move my hand over sensor it will show motion detected when hand passed the sensor it shows motion ended.

Till then keep learning keep making.


2 Comments

How to use buzzer with PIR sensor – Robotica DIY · July 21, 2019 at 12:48 pm

[…] you have not watched my previous turorial for PIR sensor in Arduino & PIR sensor as a counter. Do connection as shown in […]

How to use motion sensor as a counter with Arduino – Robotica DIY · July 21, 2019 at 1:03 pm

[…] is almost same with some part of coding added for counter. You can see previous tutorial for PIR sensor interface with LED. Do connection as shown in […]

Comments are closed.