Arduino UNO R4 - Motion Sensor - Relay

Welcome! This tutorial will show you how to use an Arduino UNO R4 with a motion sensor and a relay. It’s simple and clear, perfect for beginners. The motion sensor detects movement, and the relay can turn devices ON or OFF. For example, you can control a light, motor, or LED strip. By the end, you will know how to:

You will learn to program the Arduino UNO R4 to read the motion sensor and control the relay. Connect the relay to a device like a light bulb or fan, and the motion sensor will manage it. This project is great for anyone who wants to start with Arduino and make their home smarter. Let’s get started!

Arduino UNO R4 Motion Sensor Relay

Overview of Relay and Motion Sensor

If you are not familiar with relay and motion sensor (pinout, their functions, and programming), please refer to the following tutorials to learn more:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Motion Sensor Relay

This image is created using Fritzing. Click to enlarge image

Want a stronger connection than jumper wires? To do that, mount a Proto Screw Terminal Block Shield or a Screw Shield V2 on the Arduino UNO R4. The pins do not change. Just connect each wire to the matching screw terminal:

The wiring diagram between Arduino UNO R4 Motion Sensor Relay Screw Shield

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Initial Setting

Time Delay AdjusterScrew it in anti-clockwise direction fully.
Detection Range AdjusterScrew it in clockwise direction fully.
Repeat Trigger SelectorPut jumper as shown on the image.
arduino motion sensor initial setting

Arduino UNO R4 Code

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-motion-sensor-relay */ #define MOTION_SENSOR_PIN 10 // The Arduino Uno R4 pin connected to the OUTPUT pin of motion sensor #define RELAY_PIN 3 // The Arduino Uno R4 pin connected to the IN pin of relay int motion_state = LOW; // current state of motion sensor's pin int prev_motion_state = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode } void loop() { prev_motion_state = motion_state; // store old state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (prev_motion_state == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); digitalWrite(RELAY_PIN, LOW); // turn off } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Connect the Arduino UNO R4 to your computer using a USB cable.
  • Open the Arduino IDE and choose the correct board and port.
  • Paste the provided code into the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino UNO R4.
Arduino IDE Upload Code
  • Wave your hand in front of the sensor
  • Observe the relay's status change

Code Explanation

Look at the explanations in the comments inside the source code!

Relay vs MOSFET Module

A relay is not the only way to switch a device. If your device runs on DC power, such as a 12V LED strip, a 12V solenoid lock or a 12V fan, a MOSFET module can do the same job.

Relay MOSFET Module
What it can do turn the device on or off turn the device on or off, and set its power (PWM)
Noise when switching a click every time no sound
How fast it switches slow, a few milliseconds very fast
Life the contacts wear out after many switches nothing moves, so it lasts long
Type of power AC or DC DC only
Device power supply any voltage the relay is rated for about 6V or more
Isolation from the Arduino yes, between coil and contacts yes, by the PC817 optocoupler
Good for AC devices, or a simple ON/OFF DC devices, or dimming and speed control

Both the relay and the MOSFET module turn on when the Arduino UNO R4 pin is HIGH. So for a simple ON/OFF, the code above stays the same. Only the wiring changes. For AC mains devices, always use the relay.

To learn the MOSFET module pinout, wiring, PWM code and safety tips, see the Arduino UNO R4 - MOSFET Module tutorial.

Video Tutorial

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