Motion detection with Arduino and HC-SR501

Motion detection with Arduino

The example shows how we can detect motion using the PIR sensor in combination with the Arduino microcontroller.


In this example we used the following elements:


Connect the sensor with Arduino:

The module communicates with the bus has a single signal output. Connect to Arduino as follows:


Module PIRPin Arduino



 The connection scheme of sensor with the Arduino Uno.


Program for Arduino

In the example, we used the following code:


int sensor = 8; //pin 8 is connected to the signal from the sensor 

void setup(){ 
 Serial.begin(9600); //initialize serial monitor 
 pinMode(sensor, INPUT); //setting Arduino pin as an input 

 Serial.println ("---- TEST MOTION SENSOR ----"); 

void loop(){ 
 int movement = digitalRead(sensor); //read the value from the sensor 
 if(motion == HIGH) //display information on the serial monitor 
 { //high means motion detection, low - lack of movement 
 Serial.println("MOTION DETECTED!"); 
 else {Serial.println("no motion");} 

 delay(200); //delay between consecutive readings 


The results of the program can be seen below:


A screenshot of the serial monitor.