Arduino UNO R4 - Motion Sensor

This tutorial instructs you how to use the HC-SR501 motion sensor and Arduino UNO R4 to detect the human. In detail, we will learn:

Arduino UNO R4 motion sensor

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×HC-SR501 Motion Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

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

The HC-SR501 PIR sensor detects human or animal movement. It is commonly used in various applications such as turning lights on and off, opening and closing doors, operating escalators, and detecting intruders.

When you go to places where doors open and close by themselves, where lights turn on and off automatically, or where escalators start by themselves, have you ever wondered, "How does this happen?" If you have, this guide will not only explain how it works but also show you how to create it yourself. Let's begin!

Pinout

The HC-SR501 motion sensor comes with three pins:

  • GND pin: connect to GND (0V)
  • VCC pin: connect to VCC (5V)
  • OUTPUT pin: sends LOW when no motion, HIGH when motion. Connect this pin to Arduino UNN R4's input pin.

The HC-SR501 includes one jumper and two potentiometers, which help adjust the sensor's settings. Start with the default setting. You can find more information in the "Advanced Uses" section. Advanced Uses

HC-SR501 Motion Sensor Pinout

How It Works

The HC-SR501 sensor senses movement by noting changes in infrared light from the object that moves. For the HC-SR501 sensor to detect an object, it must satisfy two conditions:

  • Is shaking or moving
  • Is releasing infrared energy.

Therefore:

  • If an object moves without giving off infrared rays (like a robot or toy car), the sensor will not detect it.
  • If an object gives off infrared rays but does not move (like a person standing still), the sensor will not detect it.

Humans and animals naturally give off infrared. Because of this, the sensor can sense when humans and animals move.

The state of the OUTPUT pin:

  • If no person or animal is moving within the sensor's range, the sensor's OUTPUT pin is LOW.
  • If a person or animal enters the sensor's range, the OUTPUT pin changes from LOW to HIGH, indicating motion detection.
  • If a person or animal leaves the sensor's range, the OUTPUT pin changes from HIGH to LOW, indicating the end of motion.

The video shows the basic idea of how the motion sensor operates. In real situations, the way it functions can vary slightly based on its settings, as explained in the Advanced Uses section.

Arduino UNO R4 - HC-SR501 Motion Sensor

When you set a pin on the Arduino UNO R4 as a digital input, it can detect whether the connected item is in a LOW or HIGH state.

Connect the Arduino UNO R4's pin to the OUTPUT pin of the HC-SR501 sensor. Then, use the Arduino UNO R4 code to read the OUTPUT pin's value to detect motion.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Motion Sensor

This image is created using Fritzing. Click to enlarge image

Initial Setting

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
arduino motion sensor initial setting

How To Program For Motion Sensor

  • Set an Arduino UNO R4 pin to digital input mode using the pinMode() function.
pinMode(SENSOR_PIN, INPUT);
  • Use the digitalRead() function to check the status of the sensor's OUTPUT pin.
motion_state = digitalRead(SENSOR_PIN);
  • Detect when motion begins (the pin changes state from LOW to HIGH).
