Library 32- Matrix keypad on STM32F4

I have about 10 matrix keypads from ebay, like this or this. They are nice, because with 8 pins I can control 16 buttons in matrix way. I made a really simple library to handle pressed button and use it in projects.

Library supports long pressed button. This feature works like on PC.

You can adjust refresh rate how fast this works. Everything is below.



  • Interface with 3 x 4 or 4 x 4 matrix keypad
  • Supports long pressed buttons
    • With style like on computer


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM GPIO
    • defines.h
KEYPAD STM32F4xx Description
C1 PD0 Keypad COLUMN 1
C2 PD1 Keypad COLUMN 2
C3 PD2 Keypad COLUMN 3
C4 PD3 Keypad COLUMN 4 (not used on small size)
R1 PC1 Keypad ROW 1
R2 PC2 Keypad ROW 2
R3 PC3 Keypad ROW 3
R4 PC5 Keypad ROW 4

Because this library works on some basic variable counter based debounce, I made some defines to tweak that to your needs, according to device speed you are working with. STM32F4xx can work with maximum frequency of 84MHz, 100MHz, 168MHz and 180MHz, but can work with lower value.

Keypad structure looks like in table below:

  • Large keypad size
    ROW 1 1 2 3 A
    ROW 2 4 5 6 B
    ROW 3 7 8 9 C
    ROW 4 * 0 # D
  • Small keypad size
    Column 1 COLUMN 2 COLUMN 3
    Row 1 1 2 3
    ROW 2 4 5 6
    ROW 3 7 8 9
    ROW 4 * 0 #

I set all possible defines into one example. There are also settings on how to change pinout. I made some settings that I think are the best. You can still change it in defines.h file. Below is file’s example for this project:

Functions and enumerations


This example was tested on large version of keypad (4 x 4). It works like this:

  • Button 0: Toggle all leds. If you hold this button, leds will be toggled and you can see, how fast they are toggled.
  • Button 1: Turn GREEN led on
  • Button 2: Turn GREEN led off
  • Button 3: Turn RED led on
  • Button 4: Turn RED led off
  • Button 5: Turn ORANGE led on
  • Button 6: Turn ORANGE led off
  • Button 7: Turn BLUE led on
  • Button 8: Turn BLUE led off
  • Button STAR: Turn on all leds
  • Button HASH: Turn off all leds

Project is available on Github, download library below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...