Author Topic: Basic setup for button press processing - STM32  (Read 2050 times)

0 Members and 1 Guest are viewing this topic.

Offline anso-engineerTopic starter

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ua
Basic setup for button press processing - STM32
« on: August 11, 2026, 07:07:29 am »
Hello, I take this picture from the data reference of STM32F030Fx series.

As I understand, pull-up to Vdd is more preferable way for button processing. I think in the Cube IDE I can configure it as Pull UP and there is no need for external ressistor, according to that schematic?
In so many of schematics, I saw 100nF capacitor, probably for debounce purposes, so maybe this is suitable for avoding software debounce.
« Last Edit: August 11, 2026, 07:10:03 am by anso-engineer »
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 6100
  • Country: gb
  • Doing electronics since the 1960s...
Re: Basic setup for button press processing - STM32
« Reply #1 on: August 12, 2026, 06:24:56 am »
This is OK but a pullup or pulldown is the same. The problem with the cap is that it gets discharged into the switch and eventually will damage the contacts.

To debounce a switch, maintain a "last" state, and take the button as pressed when the new state is a 1 and the old was a 0.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Online kite31

  • Frequent Contributor
  • **
  • Posts: 293
  • Country: au
Re: Basic setup for button press processing - STM32
« Reply #2 on: August 12, 2026, 07:16:51 am »
To debounce a switch, maintain a "last" state, and take the button as pressed when the new state is a 1 and the old was a 0.
Then either add a delay before checking for a next state change or else delay from first detection to check again to make sure the "new" state is sustained rather than being jitter. These have different implications for how your circuit works, or whether you distinguish short from long press.
 

Offline anso-engineerTopic starter

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ua
Re: Basic setup for button press processing - STM32
« Reply #3 on: August 12, 2026, 07:29:54 am »
The problem with the cap is that it gets discharged into the switch and eventually will damage the contacts.
I assume I can somehow simulate it in Spice software. Is there is big current expected?
So, you recommend to use a software debounce)
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5395
  • Country: nl
Re: Basic setup for button press processing - STM32
« Reply #4 on: August 12, 2026, 07:34:50 am »
Pullup or pull down does not matter, the important thing is that depressing the switch results in a logic level change.

And as peter-h mentioned: the (usual) ceramic capacitors can deliver very high peak currents into a hard mechanical switch and this will damage them over time. If a capacitor is used, you should also add a series resistor to limit the peak current to below the switch specifications (can be as low as 5 to 20 mA). This combination is most often used for rotary encoders, as the relatively slow software debouncing (see below) is not satisfactory. Rotary encoders can generate several hundred pulses per second, while a typical debounce routine  last tens of ms. (A human can't press a single switch faster then about 10x per second).

Switch debouncing in software is a whole different topic. A common method is to read the switch at fixed intervals (for example an ISR key scanning routine) and only register a change if multiple consecutive reads have the same level. Debouncing with just a capacitor works best if the uC input is a schmitt trigger.

Jack Ganssle has written a nice paper.pdf about switch behavior and debouncing. There is a huge difference in switch behavior. For example the rubber dome switches with conductive rubber sometimes don't bounce at all, but behave more like a pressure sensor which change their resistance depending on the amount of finger pressure on the switch. Other mechanical switches can bounce (open & close) 100x or more during a single key press.
 
The following users thanked this post: anso-engineer

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5395
  • Country: nl
Re: Basic setup for button press processing - STM32
« Reply #5 on: August 12, 2026, 07:40:15 am »
I assume I can somehow simulate it in Spice software. Is there is big current expected?

With a hard short, the current is limited by ESR, and by other parasitic components such as the resistance and inductance of the leads and PCB tracks. It's not so easy to simulate this accurately in spice. You'd have to measure these parameters first before you can make an accurate model. As a rough guide, these peak currents can be 5 to 10A or even more.

A short peek suggests that ESR of "typical" MLCC's is in the order of 10 mOhm. Which would result in a peak current of 3V3/0.01 = 330A So the other parasitic factors dominate to get the peak current down to 10A.
« Last Edit: August 12, 2026, 07:47:56 am by Doctorandus_P »
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 12750
  • Country: nz
Re: Basic setup for button press processing - STM32
« Reply #6 on: August 12, 2026, 07:51:06 am »
This is OK but a pullup or pulldown is the same. The problem with the cap is that it gets discharged into the switch and eventually will damage the contacts.

Depends on the size of the switch and size of the cap.
Sometimes it can help if you're using a physically big robust switch on a mcu input and the switch has a rather high minimum current.
In those cases you may want a burst of current to keep the contacts from getting oxidized.
But usually you add a bigger cap and a series resistor to keep the inrush under control.
I recall once using 10uF at 15R for a large automotive rocker switch connected to a MCU input. Just to give the large switch some reasonable current flow.

In OP's case he could add a series resistor to limit current and provide some filtering lag for retriggering.
« Last Edit: August 12, 2026, 07:59:11 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline anso-engineerTopic starter

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ua
Re: Basic setup for button press processing - STM32
« Reply #7 on: August 12, 2026, 08:36:48 am »
A short peek suggests that ESR of "typical" MLCC's is in the order of 10 mOhm. Which would result in a peak current of 3V3/0.01 = 330A So the other parasitic factors dominate to get the peak current down to 10A.
then maybe it helps?)
 

