STM32 Buttons
In many projects built around a MCU, one or more push buttons will be necessary. In this article, we will explain some of the ways this can be achieved and some of the challenges involved.
Videos
We have covered the topics in this article in a couple of STM32 Tutorial videos.
The first one explaining the use of external interrupts is here:
The second one covering bouncing is here:
How to hook up buttons
An extremely basic example of how to hook up a button can be seen in the schematics for the Black Pill development board:
As mentioned in the Black Pill article, there are at least wo issues with this:
- No pull resistor
- When pressed, the button will short the PA0 pin go GND, but when released PA0 will just be left floating. This can be alleviated by enabling the internal pull-up resistor on PA0.
- No de-bouncing capacitors
- Even using the best quality buttons (which is not one used on the Black Pill board) contact will not be instantaneous. There will be a brief period where the connection goes on/off a number of times. This is known as bouncing and should be handled.
What is bouncing
An example of button bounce could be shown like this:
Time wise this could all happen within micro seconds but even so, if used to trigger an interrupt, this could result in the interrupt handler being executed multiple times. It is therefore always a good idea to handle this in some way.
How to prevent bouncing (debouncing)
Electrically
The best way to handle button bounce is by handling it electrically. This is typically done by adding a capacitor which will result in a slower but steady transition:
In software
To be added
Miscellaneous links
To be added