PIR Sensor Module
Relevant source files
The following files were used as context for generating this wiki page:The PIR (Passive Infrared) Sensor module is a core component of the HomeSecurity system, responsible for detecting motion by sensing infrared radiation from warm bodies. It serves as one of the primary triggers for the Stage 1 alert, contributing to the system's multi-layered detection strategy. The module includes a mandatory calibration period to ensure stable operation before it begins active monitoring.
Sources: HomeSecurity/README.md, HomeSecurity/src/pir_sensor.cpp, HomeSecurity/include/pir_sensor.h
System Integration
The PIR sensor functionality is managed by dedicated FreeRTOS tasks that handle its lifecycle from calibration to active detection and integration with the main alert logic.
Sources: HomeSecurity/src/main.cpp:11-13
FreeRTOS Task Integration
Two main tasks are created at startup to manage the PIR sensor:
| Task Name | Priority | Stack Size | Description |
|---|---|---|---|
calibratePirSensorTask | 3 (Medium) | 1536 bytes | Manages the initial 30-second sensor warm-up period. |
handlePirMovementTask | 4 (High) | 2048 bytes | Continuously monitors the PIR sensor for motion after calibration. |
Sources: HomeSecurity/src/main.cpp:11-13
The following diagram illustrates how the PIR sensor tasks are created and integrated within the FreeRTOS scheduler.
This diagram shows the creation of PIR-related tasks during system initialization.
Configuration and Initialization
The PIR sensor is connected to a specific GPIO pin on the ESP32, which is configured as an input during the setup phase.
| Parameter | Value | File |
|---|---|---|
PIR_SENSOR (GPIO Pin) | 21 | HomeSecurity/include/pir_sensor.h:6 |
The setupPirSensor() function initializes the hardware pin and starts the calibration timer.
// File: HomeSecurity/src/pir_sensor.cpp:6-9
void setupPirSensor() {
pinMode(PIR_SENSOR, INPUT);
pirCalibrationStart = millis();
}
Sources: HomeSecurity/src/pir_sensor.cpp:6-9, HomeSecurity/include/pir_sensor.h:6
Calibration Process
The PIR sensor requires a 30-second warm-up period to calibrate itself and prevent false positives. This process is managed by the calibratePirSensorTask.
- On startup, the
pirCalibrationStarttimestamp is recorded. - The task periodically checks if 30,000 milliseconds have elapsed.
- Once the duration is met, the
pirCalibratedflag is set totrue. - The Green LED is turned on to provide a visual indication that the system is calibrated and ready.
Sources: HomeSecurity/src/pir_sensor.cpp:12-20, HomeSecurity/README.md
The calibration flow is visualized below.
This flowchart shows the 30-second calibration logic managed by the calibratePirSensorTask.
Motion Detection
Core Detection Logic
The isPirActive() function is the primary interface for checking the sensor's state. It performs two checks:
- Ensures the sensor is calibrated (
pirCalibrated == true). - Reads the digital state of the
PIR_SENSORpin.
It returns true only if the sensor is calibrated and the pin state is HIGH.
// File: HomeSecurity/src/pir_sensor.cpp:22-32
bool isPirActive() {
bool retVal = false;
if (!pirCalibrated) {
return retVal;
}
bool pirRead = digitalRead(PIR_SENSOR);
if (pirRead == HIGH) {
retVal = true;
}
return retVal;
}
Sources: HomeSecurity/src/pir_sensor.cpp:22-32
Visual Feedback
The handlePirMovementTask is responsible for providing immediate visual feedback when motion is detected. It continuously calls isPirActive() and controls the Blue LED accordingly.
- If
isPirActive()istrue,turnBlueLedOn()is called. - If
isPirActive()isfalse,turnBlueLedOff()is called.
Sources: HomeSecurity/src/motion_logic.cpp:32-44, HomeSecurity/include/led.h:7
Alert Triggering
The possibleHumanMovementDetectedTask integrates the PIR sensor state into the main alert logic. If isPirActive() returns true, it triggers a Stage 1 alert, which involves:
- Activating the buzzer via
triggerAlarm(). - Sending a push notification via the
FirebaseNotifierwith the title "Mișcare detectată".
This task uses a notificareTrimisa flag to ensure the alert is sent only once per continuous motion event.
Sources: HomeSecurity/src/motion_logic.cpp:45-68
The following sequence diagram illustrates the complete flow from PIR detection to notification.
This diagram shows the sequence of events when the PIR sensor detects motion, leading to an LED indicator, an audible alarm, and a push notification.
Conclusion
The PIR Sensor Module is a fundamental building block of the HomeSecurity system's intrusion detection capabilities. Through a dedicated calibration task, it ensures reliable operation. Its output is tightly integrated with the system's alert logic and visual indicators, providing the first line of defense by triggering an immediate Stage 1 alert upon detecting potential human movement.
Sources: HomeSecurity/README.md, HomeSecurity/src/pir_sensor.cpp, HomeSecurity/src/motion_logic.cpp