0
\$\begingroup\$

I need a quick way of adding some de-bouncing to a button in hardware. I am aware that ultimately the software should really do it, but for now I came up with this, saw it somewhere on internet:

enter image description here

Does it make sense? I would say GPIO node is HIGH when the button is not pressed, and it will be LOW when button is pressed.

In that case, what should be the configuration on MCU side? Obviously the pin should be configured as INPUT, but the question is should I enable any pull-up or pull-down resistors internally in the MCU?

Should the trigger be POS EDGE or NEG EDGE or HIGH or LOW? I am a bit confused about all the different possibilities.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ What edges are you talking about? You wanted to read as GPIO, but edge triggering sounds like configuring interrupts. What is it that you are trying to achieve and on which MCU? \$\endgroup\$
    – Justme
    Commented May 22, 2022 at 10:16
  • \$\begingroup\$ @Justme The MCU has settings for reporting interrupt, has positive edge, negative edge, level edges (high or low) etc \$\endgroup\$
    – DEKKER
    Commented May 22, 2022 at 11:42
  • 1
    \$\begingroup\$ Yes, but you said GPIO. Do you want to use GPIO, or interrupts? And if interrupts, why, what for? \$\endgroup\$
    – Justme
    Commented May 22, 2022 at 11:59
  • \$\begingroup\$ "The MCU has settings for reporting interrupt," Assuming it is interrupt and not GPIO. Which MCU? \$\endgroup\$
    – devnull
    Commented May 22, 2022 at 12:27

2 Answers 2

1
\$\begingroup\$

Do not enable any pull up / down resistors inside the mcu, and the switch will generate a trailing edge when depressed, so if you want to trigger your event when the switch is depressed you configure for “Neg edge”. “Low” may also work but depending on how the interrupt works it may continuously retrigger when the button is held - review level sensitive vs edge sensitive interrupts for more information.

\$\endgroup\$
-2
\$\begingroup\$

In my opinion you should not go for external hardware to fix your problem. Don't fix your software with hardware.

Assumption:

You want to read a simple tactile button as HMI. Just use some debouncing code in the main - no interrupts required.

#define ReadIO() {.....}
uint8_t ButtonState = 0;

bool debounce() {
  static uint16_t state = 0;
  state = (state<<1) | ReadIO() | 0xfe00;
  return (state == 0xff00);
}

main void(void){

    //Setup

    //Endless
    while(1){

        if (debounced()) {
           //Do action
        }
    }
}

You can now directly connect your switch to the port. Use the internal pulldown. No external components required.

Vcc->switch (normally open)->port (with internal pulldown)

\$\endgroup\$
1
  • 1
    \$\begingroup\$ But external hardware is the way to go, if this is supposed to use interrupts. And many devices don't have internal pull-downs, only pull-ups. \$\endgroup\$
    – Justme
    Commented May 22, 2022 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.