Hi. This is a new forum, and for now you can post without registration.
Feel free to ask any questions. We are all here to help each other.

Smart Home Automation project competition

Starting with 2018 we will hold quarterly Smart Home Automation project competitions! There will also be prizes for the winners!
Read more in the official Competition topic.

Motion detection with PIR sensor example

All kind of sensors you can use to interact with the enviroment
User avatar
Posts: 47
Joined: Fri Nov 10, 2017 11:56 am
x 3

Motion detection with PIR sensor example

Post by oTp » Mon Nov 20, 2017 8:28 am

Integrating a simple PIR sensor ( like HC-SR501 ) with a microcontroller is quite simple. Either you use an arduino or ESP8266, it basically requires only reading an input pin, or connecting a pin to an interrupt routine.

Here's a cheap PIR sensor : http://www.ebay.co.uk/itm/IR-Pyroelectr ... 2371b40509


PIR stands for Passive InfraRed. It detects changes in infrared light intensity, coming from different angles. Therefore it is only useful for detecting movement of "warm" bodies.


There are only three pins on the sensor. GND, VCC, and OUT. Connect the correct voltage for your sensor. Usually 5V, and connect the DATA pin to an input pin on Arduino, ESP8266, or whatever controller you are using. Don't forget to add a resistor it VCC of PIR is higher than VCC of your microcontroller.

Set detection sensitivity and detection duration with potentiometers on the PIR sensor.


All that is left to configure in software is reading the input pin. Here's how you could do it in Arduino:

Code: Select all

int inPin = 7;     // PIR sesnor connected to digital pin 7
int val = 0;       // variable to store the read value

void setup()
  Serial.begin(9600);      // open the serial port at 9600 bps:  
  pinMode(inPin, INPUT);        // sets the digital pin 7 as input

void loop()
  val = digitalRead(inPin);     // read the input pin
  if(val != 0)
    Serial.println("Motion detected!");