The HCSR501 PIR Motion Sensor module is a generic component, and its specifications may vary depending on the manufacturer and supplier. Be sure to verify the specifications with your supplier before using the module in your project.
The HCSR501 PIR Motion Sensor module is a generic component, and its specifications may vary depending on the manufacturer and supplier. Be sure to verify the specifications with your supplier before using the module in your project.
HCSR501 PIR Motion Sensor (Passive Infrared Sensor) DocumentationOverviewThe HCSR501 PIR Motion Sensor is a low-cost, low-power passive infrared sensor module designed to detect motion and movement. It is commonly used in various IoT applications, such as home automation, security systems, and robotics. This sensor module is based on the BISS0001 micro-power PIR sensor IC and features a built-in voltage regulator, making it easy to interface with microcontrollers and other devices.PinoutThe HCSR501 PIR Motion Sensor module has three pins:| Pin | Description |
| --- | --- |
| VCC | Power supply (typically 5V) |
| OUT | Digital output (HIGH or LOW) |
| GND | Ground |Operating PrincipleThe HCSR501 PIR Motion Sensor detects motion by sensing the infrared radiation emitted by objects. When a person or object enters the sensor's detection range, the sensor output goes HIGH. The detection range can be adjusted by adjusting the sensor's sensitivity and the distance between the sensor and the reflective surface.Code Examples### Example 1: Basic Motion Detection with ArduinoIn this example, we will use the HCSR501 PIR Motion Sensor to detect motion and toggle an LED on and off.
```cpp
const int pirPin = 2; // PIR sensor output pin
const int ledPin = 13; // LED pinvoid setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
}void loop() {
int pirState = digitalRead(pirPin);
if (pirState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn LED on if motion detected
} else {
digitalWrite(ledPin, LOW); // Turn LED off if no motion detected
}
delay(50);
}
```
### Example 2: Motion Detection with Raspberry Pi (Python)In this example, we will use the HCSR501 PIR Motion Sensor to detect motion and print a message to the console.
```python
import RPi.GPIO as GPIO
import timeGPIO.setmode(GPIO.BCM)pir_pin = 17 # PIR sensor output pin
GPIO.setup(pir_pin, GPIO.IN)while True:
pir_state = GPIO.input(pir_pin)
if pir_state:
print("Motion detected!")
else:
print("No motion detected.")
time.sleep(1)
```
### Example 3: Motion Detection with ESP32 (MicroPython)In this example, we will use the HCSR501 PIR Motion Sensor to detect motion and send a message over Wi-Fi using the ESP32's built-in Wi-Fi capabilities.
```python
import machine
import network
import ubinascii
import urequestspir_pin = 25 # PIR sensor output pin
pir = machine.Pin(pir_pin, machine.Pin.IN)wifi = network.WLAN(network.STA_IF)
wifi.connect("your_wifi_ssid", "your_wifi_password")while True:
pir_state = pir.value()
if pir_state:
print("Motion detected!")
urequests.post("https://api.example.com/motion", json={"detected": True})
else:
print("No motion detected.")
time.sleep(1)
```
Note: In all examples, make sure to adjust the pin numbers and the sensor's sensitivity according to your specific setup and requirements. Additionally, ensure that the power supply to the sensor module is stable and within the recommended voltage range.