Library 11- Button library for ILI9341 LCD and STMPE811 touch controller on STM32F429 Discovery board

If red led is turned on, than bottom 2 buttons work, otherwise they are deadNow we have both, LCD controller and touch controller configured and we can use this. For this purpose i made a library for buttons. This library can draw buttons on LCD and check if touch has been pressed on any enabled button.



  • Set X and Y location of button
  • Set button’s width and height
  • By default 10 buttons are supported
  • Use labels
  • Custom background image or color
  • Border around button
  • Selectable font
  • Disable/Enable button
  • Selectable color for label, background and border


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx I2C
    • STM32F4xx SPI
  • TM
    • defines.h
    • TM DELAY
    • TM ILI9341
    • TM STMPE811
    • TM FONTS

If you have more than 10 buttons, you can make a define in your project’s defines.h file

Main structure

Add buttons

To add button to library, you have to create an instance of TM_ILI9341_Button_t structure first

Then fill instance with settings, like this

and add button to library with

Button operations

Draw button

Enable/disable button

  • By default, every button is enabled
  • If button is disabled, than is ignoring by touch

Delete button

Recognize pressed button

To recognize pressed button, first you have to get touch coordinates. Then, use

and pass touch data as parameter. If any button is pressed, then his id is returned, otherwise -1 is returned.


Example has 3 buttons

  • Button 1 is basic with red background and black border with “Button 1” label
  • Button 2 has disabled label with custom background with black border
  • Button 3 has “Button 3” label with custom background and without border

On board are two leds. If red led is turned on, then buttons 2 and 3 are enabled, otherwise are disabled.

Buttons operations:

  • With button 1 you toggle enable/disable of buttons 2 and 3
  • If button 2 is enabled, then it’s used for turning green led on
  • if button 3 is enabled, then it’s used for turning green led off

Project available on Github, download library below.

TM STM32F4 ILI9341 Button Library

