Arduino code and video for an HC-SR505 motion sensor module with relay

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).

HC-SR505 motion sensor

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

ManufacturerHC-SR505
Part numberHC-SR505
Logic/IO voltage3.3V - 20V
Supply voltage5V
Output current (per channel)20 mA
Peak current (per channel)100 mA
PWM frequency guidanceN/A
Input logic thresholds0.3V (low) / 2.5V (high)
Voltage drop / RDS(on) / saturation0.2V
Thermal limits+70 °C
PackageModule
Notes / variantsSmall 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

Arduino wiring for  HC-SR505 motion sensor
Arduino wiring for HC-SR505 motion sensor

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:

Wirig relay module to AC load

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

Images

Wirig relay module to AC load
Wirig relay module to AC load
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
HC-SR505 motion sensor
Arduino wiring for  HC-SR505 motion sensor
Arduino wiring for HC-SR505 motion sensor
74-This is the Arduino code for the HC-SR505 motion sensor (small) with relay.
Language: C++
Copied!

Things you might need

Resources & references

No resources yet.

Files📁

Fritzing File