DEV Community

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

Posted on Originally published at newbiely.com

Arduino UNO Q - Motion Sensor - Relay

This build wires an HC-SR501 motion sensor to a relay through an Arduino UNO Q, so the relay switches on the moment someone walks into range and switches off again once they leave. It's a clean pattern for a developer to pick up: a digital sensor input driving a digital output, with the UNO Q's dual processors ready to add a Telegram bot later that reports the relay state over Wi-Fi.

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

What you need

Wiring

Arduino UNO Q - Motion Sensor - Relay wiring diagram

Connect the motion sensor's OUTPUT pin to pin 7 on the Arduino UNO Q, and the relay's IN pin to pin 9. Loose wires are a common source of flaky behavior, so it helps to plug a Screw Shield V2 or Proto Screw Terminal Block Shield into the UNO Q and screw each wire into its matching terminal. The pin numbers don't change, only the connection method gets sturdier.

How it works

The HC-SR501 outputs HIGH while it detects motion and LOW once things go still, so digitalRead() on pin 7 is all you need to know someone's there. The relay is just an electrically controlled switch: driving pin 9 HIGH with digitalWrite() closes it and lets it power something bigger than the Arduino could drive on its own. On the UNO Q, both parts connect to the STM32 MCU, while the separate Linux-based MPU can later call Bridge.call("check_motion") to ask the MCU to check the sensor and update the relay, and since the MPU has Wi-Fi it can forward that status to Telegram.

Code

The sketch sets pin 7 as an input and pin 9 as an output, turning the relay off at startup. In the loop, it reads the sensor and compares the new reading to the previous one: a LOW-to-HIGH transition means motion just started, so it turns the relay on, and a HIGH-to-LOW transition means motion stopped, so it turns the relay off. The Bridge version keeps this same logic on the MCU but exposes it through Bridge.provide_safe() so the Linux side can trigger a check and a Python script can relay the result to a Telegram bot, including a manual /status command.

arduino-uno-q-motion-sensor-relay.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-relay
 */

#define MOTION_SENSOR_PIN 7  // The Arduino UNO Q pin connected to the OUTPUT pin of the motion sensor
#define RELAY_PIN         9  // The Arduino UNO Q pin connected to the IN pin of the relay

int motion_state      = LOW;
int motion_state_prev = LOW;

void setup() {
  pinMode(MOTION_SENSOR_PIN, INPUT);
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW);  // relay off initially
}

void loop() {
  motion_state_prev = motion_state;
  motion_state      = digitalRead(MOTION_SENSOR_PIN);

  if (motion_state_prev == LOW && motion_state == HIGH) {
    // Motion detected
    digitalWrite(RELAY_PIN, HIGH);  // turn relay on
  } else if (motion_state_prev == HIGH && motion_state == LOW) {
    // Motion stopped
    digitalWrite(RELAY_PIN, LOW);   // turn relay off
  }
}
Enter fullscreen mode Exit fullscreen mode

Things that go wrong

/dev/ttyHS1 on the Linux side and Serial1 on the MCU side are reserved for the Arduino Router that lets the two processors talk to each other, so never open them directly in your own code or the Bridge calls will break. If you're using the Bridge and Telegram setup, double-check the MCU sketch is uploaded and running before testing the Python script, since the MPU depends on it for every sensor check.

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-relay

Top comments (0)