BUTTON library for ILI9341 LCD and STMPE811 touch screen controller


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!
  • Johnny Dreher

    Hello, I’m trying to compile this example at Coocox, and it works, but this became too large and too slow. Can you help me? Do you imagine what can be?

    • I’m not sure what do you mean with too large and too slow. Make sure your device is running at 180MHz, you have enough flash to drive this code.

      • Johnny Dreher

        Hello Again, now I can do this work. I have another doubt, I’m trying to fill my buttons with a solid color, but I don’t found where to config this, I only find how to config with a image

        • Look at button 1 configuration in example.

  • Bican Guzeler

    keil gives fatal error about size limit for this exercise, why is that ?

    • Looks like you have a free version.

      Try to use GCC compiler instead.

      • Bican Guzeler

        where can i downlaod it ?

        • Use Coocox, it has GCC compiler inside, it’s free.
          My libraries are tested and works in GCC as expected.

          • Bican Guzeler

            Okay, can i open your projects with coocox, because i am using your github projects directly ?

          • Not directly.

            You have to open keil and look which libraries i have included in project, include them also in coocox.

            And copy/replace main.c and defines.h file in coocox (defines.h you have create manually) and you should be able to successfully run.

            Also, check my project created for coocox. Coocox by itself does not support F429 device, so i made manually one project. It is on the link below:


            Download project, open in coocox and copy/replace my libraries inside, activate peripheral from files included in project (do not use Coocox files that are included). They are in cmsis_lib/source. You will need LTDC, FMC, SPI, I2C, GPIO, RCC, maybe something else to. Contact me via mail or skype and I will help you if you will have any problems.

          • Bican Guzeler

            thank you so much 🙂

  • Hi,

    I’m using CoIDE, when I try to link this project I get this errors:
    [cc] c:/program files (x86)/gnu tools arm embedded/4.8 2014q2/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/bin/ld.exe: FG001.elf section .data' will not fit in region ram’
    [cc] c:/program files (x86)/gnu tools arm embedded/4.8 2014q2/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/bin/ld.exe: region ram overflowed with stack
    [cc] c:/program files (x86)/gnu tools arm embedded/4.8 2014q2/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/bin/ld.exe: region ram' overflowed by 18720 bytes
    [cc] c:/program files (x86)/gnu tools arm embedded/4.8 2014q2/bin/../lib/gcc/arm-none-eabi/4.8.4/../../../../arm-none-eabi/lib/armv7e-mlibg.a(lib_a-sbrkr.o): In function
    [cc] sbrkr.c:(.text._sbrk_r+0xc): undefined reference to `_sbrk

    Anybody has any Idea, what should I do?

    Thank you,

  • Facebook User

    I have a STM32F4 Discovery Kit and I program it with Emblocks

    I have tried Library 11 with the suggested main code.

    However when I compile the source I get the following error in 3 lines:

    ….tutorial STM32F4 GLCD and TouchTMtm_stm32f4_ili9341.c|186|error: ‘SPI5’ undeclared (first use in this function)|

    It looks the definition of ILI9341_SPI as SPI5 in tm_stm32f4_ili9341.h is not recognized by the compiler.

    Any solution?


  • Facebook User

    Do you mean the Standard Peripheral Libraries? These are embedded in the emblocks IDE (i.e. are created in the project folder when a new project is created) and my emblocks version (2.30) is the most recent available.

    • OK,

      you can still download STD drivers from ST and replace your files in your project 🙂

  • Facebook User

    The problem is that in the stm32f4xx_spi.h file that emblock creates with any new project only SPI1, SPI2, SPI3 are defined.
    I downloaded a more recent version of the Standard Peripheral Drivers where SPI5 is defined and imported in the project.
    However when I compile the project I get a huge amount of warnings and undeclared errors.

    Do you think is it possible to solve the problem by replacing SPI5 with SPI3 in the drivers I downloaded from this site? I think it could be much simpler.


    • LCD is connected to SPI5, so you can’ use anything else than SPI5.

  • Petr Machala

    Does anyone have problem with the I2C inicialization? Everything works well but I get stuck right at start of the I2C transfer on timeout…

    while (!(I2Cx->SR1 & I2C_SR1_SB) && LCD_I2C_Timeout) {
    if (–LCD_I2C_Timeout == 0x00) {
    return 1;

    • Petr Machala

      It is obviously a SW problem because STM demo touch screen works fine…

    • Hi Petr,
      sorry for delay.
      I was checking for this, and you were right. Library was not working properly.
      Now it should.

      • Petr Machala

        Delay? I am amazed by your quick reply…I cant get anything done on standard weekday…Which library wasn’t working properly? What did you change? … I am just wondering because everything seemed to be fine to me yesterday…

        • Huh, well I2C for touch had problems. I2C lib is the one ive updated today. Yesterday I didnt updated anything related to you issue.

          • Petr Machala

            Nice…it works great…just a note, I noticed that you use internal pullups but STMPE811 already has external 4k7 pullups… but I get that I2C library is general not just for 32F429IDISCOVERY…personally I would use NOPULL for PA8 and PC9… Internal pullups are enormous … my two other I2Cs dont works very badly with internal pullups…anyway amazing…thanks…

          • By default, I will leave pullups on all pins, because I2C is generic I2C library, not just for touch and on I2C you can connect anything.
            Buuut, I can add define option to disable pullups on pins, if you really think this would be good idea.

          • Petr Machala

            That wasnt a complaint…itwas just a note 🙂

  • Petr Machala

    I dont want to be a buzzkill but your update of I2C is still not without bugs…Sometimes (like 1 in 10 times ) it still gets crashes right from the starts a gets stuck there…

    while (!(I2C3->SR1 & I2C_SR1_SB)) {
    if (–TOUCH_I2C_Timeout == 0x00) {
    return 1;

    I am using another I2C for emulating SCCB bus and sometimes I experiance exacly the same problem….I was thinking that this is because of quite long connecting cables but obiously not…tomorrow I’ll look at it and post results if sucessful…

  • Agnieszka Job

    hello 🙂 Could you tell me what i miss now :
    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_GPIO_Init (referred from tm_stm32f4_ili9341.o).

    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_SPI_Init (referred from tm_stm32f4_ili9341.o).

    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_SPI_Send (referred from tm_stm32f4_ili9341.o).

    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_I2C_Init (referred from tm_stm32f4_stmpe811.o).

    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_I2C_Read (referred from tm_stm32f4_stmpe811.o).

    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_I2C_ReadMulti (referred from tm_stm32f4_stmpe811.o).

    .Objectsbuttons.axf: Error: L6218E: Undefined symbol TM_I2C_Write (referred from tm_stm32f4_stmpe811.o).

    • Tm I2C, spi and gpio libs 🙂

      • Agnieszka Job

        Ahhh….. if I forgot some libraries and I build the project in Keil I see errors.I add libraries and then rebuild but it has no result 🙁 When I create new project with all librariers everything is fine. Thanks 🙂

  • Navinder Singh

    you havn’t provided button_back.h, excluding which i discovered buttonBackground goes undeclared.

    …i m just a beginner, so pls don’t mind if i ask stupid things

    • Just delete that part there.
      This is array of colors, so you can also make your own array.
      Its size should be button_height * button_width.

      Other, on my github is located this file if you open that project there.

      • Navinder Singh

        Still some errors:

        In KEIL, upon building, 3 errors (pic1), could not fix upon including tm_stm32f4_spi_dma, tm_stm32f4_dma and tm_stm32f4_dma2d_graphic (pic2).

        In Coocox, ERROR:….ram overflow…(pic3), and “collect2.exe: error: ld returned 1 exit status”

        • You are missing my SPI DMA library, DMA library from standard peripheral drivers and DMA2D library.
          Also, note that ILI9341 with LTDC does not work with this library. You have to make changes to get ILI9341 working with button library using LTDC.

          • Navinder Singh

            Please repeat once, for i am using keil and opening your project file gives me error(pic1), ….Undefined symbol TM_SPI_DMA_***.

          • Then redownload github projects. They are updated. Last update was about week ago of this projects and it works.

        • Navinder Singh

          LITTLE INTRO:

          PAST: 2 yrs working with AVR. Recently completed Embedded System using ARM on edx.
          PRESENT: Was reading reference manual, datasheets,etc. for stm32f429 for some time, then checked this website and in one perspective became its fan…

          Its COOL.

  • leonardo

    I’m trying to test this project, but size limiting on keil seems to be a problem:

    any help on it?

  • Giona

    Sorry, what’s #include?? I can not find this library nowhere.

  • Mathias

    Hi, sorry about the questions but I would like to know if this works with the display module (dm lcd35rt without the base board) that has de ssd2119 driver? Thanks a lot!

    • Hi, it does not.

      • Mathias

        Hi, do you have the ssd2119 driver? i cannot find it or cannot find a project that worked, thanks a lot!

    • Comment not related with post, but anyway, i dont have.

  • Michele

    Hello, first of all I want to thank you for the awsome tutorial you wrote down to help all of us.
    I try to implement my code with the introducing the opportunity to use the touchscreen for customize the configuration of what the use can do with the code on the program; I’m quite new in the use of Keil and stm32f4 board, and I don’t really know how to solve the problem that I see when I build the code. The errors are the three below.
    Thank for help, and sorry for my english.

    • Errors are not part of my lib.

      • Michele

        I found them in my main code. Now all is ok. Thanks again, you are really fantastic.