Arduino UNO Q - Motion Sensor

This tutorial shows you how to use an HC-SR501 PIR motion sensor with Arduino UNO Q to detect human motion. You will learn:

Arduino UNO Q Motion Sensor

Hardware Preparation

1×Arduino UNO Q
1×USB Cable for Arduino Uno Q
1×HC-SR501 Motion Sensor
1×Alternatively, AM312 Mini Motion Sensor
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno
1×Recommended: Sensors/Servo Expansion Shield for Arduino Uno
1×Recommended: Breadboard Shield for Arduino Uno
1×Recommended: Enclosure for Arduino Uno
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following kits:

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

The HC-SR501 PIR sensor detects human or animal movement. It is commonly used in lighting automation, door control, escalators, and intruder detection.

Pinout

The HC-SR501 has three pins:

  • GND pin: Connect to GND (0V).
  • VCC pin: Connect to VCC (5V).
  • OUTPUT pin: Sends LOW when no motion, HIGH when motion detected. Connect to an MCU digital input pin.
HC-SR501 Motion Sensor Pinout

The sensor also has one jumper and two potentiometers for adjusting sensitivity and time delay. Start with the default settings — see the Advanced Uses section for details.

How It Works

The HC-SR501 detects movement by sensing changes in infrared radiation. To trigger detection, an object must:

  • Be moving
  • Emit infrared energy (humans and animals do this naturally)

The OUTPUT pin behavior:

  • No motion: OUTPUT is LOW.
  • Motion detected: OUTPUT changes from LOW to HIGH.
  • Motion stops: OUTPUT changes from HIGH to LOW.

Initial Sensor Setting

Time Delay AdjusterScrew fully anti-clockwise (minimum delay).
Detection Range AdjusterScrew fully clockwise (maximum range).
Repeat Trigger SelectorPlace jumper in repeatable trigger mode.
Arduino motion sensor initial setting

Wiring Diagram

The wiring diagram between Arduino UNO Q Motion Sensor

This image is created using Fritzing. Click to enlarge image

MCU Code

The Arduino UNO Q has two processors: the STM32 MCU (handles real-time hardware control) and the Qualcomm MPU (runs Debian Linux). In this section, only the STM32 MCU is programmed — the Linux side stays idle. A later section will show how both processors work together.

/* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-motion-sensor */ #define SENSOR_PIN 2 // The Arduino UNO Q pin connected to the OUTPUT pin of the motion sensor int motion_state = LOW; int prev_motion_state = LOW; void setup() { pinMode(SENSOR_PIN, INPUT); } void loop() { prev_motion_state = motion_state; motion_state = digitalRead(SENSOR_PIN); if (prev_motion_state == LOW && motion_state == HIGH) { // Motion detected — add your action here } else if (prev_motion_state == HIGH && motion_state == LOW) { // Motion stopped — add your action here } }

Detailed Instructions

  • First time with Arduino UNO Q? Follow the Getting Started with Arduino UNO Q tutorial to get your development environment ready before proceeding.
  • Wire the components: Connect VCC → 5V, GNDGND, OUTPUT → pin 2.
  • Connect: Plug the Arduino UNO Q into your computer with a USB-C cable.
  • Open Arduino App Lab: Launch Arduino App Lab and wait until it detects your Arduino UNO Q.
  • Create a new App: Click the Create New App button.
Create New App in Arduino App Lab on Arduino UNO Q
  • Give the App a name, for example: DIYables_MotionSensor
  • Click Create to confirm.
  • You will see a set of folders and files generated inside your new App.
