Arduino MKR WiFi 1010 - Motion Sensor

#UPDATE_NOTE: REWRITTEN, CHECKED_CODE

Detect movement and build smart automation with your Arduino MKR WiFi 1010 and HC-SR501 motion sensor! This tutorial teaches you how to use PIR motion sensors with Arduino MKR WiFi 1010 for security systems, automatic lighting, and presence detection. Learn how to wire the HC-SR501 sensor to your Arduino MKR WiFi 1010, read motion signals, detect when movement starts and stops, and trigger actions based on motion detection. Perfect for building Arduino MKR WiFi 1010 projects like automatic lights, security alarms, and energy-saving systems.

What You'll Learn

Real-World Applications

Motion sensors are essential for Arduino MKR WiFi 1010 automation and security projects!

Arduino MKR WiFi 1010 HC-SR501 PIR Motion Senso

Hardware Preparation

1×Arduino MKR WiFi 1010
1×Micro USB Cable
1×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack

Or you can buy the following 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

The HC-SR501 is a PIR (Passive Infrared) motion sensor that detects movement by sensing changes in infrared radiation. It's commonly used in automatic lighting, security systems, and presence detection applications.

HC-SR501 Motion Sensor

HC-SR501 Motion Sensor Pinout

The HC-SR501 has three pins:

  • VCC pin: Connect to 5V power supply
  • GND pin: Connect to ground (0V)
  • OUTPUT pin: Connect to any digital input pin on your Arduino MKR WiFi 1010
    • LOW when no motion detected
    • HIGH when motion detected

    The sensor also features two adjustment potentiometers and a jumper for configuring sensitivity and timing. See the Advanced Uses section for detailed adjustment instructions.

    HC-SR501 Motion Sensor Pinout

    How HC-SR501 Motion Sensor Works

    The HC-SR501 detects motion by sensing changes in infrared (heat) radiation. For successful detection, two conditions must be met:

    • The object emits infrared radiation (body heat)
    • The object is moving or changing position

    This means:

    • A person standing completely still won't be detected (no movement)
    • A cold robot moving won't be detected (no heat signature)
    • A moving person or animal will be detected (both heat and movement)

    Important Note: The sensor detects movement, not specifically humans. When motion is detected, we infer human presence. This can lead to false negatives when people are present but stationary. However, the sensor's affordability and reliability make it excellent for most applications where occasional false negatives are acceptable.

    Arduino MKR WiFi 1010 and HC-SR501 Motion Sensor

    Reading the motion sensor with your Arduino MKR WiFi 1010 is simple! Configure a digital input pin, connect it to the sensor's OUTPUT pin, and use digitalRead() to check for HIGH (motion detected) or LOW (no motion) states.

Wiring Diagram - Two Power Options

The wiring diagram between Arduino MKR WiFi 1010 Motion Sensor

This image is created using Fritzing. Click to enlarge image

Wiring Table

HC-SR501 Motion Sensor Arduino MKR WiFi 1010
VCC 5V
GND GND
OUTPUT D18

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 MKR WiFi 1010 motion sensor initial setting

How To Program Motion Sensor

  • Set one of the pins on your Arduino MKR WiFi 1010 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 MKR WiFi 1010 Code

/* * This Arduino MKR WiFi 1010 code was developed by newbiely.com * * This Arduino MKR WiFi 1010 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mkr/arduino-mkr-wifi-1010-motion-sensor */ #define PIN_TO_SENSOR D18 // Arduino MKR WiFi 1010 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

New to Arduino MKR WiFi 1010? Complete our Getting Started with Arduino MKR WiFi 1010 tutorial first to set up your development environment.

  • Wire the components according to the diagram above
  • Connect the Arduino MKR WiFi 1010 to your computer via USB cable
  • Open Arduino IDE and select the correct board and COM port
  • Copy the code and upload it to your board
  • Open the Serial Monitor
How to open serial monitor on Arduino IDE
  • Wave your hand in front of the motion sensor
  • View motion detection results on the Serial Monitor:
COM6
Send
Motion detected! Motion stopped!
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Challenge Yourself

Ready to take your motion sensing skills further? Try these exciting projects:

  • Automatic Room Lights: Make LEDs or relays turn lights on when motion is detected and off after no motion for 30 seconds
  • Security Alarm System: Trigger a buzzer and send WiFi alerts when motion is detected in restricted areas
  • People Counter: Count how many times people enter/exit a room using motion detection timing
  • Smart Fan Controller: Automatically turn on a fan when someone enters and turn it off when they leave
  • Multi-Zone Detection: Use multiple motion sensors to track movement through different rooms
  • Pet Activity Monitor: Log your pet's activity patterns throughout the day using motion detection
  • Energy-Saving System: Build a comprehensive system that controls lights, fans, and displays based on presence detection

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!