Arduino Motion Detection Project: A Beginner’s Guide to PIR Sensors & LED Control

Arduino Motion Detection Project: A Beginner’s Guide to PIR Sensors & LED Control

Arduino Motion Detection Schematic

Schematic of the Arduino Motion Detection Project

Introduction to Motion Detection with Arduino

Welcome to this exciting Arduino project! In this tutorial, we’ll build a simple yet practical motion detection system using an Arduino microcontroller, a PIR (Passive Infrared) sensor, and an LED. Whether you’re a beginner in electronics or looking to expand your DIY skills, this project is a perfect starting point. By the end, you’ll have a working system that lights up an LED and sends alerts to your computer whenever motion is detected—great for home automation, security, or just learning the ropes of Arduino programming!

Why Build This Project?

Motion detection is a fundamental concept in electronics with countless real-world applications, from triggering lights to activating alarms. Using an Arduino makes it accessible and affordable. Plus, this project introduces you to key skills like digital sensor interfacing, LED control, and serial communication—all essential for advancing in embedded systems and DIY electronics.

Project Overview: How It Works

Our motion detection system uses a PIR sensor connected to digital pin 2 of the Arduino to detect movement. When motion is sensed, the sensor outputs a HIGH signal, which triggers an LED connected to pin 13 to light up. Simultaneously, a message like “Motion detected!” is sent to the Serial Monitor via the Arduino IDE. When motion stops, the LED turns off, and “Motion stopped!” is displayed. A clever variable called state ensures the messages only print when the motion status changes, keeping the output clean and concise.

Components You’ll Need

To bring this project to life, gather these basic components—most are staples in any electronics toolkit:

  • Arduino Board: Any model works (e.g., Arduino Uno, Nano, or even a compatible Elegoo board).
  • PIR Sensor: A common motion sensor like the HC-SR501.
  • LED: A standard 5mm LED (any color) with a 220-ohm resistor.
  • Breadboard and Jumper Wires: For easy, solder-free connections.
  • USB Cable: To connect your Arduino to your computer.

Note: Elegoo boards are a budget-friendly alternative to official Arduino boards and fully compatible with the Arduino IDE—perfect for cost-conscious beginners!

The Schematic: Wiring It Up

Arduino PIR Sensor Schematic

Follow this schematic to connect your PIR sensor and LED to the Arduino

Wiring is straightforward: connect the PIR sensor’s VCC to 5V, GND to GND, and OUT to digital pin 2. For the LED, attach the anode (longer leg) to pin 13 via a resistor and the cathode to GND. Double-check your connections to avoid issues during testing!

The Code: Bringing It to Life

Here’s the Arduino code for our motion detection system, broken into clear sections with explanations. Copy this into your Arduino IDE and upload it to your board.

Variables

int led = 13;      // Pin for the LED
int sensor = 2;    // Pin for the PIR sensor
int state = LOW;   // Tracks motion status (default: no motion)
int val = 0;       // Stores the sensor reading
            

These lines define the pins and variables: led for the LED, sensor for the PIR, state to track motion, and val to hold the sensor’s value.

Setup Function

void setup() {
    pinMode(led, OUTPUT);    // Set LED pin as output
    pinMode(sensor, INPUT);  // Set sensor pin as input
    Serial.begin(9600);     // Start serial communication at 9600 baud
}
            

The setup() function runs once, configuring the LED as an output, the sensor as an input, and starting serial communication to display messages.

Loop Function

void loop() {
    val = digitalRead(sensor);  // Read the sensor value
    if (val == HIGH) {         // If motion is detected
        digitalWrite(led, HIGH); // Turn LED ON
        delay(500);            // Wait 500ms
        if (state == LOW) {     // If state was previously LOW
            Serial.println("Motion detected!");
            state = HIGH;       // Update state to HIGH
        }
    } else {                   // If no motion is detected
        digitalWrite(led, LOW);  // Turn LED OFF
        delay(500);            // Wait 500ms
        if (state == HIGH) {    // If state was previously HIGH
            Serial.println("Motion stopped!");
            state = LOW;        // Update state to LOW
        }
    }
}
            

The loop() function runs continuously, checking the sensor. If it detects motion (HIGH), the LED turns on and a message prints (only if the state changes). If no motion is detected (LOW), the LED turns off with a corresponding update.

Testing Your Project

Upload the code, open the Serial Monitor (Ctrl+Shift+M in the Arduino IDE), and wave your hand in front of the PIR sensor. The LED should light up, and you’ll see “Motion detected!” in the monitor. When you stop moving, the LED turns off, and “Motion stopped!” appears. If it doesn’t work, check your wiring or adjust the sensor’s sensitivity using its onboard potentiometers.

Take It Further

Ready to level up? Try these ideas:

  • Add a buzzer for an audible alarm.
  • Use multiple sensors for a larger detection area.
  • Log motion events with timestamps using an SD card module.
This project is just the beginning of your journey into Arduino programming and sensor-based applications!

Explore More Arduino Projects

Loved this tutorial? Dive into other beginner-friendly topics like:

  • LED control with Arduino
  • Serial communication basics
  • DIY sensor-based projects
  • Motion sensor applications
Keep experimenting and building—your next big idea is just a sketch away!

Post a Comment

0 Comments