Arduino Motion Sensor Guide (Using a PIR Sensor)

Configurare noua (How To)

Situatie

A PIR (Passive Infrared) sensor detects motion by measuring changes in infrared radiation emitted by objects (like humans or animals) within its field of view.

PIR sensors are commonly used for:

Component Quantity Notes
Arduino Uno (or Nano, Mega, etc.) 1 Any compatible board
PIR Motion Sensor (HC-SR501 or similar) 1 Adjustable sensitivity and delay
Breadboard 1 For easy connections
Jumper wires ~5 Male-to-male recommended
LED 1 Optional for visual indication
220Ω resistor 1 For LED current limiting
USB cable 1 For programming and power

 Understanding the PIR Sensor

Typical PIR module (e.g., HC-SR501) has 3 pins:

Pin Label Function
1 VCC Power (connect to +5V)
2 OUT Sends HIGH when motion is detected
3 GND Connects to Ground

Adjustable knobs (optional):

  • Sensitivity: changes detection range (typically 3–7 meters)
  • Time delay: how long output stays HIGH after motion (usually 0.3s–5min)

Wiring Diagram

PIR Sensor Arduino Notes
VCC 5V Power supply
OUT D2 Digital input pin
GND GND Common ground
LED (+) D13 Visual output (optional)
LED (–) GND (via 220Ω resistor) Current limit

Connect VCC → 5V, GND → GND, OUT → D2, LED → D13 (optional)

Arduino Code Example


// PIR Motion Sensor with LED Example

int pirPin = 2; // PIR sensor output pin
int ledPin = 13; // LED pin

int pirState = LOW; // Default state
int val = 0; // Variable for reading the pin status

void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("PIR Motion Sensor Active");
}

void loop() {
val = digitalRead(pirPin); // Read input value from PIR

if (val == HIGH) {
digitalWrite(ledPin, HIGH); // Turn LED ON
if (pirState == LOW) {
Serial.println("Motion detected!");
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // Turn LED OFF
if (pirState == HIGH) {
Serial.println("Motion ended!");
pirState = LOW;
}
}
}

Explanation: The PIR output goes HIGH when motion is detected. The LED turns on and a message is printed to the Serial Monitor.

How It works

  1. Warm-up time: After powering up, the PIR sensor takes ~30–60 seconds to stabilize.
  2. Detection: When an object moves, the sensor’s output pin goes HIGH.
  3. Reset: After the set delay, the output returns LOW until new motion is detected.

1. Control a Relay

Use motion detection to control a light or appliance:


int relayPin = 8; // connect to relay module IN pin
// Replace ledPin with relayPin in code

2. Buzzer Alarm

Add a buzzer that sounds when motion is detected:


int buzzerPin = 9;
digitalWrite(buzzerPin, HIGH);

3. Serial Logging

Send motion logs to a computer or IoT platform for analysis.

4. Automation

Combine PIR with an LDR (light sensor) so it only triggers lights at night.


Solutie

Tip solutie

Permanent

Voteaza

(10 din 20 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?