Arduino App Lab App folders and files on Arduino UNO Q
  • Find the sketch/sketch.ino file — this is where you will paste the MCU sketch.
  • Paste the sketch: Copy the MCU code above and paste it into the sketch file. Keep other files as default.
    • Install the library: Click the Add sketch library button (the open book icon with a + sign) in the left sidebar.
    Add sketch library in Arduino App Lab on Arduino UNO Q
    • Search for Arduino_RouterBridge created by Arduino and click the Install button.
    My Apps / DIYables Apps
    Run
    Bricks
    No bricks added...
    Sketch Libraries
    No sketch libra...
    Files
    python
    sketch
    .gitignore
    README.md
    app.yaml
    sketch.ino
    Add sketch library
    Arduino_RouterBridge Arduino

    This library provides a simple RPC bridge for Arduino UNO Q boards, allowing communication between the board and other devices using MsgPack serialization.

    0.4.1
    Install
    More Info
    • Upload: Click the Run button in Arduino App Lab to compile and upload to the STM32.
    Click Run button in Arduino App Lab on Arduino UNO Q
    • Test: Walk in front of the sensor. Use the Bridge section below to see motion events in Monitor.

    Linux + MCU Bridge Programming

    The Arduino UNO Q has two processors that work together: the MPU (Qualcomm, runs Debian Linux) and the MCU (STM32, runs Zephyr OS with your Arduino sketch). They communicate using RPC via the Arduino_RouterBridge library — never via raw serial ports.

    • The motion sensor is connected to the MCU (STM32) — OUTPUT pin on pin 2.
    • The MPU cannot read the sensor directly — it calls Bridge.call("check_motion") on the MCU, which reads the sensor and reports any motion transitions to Monitor.
    • The MPU has Wi-Fi — because the MPU runs full Debian Linux with Wi-Fi, it can forward motion alerts to Telegram.
    • Communication: Bridge.call() on the Linux side invokes Bridge.provide() on the MCU side (since only digitalRead() is used — no hardware output writes)
    • ⚠️ Reserved: /dev/ttyHS1 (Linux) and Serial1 (MCU) are used by the Arduino Router — never open them directly

    In short: MPU polls sensor → MCU reads pin and reports transitions → Monitor shows motion events.

    MCU sketch — motion sensor detection with Bridge:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-motion-sensor */ #include "Arduino_RouterBridge.h" #define SENSOR_PIN 2 int motion_state = LOW; int prev_motion_state = LOW; void check_motion() { prev_motion_state = motion_state; motion_state = digitalRead(SENSOR_PIN); if (prev_motion_state == LOW && motion_state == HIGH) { Monitor.println("Motion detected!"); } else if (prev_motion_state == HIGH && motion_state == LOW) { Monitor.println("Motion stopped!"); } else { Monitor.println(motion_state == HIGH ? "Motion: ACTIVE" : "Motion: none"); } } void setup() { Bridge.begin(); Monitor.begin(); pinMode(SENSOR_PIN, INPUT); Bridge.provide("check_motion", check_motion); Monitor.println("Motion Sensor Bridge ready"); } void loop() {}

    Python script (Arduino App Lab) — poll for motion events every 0.5 seconds:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-motion-sensor */ from arduino.app_utils import * import time def loop(): Bridge.call("check_motion") time.sleep(0.5) App.run(user_loop=loop)
    • Note: Make sure Bridge.begin() is called in the MCU sketch and the sketch is uploaded before running the Python script on the Linux side.
    • ⚠️ Warning: Never directly open /dev/ttyHS1 (on Linux) or use Serial1 (on MCU) in your code — these are reserved by the Arduino Router and accessing them will break the Bridge.

    Detailed Instructions

    • Upload the MCU sketch: Open Arduino App Lab, create a new App, paste the Bridge MCU sketch into sketch/sketch.ino, install the Arduino_RouterBridge library, and click Run.
    • Add the Python script: Paste the Python code above into the Python tab of the same App.
    • Run the App: Click Run — Python polls the motion sensor every 0.5 seconds.
    • Check the console: Open the Console tab → MCU Monitor subtab and walk in front of the sensor.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    Message (Enter to send a message to "Newbiely" on usb(2820070321))
    New Line
    9600 baud
    Motion Sensor Bridge ready Motion detected! Motion stopped! Motion detected! Motion stopped!

    Telegram Integration

    Receive motion alerts via Telegram.

    If you do not have a Telegram bot yet, see How to Create a Telegram Bot to get your bot token before continuing.

    MCU sketch: Keep the same MCU sketch from the previous Bridge section — no changes needed. Make sure it is already uploaded and running on the STM32 before proceeding.

    Python script (Arduino App Lab) — Telegram bot for motion detection:

    /* * This Arduino UNO Q code was developed by newbiely.com * * This Arduino UNO Q code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-motion-sensor */ from arduino.app_utils import * import requests import time BOT_TOKEN = "YOUR_BOT_TOKEN" API_URL = f"https://api.telegram.org/bot{BOT_TOKEN}" last_update_id = 0 def send_message(chat_id, text): requests.post(f"{API_URL}/sendMessage", json={"chat_id": chat_id, "text": text}) def get_updates(): global last_update_id resp = requests.get(f"{API_URL}/getUpdates", params={"offset": last_update_id + 1, "timeout": 5}) return resp.json().get("result", []) def loop(): global last_update_id updates = get_updates() for update in updates: last_update_id = update["update_id"] msg = update.get("message", {}) chat_id = msg.get("chat", {}).get("id") text = msg.get("text", "").strip() if text == "/status": status = Bridge.call("check_motion") send_message(chat_id, status if status else "No motion state change since last check.") else: send_message(chat_id, "Commands:\n/status — check current motion sensor state") time.sleep(0.5) App.run(user_loop=loop)
    • Note: Replace YOUR_BOT_TOKEN with the token obtained from @BotFather on Telegram.
    • Send /status to trigger a manual check of the motion sensor state.

    Detailed Instructions

    • Upload the MCU sketch: Use the Bridge MCU sketch from the previous section (upload it first if not already done).
    • Paste the Telegram script: Copy the Python code above into the Python tab of your App in Arduino App Lab.
    • Set your token: Replace YOUR_BOT_TOKEN in the script with your actual bot token.
    • Run the App: Click Run — the bot starts listening for Telegram messages.
    • Test it: Send /status — the bot replies with the current motion state.

    App Lab Console Output

    DIYables_Apps
    Stop
    sketch.ino
    1#include "Arduino_RouterBridge.h"
    Serial Monitor
    Python
    [2026-04-29 12:00:01] Telegram: /status [2026-04-29 12:00:01] Motion: none [2026-04-29 12:01:30] Telegram: /status [2026-04-29 12:01:30] Motion detected!
    Telegram
    Telegram 12:45
    Welcome to Telegram!
    ArduinoBot 10:19
    Chatting with Arduino...
    telegram-botfather
    BotFather Yesterday
    Your bot has been created.

    ArduinoBot

    bot
    Today
    /status
    10:15 AM ✓✓
    Motion: none
    10:16 AM
    /status
    10:17 AM ✓✓
    Motion detected!
    10:18 AM

    OpenClaw Integration

    OpenClaw integration for Arduino UNO Q motion sensor is coming soon.

    • Coming Soon: OpenClaw support for this project on Arduino UNO Q will be covered in a future update.

    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.

    Detection Range Adjuster

    Adjusts the detection distance (approximately 3–7 meters):

    • Fully clockwise → ~3 meters.
    • Fully counter-clockwise → ~7 meters.
    Motion Sensor Detection Range

    Time Delay Adjuster

    Adjusts the hold time after motion stops:

    • Fully clockwise → ~5 minutes.
    • Fully counter-clockwise → ~3 seconds.
    Motion sensor adjust time delay

    Repeat Trigger Selector

    Motion sensor trigger selection
    • Single trigger mode: OUTPUT goes HIGH for time_delay, then LOW for 3 seconds, repeating while motion continues.
    • Repeatable trigger mode: OUTPUT stays HIGH for the full duration of motion plus time_delay. Recommended for most applications.

    ※ NOTE THAT:

    Use repeatable trigger mode for most applications. In practical use:

    • Devices turn ON when a person is detected.
    • Devices turn OFF after a delay once the person leaves.

    Application/Project Ideas

    • Smart lighting: Turn lights on when someone enters a room, off after they leave
    • Security alert: Send a Telegram message whenever motion is detected
    • Occupancy counter: Count how many times motion starts per hour
    • Energy saver: Power down appliances after no motion is detected for several minutes

    Challenge Yourself

    • Easy: Make an LED blink three times whenever motion is detected
    • Medium: Log motion events with timestamps to a file on the MPU
    • Advanced: Send a Telegram notification automatically (without needing /status) whenever motion is detected

    Function References

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