Arduino Minima R4 with Breadboard, PIR setup

Motion Detection with PIR Sensor

Introduction

In this tutorial, you will learn how to use a Passive Infrared (PIR) sensor to detect motion. When motion is detected, an LED will light up, and a message will be sent to the Serial Monitor.

Objective

The goal of this experiment is to interface a PIR sensor with a microcontroller and respond to detected motion by lighting an LED and providing real-time feedback.

Arduino Minima R4 with Breadboard and LED setup

Components Required

Arduino Minima R4 with Breadboard and LED setup

Circuit Diagram

Connect the components as follows:

  • VCC of the PIR sensor to 5V (or 3.3V, depending on your sensor).
  • GND of the PIR sensor to GND on the microcontroller.
  • OUT of the PIR sensor to A0 (analog pin).
  • Connect the LED to GPIO pin 9 via a 220 ohm resistor.
PIR Sensor Circuit Diagram PIR Sensor Circuit Diagram

Code

Use the following code to interface the PIR sensor with your microcontroller:

// Pin Definitions
const int pirPin = A0;   // PIR sensor output pin (analog pin)
const int ledPin = 9;    // LED pin

void setup() {
    Serial.begin(115200);
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, LOW);
    Serial.println("PIR Motion Sensor Test Initialized");
}

void loop() {
    int pirValue = analogRead(pirPin);  // Read the analog value from the PIR sensor
    if (pirValue > 500) {              // Threshold for motion detection
        Serial.println("Motion Detected!");
        digitalWrite(ledPin, HIGH);
    } else {
        digitalWrite(ledPin, LOW);
    }
    delay(100);
}
            

Procedure

  1. Connect the components as per the circuit diagram.
  2. Copy the code above and paste it into your Arduino IDE.
  3. Select the correct board and port in Tools.
  4. Upload the code to your microcontroller.
  5. Open the Serial Monitor and set the baud rate to 115200.
  6. Wave your hand in front of the PIR sensor. The LED should light up, and "Motion Detected!" should appear in the Serial Monitor.

Adjustments and Settings

PIR Sensor Circuit Diagram

Most PIR sensors have two adjustable potentiometers:

Refer to your PIR sensor's datasheet for specific adjustment instructions.

PIR Sensor Circuit Diagram

Troubleshooting