Arduino UNO R4 - Motion Sensor - LED

Discover how to control an LED with the Arduino UNO R4 using the HC-SR501 motion sensor in this beginner-friendly Arduino tutorial. We’ll show you how to light up an LED when motion is detected and switch it off when there’s no movement. Perfect for mastering Arduino programming, motion detection, and simple LED projects!

Hardware Preparation

1×Arduino UNO R4 WiFi or Arduino UNO R4 Minima
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×HC-SR501 Motion Sensor
1×LED Kit with resistor
1×LED (red)
1×220Ω Resistor
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino UNO R4
1×Recommended: Breadboard Shield for Arduino UNO R4
1×Recommended: Enclosure for Arduino UNO R4
1×Recommended: Power Splitter for Arduino UNO R4
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following sensor 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 LED and Motion Sensor

Learn about LEDs and motion sensors (pinout, functionality, programming) in these tutorials:

Wiring Diagram

The wiring diagram between Arduino UNO R4 Motion Sensor LED

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-led */ #define MOTION_SENSOR_PIN 7 // The Arduino Uno R4 pin connected to the OUTPUT pin of motion sensor #define LED_PIN 3 // The Arduino Uno R4 pin connected to LED's pin int motion_state = LOW; // current state of motion sensor's pin int motion_state_prev = LOW; // previous state of motion sensor's pin void setup() { Serial.begin(9600); // initialize serial pinMode(MOTION_SENSOR_PIN, INPUT); // set the Arduino Uno R4 pin to input mode pinMode(LED_PIN, OUTPUT); // set the Arduino Uno R4 pin to output mode } void loop() { motion_state_prev = motion_state; // store old state motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state if (motion_state_prev == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Motion detected!"); digitalWrite(LED_PIN, HIGH); // turn on } else if (motion_state_prev == HIGH && motion_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Motion stopped!"); digitalWrite(LED_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.
  • Copy the code and open it in 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 over the sensor
  • Watch the LED's response

Code Explanation

Check the explanations in the comments within the source code!

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!