DEV Community

Cover image for Arduino UNO Q - Motion Sensor
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Arduino UNO Q - Motion Sensor

The HC-SR501 is a PIR sensor that detects human or animal movement by sensing changes in infrared radiation, and it's a good first sensor to pair with the Arduino UNO Q. The board's two processors let you read the sensor on the microcontroller side while forwarding alerts over Wi-Fi from the Linux side, for example straight into a Telegram chat.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Arduino UNO Q - Motion Sensor

What you need

Wiring

Arduino UNO Q - Motion Sensor wiring diagram

Connect the sensor's VCC pin to 5V and GND to GND. Connect OUTPUT to pin 2 on the UNO Q. The sensor also has a jumper and two potentiometers on board for adjusting detection range and hold time; leave them at default settings to start.

How it works

The HC-SR501 triggers when something moving emits infrared energy, which is how humans and animals set it off. OUTPUT sits LOW when nothing is moving and switches to HIGH the instant motion is detected, then drops back to LOW once motion stops. One potentiometer sets detection range (clockwise gives roughly 3 meters, counter-clockwise roughly 7 meters), the other sets how long OUTPUT stays HIGH after motion stops (clockwise roughly 5 minutes, counter-clockwise roughly 3 seconds). A trigger-mode jumper controls whether OUTPUT retriggers on continued motion or cycles in short bursts.

Code

On the UNO Q, the sketch runs on the STM32 MCU: it reads pin 2 with digitalRead() in the loop and compares it to the previous reading to catch the LOW-to-HIGH and HIGH-to-LOW transitions, which is where you add your own actions. In the Bridge setup, the Linux side (MPU) can't read the pin directly, so it calls Bridge.call("check_motion") to ask the MCU for the sensor state, and the MCU answers via Bridge.provide(); that's what lets a Python script on the Linux side forward motion events to Telegram over Wi-Fi.

arduino-uno-q-motion-sensor.ino

/*
 * 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
  }
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

Never open /dev/ttyHS1 on the Linux side or Serial1 on the MCU side directly - they're reserved by the Arduino Router for MPU-MCU communication, and touching them breaks the Bridge calls. If motion never registers, check that OUTPUT is wired to pin 2 and that pinMode() set it as INPUT. If it triggers constantly or never seems to reset, the hold-time potentiometer may be turned too far clockwise.

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/arduino-uno-q/arduino-uno-q-motion-sensor

Top comments (0)