Arduino Nano ESP32 - Motion Sensor

This tutorial provides instructions on how to use Arduino Nano ESP32 with HC-SR501 motion sensor. In detail, we will learn:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×HC-SR501 Motion Sensor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of HC-SR501 Motion Sensor

HC-SR501 Motion Sensor

HC-SR501 PIR sensor is a sensor that can detect the movement of humans (or animals). It's widely used to detect the presence of humans in many applications (automatically turning ON/OFF light bulb, activating/deactivating escalator, detecting an intruder, opening/closing the door...)

HC-SR501 Motion Sensor Pinout

The HC-SR501 motion sensor has 3 pins:

  • VCC pin: connect this pin to VCC (5V)
  • GND pin: connect this pin to GND (0V)
  • OUTPUT pin: connect this pin to ESP32's input pin. This pin outputs the signal corresponds to the motion deltection:
    • LOW if no motion is detected
    • HIGH if motion is detected.

    There are also two potentiometers and one jumper on the HC-SR501 motion sensor. These potentiometers and jupperare used to adjust the sensor's setting. The detailed instruction is described in the Advanced Uses.

    HC-SR501 Motion Sensor Pinout

    How HC-SR501 Motion Sensor Works

    The working principle of HC-SR501 sensor is based on the change of the infrared radiation on the moving object. To be detected by the HC-SR501 sensor, the object must meet two requirements:

    • The object is emitting the infrared way.
    • The object is moving or shaking

    So:

    • If an object is emitting the infrared ray but NOT moving (e.g, a person stands still without moving), it is NOT detected by the sensor.
    • If an object is moving but NOT emitting the infrared ray (e.g, robot or vehicle), it is NOT detected by the sensor.

    The animals and humans emit infrared ray naturally. Therefore, the animals and humans can be delected by the HC-SR501 sensor if they is moving. .

    The above video shows the working principle of the motion sensor. In practice, the motion sensor can works differently according to the sensor setting (described in the Advanced Uses section)

    Detecting the Presence of Human

    The sensor itself does NOT directly detect the presence of humans, the sensor just detects the movement. And then the presence of humans is infered on movement detection:

    • If the movement is detected, the humans are present
    • If the movement is NOT detected, the humans are NOT present

    There is an issue with this rule in pracice, the humans are present in sensor range but NOT moving. The the movement is NOT detected. The Arduino Nano ESP32 (or MCU) deduces that human is NOT present.

    However, The sensor's widely used to detect the human in many applications because this issue is NOT serious and the sensor's price is cheap.

    Arduino Nano ESP32 and HC-SR501 Motion Sensor

    When an ESP32's pin is configured as a digital input, It can read the state (LOW or HIGH) of anything it is connected to.

    By connecting the ESP32's pin to the OUTPUT pin of the HC-SR501 sensor, we can use the Arduino Nano ESP32 code to read the value of the OUTPUT pin, and then infer the motion.

Wiring Diagram between HC-SR501 Motion Sensor and Arduino Nano ESP32

  • The wiring diagram when powering the Arduino Nano ESP32 board via USB port.
The wiring diagram between Arduino Nano ESP32 and Motion Sensor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram when powering the Arduino Nano ESP32 board via Vin pin.
The wiring diagram between Arduino Nano ESP32 and Motion Sensor  with external power adapter

This image is created using Fritzing. Click to enlarge image

Initial Setting

Detection Range Adjuster Fully screw it in the clockwise direction.
Time Delay Adjuster Fully screw it in the anti-clockwise direction.
Repeat Trigger Selector Put jumper like the below image.
Arduino Nano ESP32 motion sensor initial setting

How To Program Motion Sensor

  • Configure an ESP32's pin to the digital input mode by using pinMode() function
pinMode(PIN_TO_SENSOR, INPUT);
  • Read the state of sensor's OUTPUT pin by using digitalRead() function.
motion_state = digitalRead(PIN_TO_SENSOR);
  • Detect motion start (pin's state change from LOW to HIGH)
prev_motion_state = motion_state; // store old state motion_state = digitalRead(PIN_TO_SENSOR); // read new state if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); }
  • Detect motion stop (pin's state change from HIGH to LOW)
prev_motion_state = motion_state; // store old state motion_state = digitalRead(PIN_TO_SENSOR); // read new state if (prev_motion_state == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); }

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-motion-sensor */ #define PIN_TO_SENSOR D2 // The Arduino Nano ESP32 pin connected to OUTPUT pin of sensor int motion_state = LOW; // current state of pin int prev_motion_state = LOW; // previous state of pin void setup() { Serial.begin(9600); // Initialize the Serial to communicate with the Serial Monitor. pinMode(PIN_TO_SENSOR, INPUT); // set Arduino Nano ESP32 pin to input mode to read value from OUTPUT pin of sensor } void loop() { prev_motion_state = motion_state; // store old state motion_state = digitalRead(PIN_TO_SENSOR); // read new state if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); // TODO: turn on alarm, light or activate a device ... here } else if (prev_motion_state == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); // TODO: turn off alarm, light or deactivate a device ... here } }

Detailed Instructions

How to open serial monitor on Arduino IDE
  • Move your hand in front of sensor range
  • See the output in Serial Monitor
COM6
Send
Motion detected! Motion stopped!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Language References

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!