IoT Drivers  v3.3.0 (S2022)
Engineering in Software Technology
Quick start guide for HC-SR501 PIR-sensor Driver

This is the quick start guide for the Driver for PIR sensor, with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.

HC-SR501 Driver use cases

Create and initialise the driver

The following must be added to the project:

Add to application initialization:

  • Initialise the driver:
    hcsr501Inst = hcsr501_create(&PORTE, PE5);
    if ( NULL != hcsr501Inst )
    {
    // Driver created OK
    // If NULL is returned the driver is not created!!!
    }

How to test if the sensor detects anything

In this use case, the steps to perform a detection is shown.

Note
The driver must be created Create and initialise the driver before a detection is possible.
if ( hcsr501_isDetecting(hcsr501Inst) )
{
// Something is detected
}
else
{
// Nothing is detected
}
hcsr501_isDetecting
bool hcsr501_isDetecting(hcsr501_p instance)
Tell if the sensor is detecting something.
hcsr501_p
struct hcsr501_struct * hcsr501_p
Definition: hcsr501.h:30
hcsr501.h
Driver to PIR Motion Detector Module.
hcsr501_create
hcsr501_p hcsr501_create(volatile uint8_t *port, uint8_t portPin)
Create the HC-SR501 driver.