2025-08-30 5:31 PM
Hello,
What is the best way to drive a 4x4 keypad, taking in account power consumption and MCU life expectancy?
Option 1: Continuous scan of columns and read rows.
Option 2: Like option 1, but including delays (0.01 s) to reduce MCU stress.
Option 3: Leave all columns to high or low level and use an EXTI for every row. In the EXTI function, scan columns one only time to see the pressed key.
Or doesn't matter and can use any option?. Looking technical data, the main thing to have in account for power consumption is the clock frequency.
Now using nucleo-F446RE, and when the project is finished, I will migrate to other MCU
Thanks
2025-08-30 9:05 PM
Only scanning when you know that a key is pressed can be the lowest power solution. This way you can have the processor in one of its low-power modes (sleep or one of the stop modes that still allows EXTI to work) while waiting for a keypress and there is nothing else to do.
I do not think “mcu stress” is something that should be “reduced” in software. If you have a bad hardware design that stresses the processor beyond its normal operating conditions then fix the hardware. But if all you’re doing is waggling a few GPIO lines between input-pull-high and output-low (so you never get two outputs fighting if the user happens to press two buttons at the same time) with only a few picofarads of stray capacitance, you are not stressing the stm32 in any way.
However, “best” is a very broad term. It’s no good having lowest-power code if it doesn’t work. The more complicated code becomes, the harder it is to ensure there are no bugs in it.
One other thing to be aware of is switch bounce. Mechanical switches very often do not make or break cleanly - as you press them they might connect then disconnect a few times before finally stabilising as connected. Your code needs to ignore the bounces, which might last many milliseconds, and correctly record just the user’s single press or release. This is often easier if keypad scanning is done at fixed time-intervals so you can base your decision on how many cycles all show the same keypress state.