Arduino Nano 33 IoT - Motion Sensor

This guide explains how to use the Arduino Nano 33 IoT together with the HC-SR501 motion sensor. In this tutorial, you will learn:

Arduino Nano 33 IoT HC-SR501 PIR Motion Senso

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×HC-SR501 Motion Sensor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter 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.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of HC-SR501 Motion Sensor

HC-SR501 Motion Sensor

HC-SR501 sensor can sense the movement of people or animals. It is often used to tell if someone is there, such as automatically switching lights on or off, starting or stopping an escalator, spotting an intruder, or opening and closing a door.

HC-SR501 Motion Sensor Pinout

The HC-SR501 motion sensor comes with three pins:

  • VCC pin: Hook up this pin to the 5V power supply.
  • GND pin: Connect this pin to the ground (0V).
  • OUTPUT pin: Attach this pin to an input on the Arduino Nano 33 IoT. It sends a signal based on movement:
    • It shows LOW when there is no movement.
    • It shows HIGH when it detects movement.

    The HC-SR501 motion sensor has two knobs and one jumper. These parts let you change the sensor's settings. For more detailed instructions, please check the Advanced Uses guide available at this link.

    HC-SR501 Motion Sensor Pinout

    How HC-SR501 Motion Sensor Works

    The HC-SR501 sensor works by noticing changes in infrared light when an object moves. For the sensor to pick up the object, it must meet two conditions:

    • The object is giving off invisible heat.
    • The object is either moving or trembling.

    This means:

    • If an object sends infrared light but stays still (like a person standing without moving), the sensor will not see it.
    • If an object moves but does not send infrared light (like a robot or a vehicle), the sensor will not see it.

    Animals and people naturally give off heat. So if they are moving, the HC-SR501 sensor can detect them.

    The video above explains how the motion sensor works. In real life, the sensor might behave differently based on its settings. More details can be found in the Advanced Uses section.

    Detecting the Presence of Human

    The sensor doesn't actually detect people. It only picks up any movement. When it senses movement, we assume that someone is there.

    • If you see movement, there are people.
    • If you do not see movement, there are no people.

    There is a problem with this rule. In practice, people are in the sensor’s range but not moving, so their movement is not detected. The Arduino Nano 33 IoT (or MCU) then decides that no person is present.

    However, the sensor is widely used to detect people in many situations because the issue is not very important and the sensor is very affordable.

    Arduino Nano 33 IoT and HC-SR501 Motion Sensor

    When you set a pin on the Arduino Nano 33 IoT as a digital input, it can check whether the connected device is in a LOW or HIGH state.

    Connect a pin on the Arduino Nano 33 IoT to the HC-SR501 sensor's OUTPUT pin. Then, use the Arduino's code to check the sensor’s output value and detect any motion.

Wiring Diagram between HC-SR501 Motion Sensor and Arduino Nano 33 IoT

  • Wiring diagram for powering the Arduino Nano 33 IoT board using a USB port.
The wiring diagram between Arduino Nano and 33 IoT Motion Sensor

This image is created using Fritzing. Click to enlarge image

  • Simple wiring diagram for powering the Arduino Nano 33 IoT board using the Vin pin.
The wiring diagram between Arduino Nano and 33 IoT Motion Sensor  with external power adapter

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

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 33 IoT motion sensor initial setting

How To Program Motion Sensor

  • Set one of the pins on your Arduino Nano 33 IoT board as a digital input using the pinMode() function.
pinMode(PIN_TO_SENSOR, INPUT);
  • Check the sensor's OUTPUT pin using the digitalRead() function.
motion_state = digitalRead(PIN_TO_SENSOR);
  • Detect when movement starts (when the pin changes from off to on)
prev_motion_state = motion_state; // save the previous sensor state motion_state = digitalRead(PIN_TO_SENSOR); // update the current sensor state if (prev_motion_state == LOW && motion_state == HIGH) { // check for a transition from LOW to HIGH Serial.println("Motion detected!"); }
  • Detect when movement stops (when the pin changes from HIGH to LOW)
prev_motion_state = motion_state; // preserve the previous state motion_state = digitalRead(PIN_TO_SENSOR); // obtain the current sensor reading if (prev_motion_state == HIGH && motion_state == LOW) { // detect a change from HIGH to LOW Serial.println("Motion stopped!"); }

Arduino Nano 33 IoT Code

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-motion-sensor */ #define PIN_TO_SENSOR D2 // Arduino Nano 33 IoT pin linked to the sensor's output int motion_state = LOW; // holds the current sensor reading int prev_motion_state = LOW; // retains the previous sensor reading void setup() { Serial.begin(9600); // Start serial communication for the Serial Monitor pinMode(PIN_TO_SENSOR, INPUT); // Set the Nano 33 IoT pin as an input to read the sensor output } void loop() { prev_motion_state = motion_state; // Save the previous state value motion_state = digitalRead(PIN_TO_SENSOR); // Get the current state from the sensor if (prev_motion_state == LOW && motion_state == HIGH) { // Transition detected: LOW to HIGH Serial.println("Motion detected!"); // TODO: activate alarm, lighting, or another device as needed } else if (prev_motion_state == HIGH && motion_state == LOW) { // Transition detected: HIGH to LOW Serial.println("Motion stopped!"); // TODO: deactivate alarm, light, or another device as needed } }

Detailed Instructions

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button on the Arduino IDE to build and install the code on your Arduino Nano 33 IoT board.
  • Open the Serial Monitor in the Arduino IDE.
How to open serial monitor on Arduino IDE
  • Wave your hand in front of the sensor.
  • Look at the output in the 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!