Library 02- STM32F429 Discovery GPIO tutorial with onboard leds and button

Your first blinky project works, but you don’t know how?

I will explain GPIO (General Purpose Input/Output) CMSIS Library. This library is used to work with physical pins on microcontroller. You can set pins to input or output, put them low (0 volts) or HIGH (3,3 volts), select pull resistors, choose output type and select clock speed.

To be able to work with pin, you have to enable pin clock. This is set inside RCC (Reset and Clock Control). All GPIO‘s are on AHB1 bus. We will use GPIOG port, because our onboard leds are connected to pins PG13 and PG14. We can enable clock with code below:

Next, you need a GPIO_InitTypeDef struct to set options for pin. If you have included GPIO library into project, you can do this with following:

GPIO_InitTypeDef has 5 options:

  1. GPIO_Pin: Choose pins you will use for set settings
  2. GPIO_Mode: Choose mode of operation. Look down for options
  3. GPIO_OType: Choose output type
  4. GPIO_PuPd: Choose pull resistor
  5. GPIO_Speed: Choose pin speed

Every of this settings has it’s own options.

  • GPIO_Pin: With this you select pins.
  • GPIO_Mode: Mode of pins operation
    • GPIO_Mode_IN: Set pin to input
    • GPIO_Mode_OUT: Set pin to be an output
    • GPIO_Mode_AF: Set pin to alternating function (to use with peripheral ex. SPI, USART, etc)
    • GPIO_Mode_AN: Set pin to be an analog (ADC or DAC)
  • GPIO_OType: Mode for pin’s output type
    • GPIO_OType_PP: Output type is push-pull
    • GPIO_OType_OD: Output type is open drain
  • GPIO_PuPd: Select pull resistors or disable it
    • GPIO_PuPd_UP: Enable pull up resistor
    • GPIO_PuPd_DOWN: Enable pull down resistor
    • GPIO_PuPd_NOPULL: Disable pull resistor
  • GPIO_Speed: Select GPIO speed
    • GPIO_Speed_100MHz
    • GPIO_Speed_50MHz
    • GPIO_Speed_25MHz
    • GPIO_Speed_2MHz

We never told the script which port we will use because we have more pins 13 and 14 on board. Here comes the first GPIO function called GPIO_Init(). It is located in stm32f4xx_gpio.h

We want to initialize pins PG13 and PG14 (there are red and green leds on board) to be an output, push-pull output type, without pull resistor and speed 50MHz. We do this like this:

Our pins are now ready. But leds are still off. We need to turn them on:

Or if we would like to put leds back off:

Toggle pin is possible too:

On discovery board also has blue “User button” connected to PA0. Let’s configure it as input with enabled pull down resistor.

We have configured pin as input, but how to read value on pin?

This is all. I made a little program to test all. Leds are turned off until you press on blue button.

And because I don’t always want to initialize leds and button, I made a library that will do this for you.



  • Sets led on, off or toggle
  • Checks led state if it is on or off
  • Checks button state
  • Supports STM32F429-Discovery leds and button
  • Supports STM32F4-Discovery leds and button
  • Supports STM32F401-Discovery leds and button
  • Supports Nucleo F401 led and button
  • Supports Nucleo F411 led and button


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
  • TM
    • TM GPIO
    • defines.h

By default, library don’t know which board is used in your project, so you have to tell it. Open defines.h file or global project’s defines (IDE options) and add various defines, according to the board you use. If you not specify settings, you will get an error “tm_stm32f4_disco.h: Please select your board“.

Functions and enumerations


  • When you press the button, led will be turned on.

Project available on Github, download library below.

TM STM32F4 DISCO Library

Leds and buttons for Discovery boards

