Arduino + motion sensor HC-SR501 (PIR)

HC-SR501 – is a module consisting of a PIR-sensor (Pyroelectric (Passive) InfraRed sensor) and the control circuit (Figure 1).

Figure 1 – Appearance sensor HC-SR501

Parameters of motion sensor is provided in Table 1.


Let’s write a program to Arduino, which will read the motion sensor and if there is movement, display it on your computer and turn on the standard Arduino LED (pin 13).

//VSS, OUT, GND => 5VC, Digital pin 7, GND

int pirPin = 7;               //contact to connect the sensor to Arduino
int ledPin = 13;              //using a standard LED
int val = 0;                  //variable to store the state of the sensor

void setup() {
Serial.begin(9600);           //open a session with the computer
pinMode(pirPin, INPUT);       //determine the input pin for the sensor
pinMode(ledPin, OUTPUT);      //determine the contact with the LED as output

void loop() {
val = digitalRead(pirPin);    //read the state of motion sensor
if (val == HIGH) {            //if there is movement
digitalWrite(ledPin, HIGH);   //turn on LED
Serial.println("Motion!");    //sent to computer "Motion!"
else {
digitalWrite(ledPin, LOW);    //otherwise turn off LED
Serial.println("No motion");  //and sent to computer "No motion"
delay(1000);                  //wait a second

2014-08-15 21.13.49

In Arduino IDE open Tools/Serial Monitor


Добавить комментарий

Этот сайт использует Akismet для борьбы со спамом. Узнайте как обрабатываются ваши данные комментариев.

Закрыть меню