Arduino Tutorials

How to connect the HC-SR501 motion detector to the Arduino

motion detector with arduino

If you want to detect motion, e.g. to switch on the light, the motion detector HC-SR501 (also called PIR for “passive infrared sensor”) is often a sufficiently good and inexpensive choice.

Primarily, this sensor reacts to heat in the form of infrared rays. To prevent the radiator from switching on the light, the motion detector only reacts to warm objects that are moving. For this purpose a so-called Fresnel lens (the “dome”) is installed above the actual sensor.

Connect the motion detector

Connecting the HC-SR501 is very simple. You only need three pins: Plus (VCC), Minus (GND) and OUT. Connect the latter to a digital pin on your Arduino. When your sensor detects movement, it sends a HIGH to the Arduino, which you can read in your sketch.

At the bottom of the motion detector you will find two potentiometers. Turn the sensor upside down so that the board is facing upwards. On the left potentiometer you can adjust the sensitivity of the sensor; on the right potentiometer you can adjust the length of the signal that is sent to your Arduino. If you set a long time span here, the motion detector will hold the “alarm” so that no further motion can be detected during this time. This is useful if you do not want the same movement to send multiple signals.

The right sketch

For this sketch we have connected a LED to digital pin 9. This LED lights up as soon as motion is detected. The motion detector is connected to digital pin 7.

Besides the settings and specifications for the used pins you only need one variable and one If Statement:

status = digitalRead(sensor);

if (status == HIGH){
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}

The value that the motion detector outputs is stored in the variable status – if there is no motion, this value is 0. If something in front of the sensor moves, this value is 1.

Then the LED lights up when you move. When your motion detector switches back to zero – because it does not detect any movement – it goes off again. That’s it.

Here is the complete sketch:

int led = 9;
int sensor = 7;
int status = 0;

void setup(){
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
}

void loop(){

status = digitalRead(sensor);

if (status == HIGH){
digitalWrite(led, HIGH);
} else {
digitalWrite(led, LOW);
}
}

What next?

Now that you know how to properly connect and use the HC-SR501 motion detector, you can get started on a real project. For example, how about the classic – a light that turns on when you enter a room?

In this project you create a silent alarm that sends a message to your smartphone via Telegram.

By the way, this kind of motion detector is not the only one: In this project you use a microwave radar to find out if you need an umbrella.

You may also like

Comments are closed.