HC-SR501 PIR Sensor (Passive Infrared Sensor) + IR Proximity Sensor Documentation
The HC-SR501 PIR Sensor is a low-power, low-cost sensor that detects infrared radiation, typically emitted by humans. It is widely used in various applications, including security systems, home automation, and robotics. This sensor combines a passive infrared (PIR) sensor with an infrared (IR) proximity sensor, allowing it to detect both motion and proximity.
The HC-SR501 PIR Sensor has three pins:
VCC (Red wire): Connect to a 5V power supply
GND (Black wire): Connect to the ground (GND) of the circuit
OUT (Yellow wire): Connect to a digital input pin of a microcontroller or other device
Operating voltage: 5V
Current consumption: 65mA
Detection range: Up to 7 meters (23 feet)
Detection angle: 120
Response time: 0.5 seconds
### Example 1: Basic Motion Detection using Arduino
This example demonstrates how to use the HC-SR501 PIR Sensor with an Arduino board to detect motion and toggle an LED.
```cpp
const int pirPin = 2; // Define the PIR sensor pin
const int ledPin = 13; // Define the LED pin
void setup() {
pinMode(pirPin, INPUT); // Set the PIR sensor pin as an input
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
int sensorState = digitalRead(pirPin); // Read the PIR sensor state
if (sensorState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on the LED if motion is detected
} else {
digitalWrite(ledPin, LOW); // Turn off the LED if no motion is detected
}
delay(100);
}
```
### Example 2: Proximity Detection using Raspberry Pi (Python)
This example demonstrates how to use the HC-SR501 PIR Sensor with a Raspberry Pi to detect proximity and print a message to the console.
```python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM) # Set the GPIO mode to BCM
pirPin = 17 # Define the PIR sensor pin
GPIO.setup(pirPin, GPIO.IN) # Set the PIR sensor pin as an input
while True:
if GPIO.input(pirPin) == GPIO.HIGH:
print("Object detected!")
else:
print("No object detected")
time.sleep(0.5) # Wait for 0.5 seconds before checking again
```
Note: Make sure to connect the HC-SR501 PIR Sensor to the appropriate pins on your microcontroller or single-board computer, and adjust the code accordingly.