PIR sensor with STM32

The term PIR is the short for the Passive Infra Red. The term “passive” indicates that the sensor does not actively take part in the process, it does not emit the IR signals itself, rather passively detects the infrared radiations coming from the human body in the surrounding area.

In this tutorial we are going to interface PIR sensor with STM32. I am using STM32F446RE and the procedure will remain same for other STM32 devices also.
PIR sensors have adjustable settings for the timing and sensitivity. You can see that in the picture below.

We can adjust delay time to Set how long the output remains high after detecting motion…. Anywhere from 5 seconds to 5 minutes. Similarly sensitivity adjusts the range from 3 to 7 meters.  
Retrigger setting jumper is to set for single or repeatable jumpers.


The working of PIR sensor is pretty simple and straight forward. Whenever the sensor detects any motion, it pulls the out (DATA) pin HIGH and after some delay the pin goes back to LOW. So all we need to do is set the pin as input and than read it for the changes.

I am using a LED as output, so whenever the PIR sensor read some activity, the LED will glow.

So let’s start by firing up our CubeMx and set the PIN A1 as input pin

Clock is configured to run the controller at its maximum frequency (i.e. 180 MHz)

Some Insight into the CODE

while (1) 
  if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1)) // if the pin is HIGH 
   HAL_GPIO_WritePin (GPIOA, GPIO_PIN_2, 1); // turn on LED 
   HAL_Delay (1000); 
   HAL_GPIO_WritePin (GPIOA, GPIO_PIN_2, 0); // LED OFF 
   while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1)); //wait for pin to go low 


Check out the VIDEO Below
Notify of
Oldest Most Voted
Inline Feedbacks
View all comments