I hope this tutorial a little bit helps to you.


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!
  • bilal

    thanks its very helpful for me. im working on stm32f429 using keil uvision4. i just want to know how to get rid of this error. n how to add library in keil uvision4. thanks again
    main.c(11): error: #5: cannot open source input file “stm32f4xx_rcc.h”: No such file or directory
    #include “stm32f4xx_rcc.h”.

    • tilz0R

      Hi. I’m working on, how to set up my libraries inside Keil uVision 5. I will post soon as I got it 🙂

  • محــمد مهــدی سلطانی

    How can I read a port or a group of pins?

    • GPIO_ReadInputData function.

      • محــمد مهــدی سلطانی

        Thanks a lot…

        Is there any way to put the data on a port, to the DMA ?

        • Not directly.

          • محــمد مهــدی سلطانی

            what is the indirect way?

          • You have to configure timer to trigger DMA read, eg. DMA2_Stream5, Channel6 for TIM1 update.
            Set peripheral address to your GPIO IDR register and memory address to your 16bit variable to store data from PIN.

  • Yingqiu Cao

    Thanks for providing the libraries ! They are really useful. But this LED and Button library is not working on my STM32F4 Dicovery board. Debugging generates an error like “targed not recognized”. Guess there might be a typo in the code somewhere.

    • Follow the error.
      You must add global define in project. TM_DISCO_STM32F4_DISCOVERY, so my lib can recognize your board.
      Also, redownload lib.

  • Artem Razdymakha

    .STM32F4xx_StdPeriph_Driverinctm_stm32f4_disco.h(183): error: #35: #error directive: “tm_stm32f4_disco.h: Please select your board”

  • abdurrahman korkmaz

    please ,
    can anybody send me the example whole project for keil?

  • abdurrahman korkmaz

    .axf: Error: L6218E: Undefined symbol …. (referred from usartx.o).

    gives errorrs

    • because you’ve made changes in library filename!!!

      • abdurrahman korkmaz

        i didnt anything. im new user keil.please send a project zip file with any example.
        i want to discus it what file where or why. please…

  • Ronak Patel

    I’m trying to define my board (STM32F407VG) in the defines.h and I’m confused as to where “open project options” is located. I added this line in the define.h file, #define STM32F407VG

    • If you are using Keil uVision, then:
      “Options for target” (ALT+F7), tab “C/C++”, under “Define” add comma and type STM32F407VG

      • Ronak Patel

        Do I keep whatever is under Define already there? and add ,STM32F407VG to the end of it?

        • Correct.

          • Ronak Patel

            Got it. I ran into another problem. When you declared you 4 LED, are those the LED’s in the middle of the board? I pressed the blue button and the 2 LED’s didn’t turn on. Not sure if I’m making a mistake somewhere

          • Look example.

            TM_DISCO_LedOn(LED_RED | LED_GREEN); so only RED and GREEn led will be ON. Use LED_ALL to turn on all leds.

          • Ronak Patel

            Thank you thank you. I’ve just started using this STM board for my senior project and I was running into problems with the header directories. The template you provided is really helpful and convenient. Cheers for the help.

  • Fawad Aslam

    problem regarding pin configuration. i have an “ADC Regular conversion example” in which the gpio pin 10 is selected(example already given in srm32cubef4 folder i downloaded)

    #define ADCx_CHANNEL_PIN GPIO_PIN_10

    while you selected pins 13 and 14 which are on the discovery board. Can you tell me where are the other pins located or why in example (ADC Regular conversion) pin 10 was selected?

    • Pins PG13 and PG14 are pins for LEDs on STM32F429-Discovery board.

      I’m not using HAL drivers, expecially not Cube code generator.

      ADC has nothing to do with LEDs.

      • Fawad Aslam

        ok. but the gpio has 16 pins. how do i know the functions of other pins to use them?

        • One (and proper) way is to read datasheet, pin description for your specific device.
          Second option is to open STM32Cube, and when you select peripheral, click on pin on chip which is displayed and check it’s available options,

  • Katendencies

    Majerie thanks bro!!

  • phivu

    Dear sir,
    If I want to setup one pin as output at the first time but input at the next time, i wonder that how can I program like that

    • Oh man..set pin to output, do your stuff, set pin to your stuff.
      Simple as that. 2 times you will call GPIO_Init() function with corresponding parameters.

  • BrainworksStudios Nicolas

    Majerle! Thank you so much for explaining so easily an clearly! All the infos are in the same place. STM32 is so powerful but a nightware to start from scratch.

    Thanks for the hard work. Great site.

  • Jacky Cr

    Hi Tilen,

    Can we declare a variable in .h files? When I try to do it, I get ‘multiply defined’ error. The sample code is below

    • You can with some restrictions.
      You showed me lib.h file and inside this file you have include “lib.h” again.

      Take a look for “extern” keyword in C.

  • Chaitanya Allam

    Hi Tilen,
    Nice work!, I have a query for u. can we detect open wire in input apart from 0 and 1.

  • Cxl

    Dear sir,I want to disable some of the JTAG/SWD pins,but no way.

    • How you mean disable?
      To set them as output/alternate/input?
      Or how exactly? You wanna disable these pins forever?

  • John Doe

    Love your site!

  • marie

    Is there a possibility to read/write from/to the pins with another frequency than 2, 25, 50 or 100MHz? I am using a STM32F407. Thanks for your help!

    • These speeds does not mean that this is read/write frequency.
      This is the maximum GPIO PIN frequency pin can handle.

      So, if you set GPIO pin to 2MHz, then you can 4M times in one second change GPIO state and will signal be nice.
      If you toggle pin faster, signal might not be nice square anymore. It is somesort of capacitance load only for this setting.

      • marie

        Oh great, thanks for the fast answer, that was very helpful!

  • selfie mug

    what is library work in stm32f407vg?

    • “Make a clean and grammatically correct written meassage,”

      • selfie mug

        what you mean? work or not?

        • I mean that I don’t understand you.
          This lib works for any F4xx series.

      • selfie mug

        what you mean? work or not

  • Tyrion lannister

    thanks for your reply for my earlier doubt.
    please help me on this,

    Do i need to connect external pull ups for data and clock lines.(stm32l052r8)

  • Loarri

    Hello sir! Congratulation for your useful And Good work! One question. .. I have a doubt:which Ide software need to use with your code? Keil uvision or?At Now I use The online mbed Ide And I like It But i Think i Cannon use yor libraries with It right?thanks you Very much for your help!!!!!bye,Lo

    • You can use any IDE which has ARM based compiler, so some of them are these:
      – Keil uVIsion with ARMCC compiler (non-free)
      – Coocox with GCC (free)
      – OpenSTM32 with GCC (free)
      – Atollic TrueSTUDIO with GCC (non-free)
      – IAR (non-free)
      – Eclipse with GCC (free)

      I’m using Keil, but library works in any C compiler which supports ARM.

      • Loarri

        that’s good!many thanks for your very quickly reply!!!! 🙂 so I don’t know very well about these ide softwares…I have uvison on my laptop but I never used it because it seems rather difficult for me! so what is your suggestion to use?why did you not use the online ide from arm (mbed)? I use this one because It seems the easiest and similar language as Arduino.
        Many thanks again and ciao

        • I think that reasons like “it is online” and “seems the easiest and similar language as Arduino” are enough why I don’t use mbed 🙂

          I suggest Coocox or OpenSTM32.

          • Loarri

            ☺ok ok I ‘ll tray! Thanks again! ☺bye

          • jacky nguyen


  • Jacob

    Hello, I’m creating a mux controler and I would like to configure PF11 to be 1 on program start, and PF13, PF13 to be 0 on program start.

    So I set PF11 with pull-up and the others with pull-down. After I send compiled program to my STM32 all of the configured pins are 0. Changing bits later in the code works fine.

    Could you please tell me what am I doing wrong?

    • Your output type of pins is “PUSH-PULL” and you have it configured as outputs with pull-ups Pull-ups here does not have any point, because with PP mode, you can force pin to be high or low according to output register value.
      So, you can use Open-Drain mode on pin with pull-up where you can just force it to GND, pull-up makes logical 1 or force pin high with GPIOF->ODR register, or better:
      GPIO_SetBits(GPIOF, GPIO_Pin_11);

  • Farhan Musthafa


  • Anh Pôn Lê

    Hi Majerle Tilen , I just start learning stm32f4-discovery . Your lessons are great , easy to understand . But can I ask you a question that how can I define STM32F407VG in the define.h file

    • radekmh

      Simply write ‘#define STM32F407VG’ under the comment in this file that tells you to do it 🙂

  • Alejo Aldana

    Hi man nice libraries job !. I’m noob with STM32F4
    I’ve a problem. I add all libraries needed with .h and .c files, but always says “fatal error: (for example “tm_stm32_gpio.h ” file NOT FOUND) or any other, I defined in defines.h file but it still doesn’t work, what it could be !!. Thanks ! (and where I add the HAL driver files)

  • Mohamad Chamrami

    a lot of thanks