Arduino Code and Video for the HC-SR501 Motion Sensor
In this tutorial, we will explore the HC-SR501 motion sensor, which is designed to detect motion through infrared technology. The sensor can trigger an output, allowing you to control devices such as LEDs or alarms based on motion detection. By the end of this tutorial, you'll have a clear understanding of how to wire the sensor and implement the Arduino code to make it function effectively. (in video at 00:45)

Hardware Explained
The HC-SR501 motion sensor is a compact module measuring 32 by 24 millimeters, capable of operating within a voltage range of 5 to 20 volts. At its core is the LHI 778 infrared sensor, which detects motion by sensing changes in infrared radiation. This makes it suitable for various applications, including security systems and robotic projects.
This module features three main pins: VCC for power, GND for ground, and OUT for the output signal. When motion is detected, the OUT pin sends a high signal (3.3 volts TTL), which can be used to trigger alarms or other devices. The sensor also includes adjustable sensitivity and time delay settings, allowing you to customize its operation based on your specific needs.
Datasheet Details
| Manufacturer | HC-SR501 |
|---|---|
| Part number | HC-SR501 |
| Logic/IO voltage | 3.3 V |
| Supply voltage | 5–20 V |
| Output current (per channel) | ≤ 20 mA |
| Peak current (per channel) | ≤ 50 mA |
| PWM frequency guidance | Not applicable |
| Input logic thresholds | 0.8 V (LOW), 2.0 V (HIGH) |
| Voltage drop / RDS(on) / saturation | 0.1 V |
| Thermal limits | 0°C to 70°C |
| Package | Module |
| Notes / variants | Adjustable sensitivity and time delay |
- Ensure correct power supply voltage (5-20 V).
- Use a resistor in series with LEDs to limit current.
- Adjust the sensitivity potentiometer for detection range.
- Set the time delay to control how long the output stays high.
- Be cautious of floating inputs; use pull-up or pull-down resistors as needed.
Wiring Instructions

To wire the HC-SR501 motion sensor, start by connecting the VCC pin to the 5V output on the Arduino. Next, connect the GND pin to the ground (GND) on the Arduino. The OUT pin should be connected to digital pin 2 on the Arduino, which will read the motion detection signal.
If you are using an LED for visual feedback, connect the anode (long pin) of the LED to digital pin 8 on the Arduino. The cathode (short pin) should be connected to one terminal of a 680-ohm resistor, with the other terminal of the resistor going to ground. This setup allows the Arduino to control the LED based on motion detection.
Code Examples & Walkthrough
The Arduino code for this setup initializes the serial monitor and configures the input and output pins. The key identifiers in the code include motion, which stores the state of the motion sensor, and the digital pins used for reading input and controlling the LED.
void setup() {
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(2, INPUT);// Input from sensor
pinMode(8, OUTPUT);// OUTPUT to alarm or LED
}
In this excerpt, the setup() function initializes the serial communication and sets the pin modes for the sensor and LED. Pin 2 is set as an input to read the motion sensor's output, while pin 8 is set as an output to control the LED.
void loop() {
int motion = digitalRead(2);
if(motion){
Serial.println("Motion detected");
digitalWrite(8, HIGH);
}else{
Serial.println("===nothing moves");
digitalWrite(8, LOW);
}
delay(500);
}
This code snippet represents the main loop where the program continuously checks the state of the motion sensor. If motion is detected, it prints "Motion detected" to the serial monitor and turns on the LED. If no motion is detected, it prints "===nothing moves" and turns off the LED.
Demonstration / What to Expect
When the setup is complete and the code is uploaded to the Arduino, you should expect the LED to light up when motion is detected by the HC-SR501 sensor. Additionally, the serial monitor will display messages indicating whether motion is detected or not. If you adjust the sensitivity and time delay settings on the sensor, you may notice changes in how quickly the sensor responds to motion (in video at 05:30).
Common pitfalls include incorrect wiring, such as reversing the connections for VCC and GND, which could damage the sensor. Ensure that the sensor's output pin is correctly connected to the designated input pin on the Arduino to avoid floating inputs.
Video Timestamps
- 00:00 Introduction
- 00:45 Hardware Overview
- 02:30 Wiring Instructions
- 04:00 Code Explanation
- 05:30 Demonstration
/*
* This is the Arduino code for HC-SR501 Motion Sensor
* to detect motion of humans or objects using Arduino for robotic car and other applications
* Watch the video https://youtu.be/mZCJNOf69JI
*
* Written by Ahmad Shamshiri for Robojax Video
* Date: Dec 27, 2017, in Ajax, Ontario, Canada
* Permission granted to share this code given that this
* note is kept with the code.
* Disclaimer: this code is "AS IS" and for educational purposes only.
*
*/
void setup() {
Serial.begin(9600);// setup Serial Monitor to display information
pinMode(2, INPUT);// Input from sensor
pinMode(8, OUTPUT);// OUTPUT to alarm or LED
}
void loop() {
int motion =digitalRead(2);
if(motion){
Serial.println("Motion detected");
digitalWrite(8,HIGH);
}else{
Serial.println("===nothing moves");
digitalWrite(8,LOW);
}
delay(500);
}
资源与参考
尚无可用资源。
文件📁
没有可用的文件。