Online Psi

  • Super Contributor
  • ***
  • Posts: 12750
  • Country: nz
Re: Basic setup for button press processing - STM32
« Reply #8 on: August 12, 2026, 09:30:43 am »
To get a good amount of time for debounce while also allowing enough current to flow so that you don't run into issues with the minimum switch current you'll probably want a larger cap.

Maybe  4.7uF and 1000R
That will be around 3.3mA peak and around 60% drop in around 5ms

https://snubber.ai/calculators/rc-time-constant-calculator

Personally I usually do software based switch debounce unless i have a reason to do it in hardware.
It saves board space and parts cost.
« Last Edit: August 12, 2026, 09:33:50 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2545
  • Country: 00
Re: Basic setup for button press processing - STM32
« Reply #9 on: August 12, 2026, 09:53:04 am »
I believe this is the "best" solution:



It's taken from the schematic of the NUCLEO-L452RE development board.

The internal (weak) pull-up resistor is too high and the resulting very low current when the
button is pressed can't keep the contacts clean. Hence the external 4K7 pull-up resistor.
« Last Edit: August 12, 2026, 09:55:16 am by Karel »
 
The following users thanked this post: anso-engineer

Online Psi

  • Super Contributor
  • ***
  • Posts: 12750
  • Country: nz
Re: Basic setup for button press processing - STM32
« Reply #10 on: August 12, 2026, 10:00:22 am »
Yes, its important to consider the charging pullup.
Using my previous example of 4.7uF 1k if you're using internal pullups that are like 30k it will take like 100-200ms to recover after a press. Which may or may not be an issue for your application.

It also depends on the type of switch and what sort of switch bounce you expect. A larger switch with big contacts and lever arm will bounce for longer than a small tack switch.
For small tack switches 100nF is fine.
« Last Edit: August 12, 2026, 11:08:54 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: anso-engineer

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2545
  • Country: 00
Re: Basic setup for button press processing - STM32
« Reply #11 on: August 12, 2026, 10:12:37 am »
And on the NUCLEO-H533RE development board they use this circuit:



Probably another engineer with different ideas  8)
 
The following users thanked this post: anso-engineer

Offline anso-engineerTopic starter

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ua
Re: Basic setup for button press processing - STM32
« Reply #12 on: August 12, 2026, 12:01:45 pm »
Personally I usually do software based switch debounce unless i have a reason to do it in hardware.
Yes, I already have used software debounce and it worked well.
Maybe I should take btn, 5mm in diameter which I plan to use, and measure it with osciloscope. It probably let me understand the bounce period.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->