Motion Detection Light System Using PIR Sensor

Introduction

Automating your home lighting is one of the most practical projects you can undertake with an Arduino. A Motion Detection Light System not only provides convenience—turning on lights when you enter a room—but also acts as a security feature and an energy-saving tool. In this guide, I will show you how to build a reliable motion-sensing light using an Arduino and a PIR (Passive Infrared) sensor.

 


How the PIR Sensor Works

The heart of this project is the HC-SR501 PIR Sensor. Unlike active sensors that emit energy (like ultrasound or laser), a PIR sensor is "passive." It works by detecting the infrared radiation (heat) emitted by living beings.

The sensor has two slots. When a person or animal moves in front of it, one slot detects a change in heat levels before the other, which triggers the output pin to go HIGH. It is a robust and inexpensive way to detect human presence.



Required Components

For this project, you will need:

✔ Arduino Uno or Nano: The brain of the system.
✔ PIR Motion Sensor: To detect movement.
✔ 5V Relay Module: This acts as a switch to control high-voltage lights.
✔ LED (for testing): To verify the logic before connecting AC power.
✔ Breadboard and Jumper Wires.


Technical Specifications of PIR

To provide the best results, you should understand the two potentiometers on the PIR sensor:

  ✔ Sensitivity Adjust: Controls the distance (typically 3 to 7 meters).
  ✔ Time Delay Adjust: Controls how long the light stays ON after movement stops (from 5 seconds to 5 minutes).
  ✔ Operating Voltage: 5V DC.


The Circuit Connections

Wiring the PIR sensor is simple, but the Relay module requires careful attention to safety.

PIR Sensor Connections:

   ✔ VCC: Connect to Arduino 5V.
   ✔ GND: Connect to Arduino GND.
   ✔ OUT: Connect to Arduino Digital Pin 2.

Relay Module Connections:

  ✔ VCC: Connect to Arduino 5V.
  ✔ GND: Connect to Arduino GND.
  ✔ IN: Connect to Arduino Digital Pin 3.





Understanding the Logic: How the Arduino Processes Motion

The code for a motion-controlled light system is a perfect example of a "Conditional Input-Output" loop. Even though the script is short, it performs several critical tasks that allow the hardware to interact with the physical world.

1. The Setup Phase: Defining the Pins

In the void setup() function, we initialize the communication between the microcontroller and the components.

  ✔ Pin 8 (Input): We set Digital Pin 8 as an INPUT. This pin is connected to the "Out" pin of the PIR (Passive Infrared) sensor. The sensor sends a signal to the Arduino here whenever it detects a change in the infrared heat signatures in its field of view.
  ✔ Pin 13 (Output): We set Digital Pin 13 as an OUTPUT. On most Arduino boards, like the Uno, Pin 13 is connected to an onboard LED. This allows us to test the circuit immediately without needing external components.

2. The Loop: Constant Monitoring

The void loop() function is where the real-time detection happens. It runs thousands of times per second, ensuring that the motion detection feels instantaneous.

  ✔ Reading the Sensor: We create an integer variable called val to store the data from the sensor. The command digitalRead(8) checks if the voltage on Pin 8 is High (5V) or Low (0V).
  ✔ The Conditional Logic: This is the "brain" of the code. We use an if-else statement to decide what to do:

If val == 1: This means motion was detected. The code tells the Arduino to set Pin 13 to HIGH, which turns the light on.
Else: If the value is 0 (no motion), the code sets Pin 13 to LOW, turning the light off.

3. Why Use Digital Signals?

PIR sensors are digital devices. Unlike a temperature sensor that might give you a range of values, a PIR sensor only has two states: Motion Detected (1) or No Motion (0). This is why we use digitalRead rather than analogRead. This simplicity makes the system very reliable for security alarms and automatic lighting.

4. Pro-Tip for Better Performance

While this code works perfectly for simple testing, PIR sensors often have a "settling time" or a "re-trigger delay." If you notice the light flickering, you can add a small delay(100); at the end of the loop to stabilize the readings.

CODE

void setup() {
  pinMode(8,INPUT);
  pinMode(13,OUTPUT);
}

void loop() {
  int val = digitalRead(8);

  if(val == 1){
    digitalWrite(13,HIGH);
  }
  else{
    digitalWrite(13,LOW);
 }
}

⚠️Troubleshooting Common Issues 

When building a motion detection system, you might encounter some unexpected behavior. Because PIR (Passive Infrared) sensors are sensitive to environmental changes, they require specific conditions to work perfectly. Here is how to fix the most common problems:

1. The Light Stays "ON" Constantly

If your LED or Relay stays active even when no one is in the room, it is usually not a code error. Check these three hardware factors:

  • Warm-up Time: PIR sensors require a "settling time" of about 30 to 60 seconds after being powered on. During this time, the sensor is learning the infrared signature of the room. If you test it immediately, it will stay triggered.
  • Sensitivity Adjustment: Most HC-SR501 modules have two orange potentiometers. The one labeled SENS (Sensitivity) might be turned to the maximum. Rotate it slightly counter-clockwise to reduce the detection range.
  • Retrigger Jumper: On the corner of the sensor, there is a yellow jumper. If it is set to the 'H' (Repeat Trigger) position, the output will stay HIGH as long as motion is detected. For a simple light, this is fine, but for testing, you might prefer the 'L' (Single Trigger) position.

2. The Sensor is Not Detecting Any Motion

If the light refuses to turn on, check your physical setup:

  • The Glass Barrier: PIR sensors detect heat radiation. If you place the sensor inside a plastic box with a glass cover, it will not work. Glass blocks the infrared energy from your body. Always ensure the white Fresnel lens (the dome) is exposed to the open air.
  • Voltage Drops: The HC-SR501 needs a stable 5V DC supply. If you are powering a large Relay module and the PIR sensor from the same Arduino 5V pin, the voltage might drop when the relay clicks, causing the sensor to fail. Use a separate power source if the system feels unstable.

3. False Triggers (Light turns on randomly)

Since the sensor detects infrared (heat), it can be "fooled" by non-human objects:

  • Heat Sources: Ensure the sensor is not pointed at a window, a heater, or an air conditioning vent. Rapidly moving hot or cold air can trigger the sensor.
  • WiFi Interference: PIR sensors are surprisingly sensitive to high-frequency radio waves. If your Arduino is sitting next to a WiFi router or a smartphone, the signal can cause "ghost" triggers. Keep the sensor at least 2 feet away from wireless devices.

4. Code "Lag" or Flickering

If the light flashes quickly or feels slow to respond:

  • Add a Delay: In the Arduino code, the loop() runs very fast. Adding a delay(500); at the end of your loop prevents the Arduino from reading "noise" as a valid motion signal.
  • Common Ground: Ensure the Ground (GND) of your PIR sensor, Relay, and Arduino are all connected together. Without a common ground, the digital signal from the sensor will be unstable.



إرسال تعليق

Post a Comment (0)

أحدث أقدم