Arduino code and video for an HC-SR505 motion sensor module with relay
In this tutorial, we'll build a motion detection system using the HC-SR505 motion sensor and a relay module. This setup will allow you to turn on devices like lights or buzzers when motion is detected. The relay acts as a switch to control high-voltage devices safely. By the end of this guide, you'll have a functional motion sensor setup that can activate various appliances based on movement detection. For a clearer understanding, make sure to check the associated video (in video at 0:00).

Hardware Explained
The main components of this project include the HC-SR505 motion sensor, a relay module, and an Arduino board. The HC-SR505 is a passive infrared (PIR) sensor that detects motion by sensing changes in infrared radiation. It works by measuring the heat emitted by objects in its field of view. When a moving object is detected, the sensor sends a signal to the Arduino.
The relay module is used to control a high-voltage device, such as a light bulb. It acts as an electrically operated switch, allowing the Arduino to turn the device on or off safely. The relay has two main configurations: normally open (NO) and normally closed (NC), which determine the default state when the relay is not activated.
Datasheet Details
| Manufacturer | HC-SR505 |
|---|---|
| Part number | HC-SR505 |
| Logic/IO voltage | 3.3V - 20V |
| Supply voltage | 5V |
| Output current (per channel) | 20 mA |
| Peak current (per channel) | 100 mA |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3V (low) / 2.5V (high) |
| Voltage drop / RDS(on) / saturation | 0.2V |
| Thermal limits | +70 °C |
| Package | Module |
| Notes / variants | Small form factor; low power consumption |
- Ensure proper heat dissipation for high-current loads.
- Use decoupling capacitors for stable power supply.
- Be cautious with high-voltage connections; ensure safety.
- Test the sensor with simple outputs before connecting to relays.
- Check the orientation of the PIR sensor for optimal detection.
Wiring Instructions

To wire the HC-SR505 motion sensor, first connect the power pin (labeled as +) to the 5V output on your Arduino. Next, connect the ground pin (labeled as -) to one of the Arduino's ground pins. The signal pin (middle pin) should be connected to digital pin 2 on the Arduino.
For the relay module, connect the VCC pin to the 5V output on the Arduino and the ground pin to the Arduino's ground. The input pin of the relay should be connected to digital pin 9 on the Arduino. Finally, connect the buzzer's positive terminal to digital pin 8 and its negative terminal to ground. Make sure to connect the relay's common and normally open pins appropriately to control your device.
Code Examples & Walkthrough
The code for this project initializes the pins for the sensor, buzzer, and relay. It uses the digitalRead() function to check for motion and responds accordingly. Below is an excerpt from the setup function:
void setup() {
Serial.begin(9600); // setup Serial Monitor
pinMode(PIR, INPUT_PULLUP); // define pin as Input for sensor
pinMode(BUZZER, OUTPUT); // define pin as OUTPUT for buzzer
pinMode(RELAY, OUTPUT); // define pin as OUTPUT for relay
}
In this excerpt, the pins are set to either input or output mode. The Serial.begin(9600) initializes the serial communication for debugging.
The main loop checks for motion and activates the buzzer and relay when motion is detected. Here's a focused excerpt from the loop function:

void loop() {
int motion = digitalRead(PIR); // read the sensor
if(motion) {
Serial.println("Motion detected");
digitalWrite(BUZZER, HIGH); // turn the buzzer ON
digitalWrite(RELAY, LOW); // turn the relay ON
} else {
Serial.println("===nothing moves");
digitalWrite(BUZZER, LOW); // keep the buzzer OFF
digitalWrite(RELAY, HIGH); // turn the relay OFF
}
delay(500);
}
This excerpt reads the state of the PIR sensor and activates or deactivates the buzzer and relay based on the motion detected. The delay of 500 milliseconds ensures the loop checks the sensor at a reasonable interval.
Demonstration / What to Expect
When motion is detected, the light connected to the relay will turn on, and the buzzer will sound. If no motion is detected, the relay will turn off the light, and the buzzer will be silent. Be aware that the HC-SR505 has a built-in delay that keeps the output active for a short time after motion is detected (up to 8 seconds), which cannot be adjusted (in video at 5:30).
Video Timestamps
- 00:00 – Introduction to the project
- 01:30 – Explanation of components
- 03:15 – Wiring instructions
- 05:30 – Code walkthrough
- 07:00 – Demonstration of the setup
Things you might need
-
Amazon
-
Amazon
-
AliExpressPurchase 5v 12v 1 2 4 6 8 channel channel relay moduls.click.aliexpress.com
Resources & references
No resources yet.
Files📁
Fritzing File
-
HC-SR501 Body Sensor Module
application/zip0.01 MB