Skip to main content
Associate
July 4, 2026
Solved

STM32F4 connected to AM312 through ISR issue

  • July 4, 2026
  • 5 replies
  • 107 views

Hello,

 

I have connected a sensor to my nucleo board to send data when sensor detects movement.

I have introduced an ISR for that.

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0)
{
pirDetected = true;
}
}

 

unfortunatelly with that approach everytime the sensor was triggered ( I was able to measure 3.3v on the OUT pin using voltmeter) my stm32 is kind a freezing.

 

 

I have introduced some testcode,

extern "C" int main(void)
{

/* USER CODE BEGIN 1 */



/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */
/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();

while (1)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0))
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
HAL_Delay(5000); // Keep LED on for 5 seconds
}
else
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
}
HAL_Delay(100);
}
  /*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

...

/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
...

with that my LED is on for about 5-6seconds and after that goes off, even when I am triggering the sensor, no reaction on led.

 

I am not sure what could be the reason, voltage is present when waving in front of the sensor.

AM312_OUT → PA0

AM312_vcc → 5v (have treid 3.3 as well)

AM312_GND → GND

 

sensor involved:

https://de.aliexpress.com/w/wholesale-am312-pir-sensor.html

 

Cheers

Best answer by DennisM

oh, my goodness ! I messed up  …

I had generated the code with ISR in CubeMX, but I am writing not in .c but .cpp, and I had my new file generated with ISR as .c file, but I am using g++ compiler for .cpp

 

Thanks everyone for support :(

5 replies

Associate
July 4, 2026

The problem is the EXTI priority. In HAL, SysTick has priority 15 (defined as TICK_INT_PRIORITY = 0x0F), and it is used to increment the counter inside HAL_Delay. Your EXTI0 is set to priority 0, which is the highest. This means SysTick interrupt cannot preempt EXTI0, so HAL_GetTick() stops incrementing and HAL_Delay hangs forever.

This is made worse by the AM312 behavior: it has a 2-second hold time with repeatable trigger mode, meaning as long as movement is detected the output stays HIGH and EXTI0 keeps firing.

Fix: change the EXTI priority to any value from 1 to 15:

HAL_NVIC_SetPriority(EXTI0_IRQn, 15, 0);
DennisMAuthor
Associate
July 4, 2026

unfortunately setting priority to 1 or 15 didnt help, I have tried to directly connect PA0 to 3.3v and GND with test code below, but dont see any reaction. If I start whit PA0 connected to GND, then my LED stays off all the time even if I connect to 5v afterwards. If I start with PA0 not connected, then my LED stays on even if I connect PA0 to GND … That is strange..

 

I am using Pin 18->5v, 20->GND and 28->PA0

 


// uint8_t tx_counter = 0;

while(1)
{
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// HAL_Delay(200);

if(pirCount)
{
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// HAL_Delay(500);
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// HAL_Delay(500);

//pirDetected = false;
pirCount--;

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
// chupi[6] = tx_counter;
//
// sendPacket(cfg, chupi, 8);
//
// tx_counter++;

}

// HAL_Delay(50);
}

}

...

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0)
{
// pirDetected = true;
pirCount++;
}
}

 

unfortunatelly I cannot debug with cubeide as I cannot connect/flash the code with it, I have to use CubeProgrammer for flashing...

Richard Li
Senior
July 5, 2026

Your Callback should clear EXTI_PR (Pending register).

If you can’t debug, it is very difficult find these SFR value.

Richard Li
Senior
July 5, 2026

One suggestion is: add LED_GPIO turn on code inside callback, then you can make sure the ISR work.

DennisMAuthorBest answer
Associate
July 5, 2026

oh, my goodness ! I messed up  …

I had generated the code with ISR in CubeMX, but I am writing not in .c but .cpp, and I had my new file generated with ISR as .c file, but I am using g++ compiler for .cpp

 

Thanks everyone for support :(