prev_motion_state = motion_state; // Store the previous state of the pin motion_state = digitalRead(SENSOR_PIN); // Read the current state of the pin if (prev_motion_state == LOW && motion_state == HIGH) { // Check for transition from LOW to HIGH Serial.println("Motion detected!"); }
  • Detect when motion stops (pin's status changes from HIGH to LOW).
prev_motion_state = motion_state; // Preserve the previous state of the pin motion_state = digitalRead(SENSOR_PIN); // Update the current state from sensor input if (prev_motion_state == HIGH && motion_state == LOW) { // Detect transition from HIGH to LOW Serial.println("Motion stopped!"); // Output message when motion stops }

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-motion-sensor */ #define SENSOR_PIN 2 // The Arduino UNO R4 connected to the output pin of the motion sensor int motion_state = LOW; // Initialize current pin state to LOW int prev_motion_state = LOW; // Initialize previous pin state to LOW void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate pinMode(SENSOR_PIN, INPUT); // Configure pin 2 as an input for the sensor } void loop() { prev_motion_state = motion_state; // Assign the current pin state to the previous pin state for comparison motion_state = digitalRead(SENSOR_PIN); // Read the current state from the sensor output // Check for transition from LOW to HIGH which indicates motion detected if (prev_motion_state == LOW && motion_state == HIGH) { Serial.println("Motion detected!"); // Print message when motion is detected // Optional: Additional actions when motion is detected } // Check for transition from HIGH to LOW which indicates motion has stopped else if (prev_motion_state == HIGH && motion_state == LOW) { Serial.println("Motion stopped!"); // Print message when motion has stopped // Optional: Additional actions when motion stops } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it using Arduino IDE
  • Press the Upload button in Arduino IDE to upload the code to Arduino UNO R4
  • Open the Serial Monitor
  • Move your hand in front of the sensor
  • Check the output on the Serial Monitor
COM6
Send
Motion detected! Motion stopped!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Advanced Uses

This section includes advanced information that may be overwhelming. If you are unsure about the content, feel free to skip it and move on to the next sections.

We can adjust the settings of the sensor using one jumper and two potentiometers, as described above.

Detection Range Adjuster

This potentiometer adjusts the detection distance (about 3 meters to 7 meters).

  • When fully turned clockwise, the detection range is about 3 meters.
  • When fully turned anti-clockwise, the detection range is about 7 meters.
Motion Sensor Detection Range

We can change the potentiometer settings to get the range we want, between 3 meters and 7 meters.

Time Delay Adjuster

This potentiometer adjusts the time delay.

  • If you turn it all the way clockwise, the time delay will be about 5 minutes.
  • If you turn it all the way counter-clockwise, the time delay will be about 3 seconds.

Next, we'll explain what time delay means and how it relates to Repeat Trigger.

motion sensor adjust time delay

Repeat Trigger Selector

This jumper lets you pick a trigger mode: single trigger or repeatable trigger.

motion sensor trigger selection

We'll refer to the time delay setting (adjusted by the Time Delay Adjuster) as time_delay. Imagine you continue to move within the sensor's detection area for a considerable period, which we’ll call motion_time (much longer than time_delay).

  • In single trigger mode, the OUTPUT pin switches between LOW and HIGH multiple times. It stays HIGH for a period equal to "time_delay" and LOW for a fixed time of 3 seconds.
motion sensor single trigger mode
  • The OUTPUT pin stays HIGH for the duration of the motion time plus the time delay.
motion sensor repeatable trigger mode

Testing

Let's test the trigger modes. Turn the Time Delay Adjuster completely to the left to set the delay time to 3 seconds.

  • Single Trigger Mode:
    • Place the jumper to select the single trigger mode.
    • Wave your hand in front of the sensor continuously for 10 seconds.
    • Remove your hand from the sensor's range.
    • Wait for 3 seconds; then check the serial monitor for the output as shown.
    COM6
    Send
    Motion detected! Motion stopped! Motion detected! Motion stopped! Motion detected! Motion stopped!
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
    • Repeatable Trigger Mode:
      • Place the jumper to activate the Repeatable Trigger mode.
      • Move your hand continuously in front of the sensor for about 10 seconds.
      • Remove your hand from the sensor's range.
      • After waiting for 3 seconds, check the serial monitor to see the output.
      COM6
      Send
      Motion detected! Motion stopped!
      Autoscroll Show timestamp
      Clear output
      9600 baud  
      Newline  

      In single trigger mode, the sensor activates two or three times. In repeatable trigger mode, it activates only once.

      ※ NOTE THAT:

      During the LOW period, which lasts 3 seconds, the sensor cannot detect any motion. This means the sensor is inactive during this time, but this does not usually cause any problems.

      It is advised to use the repeatable trigger mode.

      In practical applications:

      • Devices or machines are often turned on when a person is detected nearby.
      • Devices or machines are not turned off as soon as the person leaves. They are turned off after a certain delay.

Function 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!