cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO interrupt triggered incorrectly

hchienti
Associate II

I am using micro switches in my system with the GPIO EXTI. I would like to detect only the press of the switch, but the interrupt was triggered on both press and release of the switch. The interrupts were even triggered twice on each press and release.

Here is my configuration of the pin. PC9 on the STM32H563ZI board is used. The GPIO pin is set as below, and the EXTI LINE9 Interrupt is checked in the NVIC setting.

hchienti_0-1714113986504.png

And here is the code:

void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)

{

printf("%d\n", GPIO_Pin);

}

Does any one know what's happening and how to fix it?

6 REPLIES 6
Peter BENSCH
ST Employee

You probably have the same problem with bouncing as described there, except that your switch goes to GND and the one there goes to VDD. You should think about debouncing, for which there are (almost) as many solutions as there are developers.

Hope that helps?

Regards
/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi @Peter BENSCH ,

Thanks for reply. I guess bouncing might be the reason why the press is detected twice. How about detecting both on pressing and releasing? Could this came from bouncing, too?

Peter BENSCH
ST Employee

Yes.

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Yes, bouncing can activate any edge trigger on both, press and release of a button. Try to solve this issue first. The most common way to do it is by adding a small capacitor between GPIO line and ground or by adding a time delay after the first edge detection.
If the button press logic is still reversed after solving the bouncing issue, you can simply change the falling edge detection to rising edge. This inverted logic issue might be because of how your button outputs a signal. There is a chance that it's connected to ground when released, and to +3.3V, when pressed.

Did you try some simple "standard" way to connect a switch ?

Try with a cap:

AScha3_0-1714119516480.png

 

If you feel a post has answered your question, please click "Accept as Solution".
Karl Yamashita
Lead III

You can do software debounce of switches. See this video https://www.youtube.com/watch?v=o0qhmXR5LD0

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.