Contents

Interfacing HC-SR501 PIR Sensor with Arduino

HC-SR501 PIR Sensor Features

The HC-SR501 Infrared Motion Sensor is an effective, inexpensive module that can detect motion in the environment. The small size and physical design of this module allows you to easily use it in your project.

The output of this sensor can be connected directly to one of the Arduino digital pins, and if any movement is detected by the sensor, the value of this pin would be 1. The two potentiometers on the board allow you to adjust the sensitivity and waiting time for delay after detecting a move.

HC-SR501 PIR Sensor Pinout

This module has 3 pins:

  •  VCC: Module power supply – 5V
  •  GND: Ground
  • OUT: Output digital data to microcontroller

You can see pinout of this module in the image below.

You can download the datasheet of this module here.

Required Materials

Hardware Components

Arduino Nano × 1
HC-SR501 Sensor × 1
female-female-jumper-wire × 1

Software Apps

Arduino IDE

Interfacing HC-SR501 PIR Sensor with Arduino

Step 1: Circuit

The following circuit shows how you should connect STM32 or Arduino to HC-SR501 sensor. Connect wires accordingly.

Step 2: Code

Upload the following code to Arduino.
/*
  by MohammedDamirchi
  
Home
*/ //for stm32 //const int PIR = PB12; //const int LED = PC13; //for Arduino const int PIR = 2; const int LED = LED_BUILTIN; bool PIRstate = 0; void setup() { pinMode(PIR, INPUT); pinMode(LED, OUTPUT); } void loop() { PIRstate = digitalRead(PIR); if (PIRstate == HIGH) { // turn LED on: digitalWrite(LED, HIGH); } else { // turn LED off: digitalWrite(LED, LOW); } }
After uploading the code, if you move in front of the sensor, the onboard LED will turn on.
Liked What You See?​
Get Updates And Learn From The Best​

Leave a Reply

Your email address will not be published. Required fields are marked *