GPIO Key Debounce State Machine

State Machines · flowchart diagram · unknown license

Illustrates a robust debouncing mechanism for a GPIO key press, managing state transitions to accurately detect button presses and releases in embedded sys

Source: https://github.com/YueyangJiang97/stm32_cours/blob/144532faba58db904d15680fcda8782492d32ecc/README/organ_key_mode2.md
Curated by YueyangJiang97
Debounce GPIO Embedded State Machine Button Microcontroller Flowchart

Mermaid source

graph TB
t1(start)
t1-->t2(init)
t2-->t3(get current tick value: prev_tick, set status to 0 )
t3-->t4(get current tick value: cur_tick)
t4-->t5{if cur_tick > prev_tick+10}
t5--no-->t4
t5--yes-->t6(prev_tick = cur_tick)
t6-->t7{key_gpio_pin == 0, }
t7--yes-->t8{status == 0}
t8--yes-->t9(ststus = 1)
t8--no-->t10{status == 1, key is pressed for 10ms}
t7--no-->t11{status == 2}
t11--yes-->t12(status = 0 Key released)
t10--yes-->t13(status = 2, KEY PRESSED)

What this diagram shows

This flowchart details the logic for debouncing a GPIO key press. It tracks current and previous tick values to implement a delay, and uses a 'status' variable (0, 1, 2) to manage the key's state: released, pressed for 10ms (debounced), and fully pressed/released.

When to use it

Use this diagram when designing embedded software for microcontrollers (e.g., STM32) that require reliable detection of physical button presses, preventing multiple triggers from a single press due to mechanical bounce.

How to adapt it for your project

This logic can be adapted by changing the debounce delay (e.g., 'prev_tick+10' to a different value), integrating it into an interrupt service routine, or extending the state machine to handle long presses or double clicks. The 'key_gpio_pin' can be generalized to any digital input.

Key concepts