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
Or you can buy the following 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 .
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

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 Adjuster | Screw it in anti-clockwise direction fully. |
Detection Range Adjuster | Screw it in clockwise direction fully. |
Repeat Trigger Selector | Put jumper as shown on the image. |

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

- Wave your hand over the sensor
- Watch the LED's response
Code Explanation
Check the explanations in the comments within the source code!