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. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Tim


    Love your posts. They are very helpful as I am new to this MCU.

    I am trying your 4×4 keypad. When I tried to compile, the delay.h gives me the following syntax error:
    __weak void TM_DELAY_1msHandler(void);
    __STATIC_INLINE void Delayms(uint32_t millis)

    The bold ones are the ones that made the compiler unhappy. I am using the STM32F407 btw. Thanks in advance.

    • Hi,

      which compiler do you use?
      Did you include attributes.h file? __STATIC_INLINE is standard CMSIS define for ARM.

      • Tim

        I am using Coocox. And yes, I did include that.
        Thanks for the prompt reply Majerle

        • Can you post error log?
          I don’t know why it fails, should work just great.

          • Tim

            cc] ..objmain.o: In function main':

            [cc] C:CooCoxCoIDEworkspacekeypad/main.c:31: undefined reference to TM_DISCO_LedInit’

            [cc] ..objtm_stm32f4_usart.o: In function TM_USART_Init':

            [cc] C:UsersTimDropboxDocumentsSchoolECE493Keypadtm_stm32fusart/tm_stm32f4_usart.c:250: undefined reference to NVIC_Init’

            [cc] c:/program files (x86)/gnu tools arm embedded/4.8 2014q3/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-mlibg.a(lib_a-sbrkr.o): In function _sbrk_r':

            [cc] sbrkr.c:(.text._sbrk_r+0xc): undefined reference to _sbrk

            [cc] ‘

            [cc] collect2.exe: error: ld returned 1 exit status

            BUILD FAILED

            Total time: 2 seconds

          • You don’t have my DISCO lib (leds and buttons) and you don’t have _sbrk implemented which is important for sprintf.

            Also, you are missing misc.h file from ARM (or STM32F4xx CMSIS files). This file is used for NVIC.

          • Tim

            Yes you are correct. I added disco.h but did not add disco.c. Silly me 🙁
            Regarding the _sbrk , is it implemented somewhere in your files? Or do I have to do that myself?
            And I manually added the misc.h from your files on github. Not sure if that’s the correct one. I tried to look for it in the repository but could not find it
            Thanks again for your help. I really appreciate it!

          • I think _sbrk has Coocox implemented, you have to enabled it. Another option is that you enable it by yourself.

            Yes, misc.h/.c is ok from my libs. That’s the file you need, but it is also in Coocox included.


            From picture above, you need “Retarget printf” and MISC.

          • Tim

            Hmm. No idea why I could not find MISC in the repository, but I manually added misc.c and the NVIC error is gone now. Still left with [cc] sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk’ error though. I do not know how to fix this

          • saeid

            hello. How can i use this library with interrupt?

  • Tim

    Sorry for bothering you again Majerle.
    I got the Keypad to work. I am trying to combine my working OLED (SPI, 128×32, driver SSD1306) with your working keypad code so that the key pressed will be displayed on the OLED. I don’t know why as soon as I included my initialization for the OLED, both the OLED and the keypad stop working. At first, I thought there were some conflicts, but my OLED uses GPIOA and GPIOE, while the keypad uses GPIOC and GPIOD. Is there anything you can think of that can be causing the problem? Thanks again Majerle

  • samit

    Hello… can the examples of stm32f429 will work on board stm32f103r?

    • No directly. Some low level modifications are required.

      • samit

        can you tell me what sort of modification? I am facing a problem as i am not getting examples of that particular board

        • No examples for 103? Wow thats new to me.

          Peripheral modifications need to be done like gpio, rcc, and so on.

          • samit

            i have gpio, rcc library of stm32f103…I just need to change pins .and use peripheral library of stm32f103? right??

      • samit

        we just need to change pins?? right?

        • In matrix keypad yes.

          • imad

            I m taking about the STM32 ports. how did you associate PD and PC ports to the pins ?

  • Derek Mundondo

    Hello Tilen

    I am having the error in the attached error log. Can you please assist me. have the LCD library running, but when I add the keypad library to the project then I get this error. The confusing part is that I have defined all the pins I am using in the defines folder.

    Is there anything I might be overlooking.

    Thanks in advance


  • Tính

    Hi guy,
    This a good library, But how can I work with matrix 5×5 when your library can’t repare. I hope that. Thank you so much!

  • abdallah

    plz can you give me full project code if you can i use COOCOX

    • No.

      • abdallah

        i have a problem

        • abdallah

          i dont know who to use this fils in coocox

  • imad

    Hi i’m having hard time to understand how did you physically connect the pins to the keypad??

    THX in advance

    • Search for matrix keypad on google.

      • imad

        I m taking about the STM32 ports. how did you associate PD and PC ports to the pins ?

        • Im verybad with crystal sphere, so please explain what you wanna hear from me.

          • imad

            what s the line in your code that specifies a GPIO port (pin) to the keypad input ?

  • vm

    hello sir,
    can u tell me how to solve switch de bounce in this matrix keypad code ?
    single switch i know but i am not getting any logic to solve issue with keypad matrix.

  • Amol Varwade

    compiling tm_stm32f4_keypad.c…

    ..tm_stm32f4_keypad.c(98): error: #136: struct “” has no field “BSRRL”


    ..tm_stm32f4_keypad.c(99): error: #136: struct “” has no field “BSRRL”


    ..tm_stm32f4_keypad.c(100): error: #136: struct “” has no field “BSRRL”


    ..tm_stm32f4_keypad.c(102): error: #136: struct “” has no field “BSRRL”


    ..tm_stm32f4_keypad.c(107): error: #136: struct “” has no field “BSRRH”


    ..tm_stm32f4_keypad.c(110): error: #136: struct “” has no field “BSRRH”


    ..tm_stm32f4_keypad.c(113): error: #136: struct “” has no field “BSRRH”


    ..tm_stm32f4_keypad.c(116): error: #136: struct “” has no field “BSRRH”


    ..tm_stm32f4_keypad.c: 0 warnings, 8 errors

    please give me solution for this problem

    • You have to modify macros for setting pins high, low, as input and output.
      It worked on previous SPL drivers on F4.

      • Amol Varwade

        which macro i have to modify in which file it was defined

  • Tomasz

    Hello Tilen,
    I’ve used your keypad library with success. However, sometimes I was getting double triggers after pressing a key.
    I resolved it the following way: in TM_KEYPAD_Update handler I moved the call
    to TM_KEYPAD_INT_Read() outside the if{} (so it is now the last instruction in the handler).
    Now the keys are being scanned faster.
    It might be a bug but honestly I only checked it with one keyboard (the one I use for my project).
    Bit it’s working like a charm 🙂

    • ezequiel

      hello a query I am new to this, I consult you charge the 3 files and I would have to walk? or do we have to schedule something else?

  • Gaurav Mishra

    What modifications need to be done if I am using STM32L476RG MCU?

  • Electrotwelve

    Wouldn’t you need pullups on the inputs (or pulldowns on the outputs)? Otherwise on each key press you’d end up shorting VCC and GND.