How to use GPIO pins of SMT32F4 discovery board

In this tutorial we are going to go through step by step how to use Atollic True Studio for programming STM32F401VC and how to to blink an LED using STM32F4 discovery board. It is the most user friendly software for programming and using libraries.  We have already installed the software from the steps described in the previous tutorial. Now we will open it and start working.

  1. After Opening, your screen will look like this.
    First of all open Atollic True Studio, it will ask you to select the workspace you want to use. Select the workspace you want to use to store your projects.
  2. On the left side is the Project Pane. In it you can see all of your projects and its files.
  3. Middle space is for writing your code.
  4. Below code writing space is your space where you can see if any of the errors comes up.
  5. After that lets make a project.
  6. Navigate to File>New>C Project.
  7. Following window will open
  8. Name the project and select “Embedded C Project”
  9. Following window will open.
  10. Select STM32F4>MCU>STM32F401VC. I will show MCUs specifications You can select your own Micro controller as well.
  11. Click Next twice. And then select “ST Link” in debug probe selection.
  12. Click Next and then finish. It will make your working project. Wait for 10 seconds.
  13. When it will be done it will look like this.
  14. It will showing a simple main function. Here you will program you MCU.
  15. Libraries folder will contain all the libraries. Go to Libraries> STM32F4xx_StdPeriph_Driver>Inc. Here you will see all the peripheral Libraries header files.
  16. In project explorer pane Go to Libraries> STM32F4xx_StdPeriph_Driver>Src. Here you will see all the peripheral Libraries .c files.
  17. About rest of the files, don’t worry we will look on to these later.

Now lets see how to blink an LED using GPIO pins of stm32F4 microcontroller. First of all lets see what are GPIO pins. How to use GPIO pins either as a digital input or digital output.

GPIO pins of STM32F4

Each general-purpose I/O port has four 32-bit configuration registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR and GPIOx_PUPDR), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit set/reset register (GPIOx_BSRR), a 32-bit locking register (GPIOx_LCKR) and two 32-bit alternate function selection register (GPIOx_AFRH and GPIOx_AFRL).   

GPIO STM32F4  main features 

  •  Up to 16 I/Os under control
  • Output states: push-pull or open drain + pull-up/down
  • Output data from output data register (GPIOx_ODR) or peripheral (alternate function output)
  • Speed selection for each I/O
  • Input states: floating, pull-up/down, analog
  • Input data to input data register (GPIOx_IDR) or peripheral (alternate function input)
  • Bit set and reset register (GPIOx_BSRR) for bitwise write access to GPIOx_ODR
  • Locking mechanism (GPIOx_LCKR) provided to freeze the I/O configuration
  • Analog function
  • Alternate function input/output selection registers (at most 16 AFs per I/O)
  • Fast toggle capable of changing every two clock cycles
  • Highly flexible pin multiplexing allows the use of I/O pins as GPIOs or as one of several peripheral functions
  • GPIO functional description
    Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each
    port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in
    several modes:
  • Input floating
  • Input pull-up
  • Input-pull-down
  • Analog
  • Output open-drain with pull-up or pull-down capability
  • Output push-pull with pull-up or pull-down capability
  • Alternate function push-pull with pull-up or pull-down capability
  • Alternate function open-drain with pull-up or pull-down capability

I/O port control registers

Each of the GPIOs has four 32-bit memory-mapped control registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR) to configure up to 16 I/Os. The GPIOx_MODER register is used to select the I/O direction (input, output, AF, analog). The GPIOx_OTYPER and GPIOx_OSPEEDR registers are used to select the output type (pushpull or open-drain) and speed (the I/O speed pins are directly connected to the corresponding GPIOx_OSPEEDR register bits whatever the I/O direction). The GPIOx_PUPDR register is used to select the pull-up/pull-down whatever the I/O direction.

I/O port data registers

Each GPIO has two 16-bit memory-mapped data registers: input and output data registers (GPIOx_IDR and GPIOx_ODR). GPIOx_ODR stores the data to be output, it is read/write accessible. The data input through the I/O are stored into the input data register (GPIOx_IDR), a read-only register

Digital Output Configuration

Now we are going to jump into the world of programming of STM32. Starting from the basics we are going to cover the complex parts of its hardware. Now we already have the data  sheet and the reference manual. So what are we waiting for?? Lets make a pin High.

So let’s see what are the things we need to turn a pin high.

  1. Decide Port
  2. Clock Enable for port
  3. Pin number in Port
  4. Pin mode
  5. GPIO speed and Push Pull configuration
  6. GPIO Initialization

Lets discuss all these in detail.

  1. Decide Port

So ST32F401 has number total 9 ports from Port A to Port I. All of these have different functionalities. All of them can be configured for GPIO purposes. Every MCU has some pin attached to LEDs mounted on board. In case of STM32F401 LED are connected to pins

  • Orange LED is a user LED connected to the I/O PD13 of the
  • Green LED is a user LED connected to the I/O PD12 of the
  • Red LED is a user LED connected to the I/O PD14 of the STM32F401VC.
  • Blue LED is a user LED connected to the I/O PD15 of the STM32F401VC.
  1. Clock for port

In this step we are going to provide clock to the specific port we are using. Atollic libraries help us to do it easily. We have a function in the library which can Enable clock to any port in just one line.


Here RCC_AHB1Periph_GPIOD is a macro which decides that clock port D has to be controlled. And the second parameter “Enable”, its value is one, its counterpart is “Disable” which has value 0. So enable actually starts the clock to Port D.

We can control the clock of any port now.

  1. Pin number in port

Now we need to decide the pin number we want to control. As we know about the LEDs, let’s use these. Pin PD12, 13, 14, 15. So we can easily indicate whether the pin is high or not.First of all make a macro

                         GPIO_InitTypeDef GPIO_LED;

GPIO_InitTypeDef is a structure defined in gpio header file present in libraries folder. It is defined as follows

typedef struct


  uint32_t GPIO_Pin;              /*!< Specifies the GPIO pins to be configured.

                                       This parameter can be any value of @ref GPIO_pins_define */

  GPIOMode_TypeDef GPIO_Mode;     /*!< Specifies the operating mode for the selected pins.

                                       This parameter can be a value of @ref GPIOMode_TypeDef */

  GPIOSpeed_TypeDef GPIO_Speed;   /*!< Specifies the speed for the selected pins.

                                       This parameter can be a value of @ref GPIOSpeed_TypeDef */

  GPIOOType_TypeDef GPIO_OType;   /*!< Specifies the operating output type for the selected pins.

                                       This parameter can be a value of @ref GPIOOType_TypeDef */

  GPIOPuPd_TypeDef GPIO_PuPd;     /*!< Specifies the operating Pull-up/Pull down for the selected pins.

                                       This parameter can be a value of @ref GPIOPuPd_TypeDef */


So we are going to select the following parameters to configure a Pin.

  1. Pin Mode

No we need to configure it as output pin. So using the macro of structure we are going to type


So GPIO_Mode_OUT is also defined in Header file as follows

typedef enum


  GPIO_Mode_IN   = 0x00, /*!< GPIO Input Mode */

  GPIO_Mode_OUT  = 0x01, /*!< GPIO Output Mode */

  GPIO_Mode_AF   = 0x02, /*!< GPIO Alternate function Mode */

  GPIO_Mode_AN   = 0x03  /*!< GPIO Analog Mode */



  1. GPIO speed and Push Pull configuration

Now we are going to configure speed of  GPIO and Push pull register of the pin.we are going to it as follows.



These are again macros as previous ones. You can check these in the  header file.

  1. GPIO Initialization

Now we need to call the function which initializes all the parameters and makes the pin ready for use.


Here you cans ee GPIOD is the port we are using and &GPIO_LED is the macro we made for structure. After calling this function we can control the pin.

  1. GPIO Initialization

Now to turn on PIN





This function makes the pin High. Bit_SET has value 1. Similarly for turning it off.





It will turn the pin off.

You can get the code for Pin HIGH from downloads. Make a project. Copy the code. And lets see how to run it.

Building and running code.

Press the builf button at top.It will chech the code for errors.Now press the debug button and it will load the code and pause it and wait for you to press the start button. Press the button. And LED will glow.

Complete code

#include "stm32f4xx.h"

void delay(__IO uint32_t ncount)



int main(void)

GPIO_LED.GPIO_Pin=GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14|GPIO_Pin_15;


while (1)


led blinking stm32F4

5 thoughts on “How to use GPIO pins of SMT32F4 discovery board”

  1. Just something to be on the lookout for: The documentation I have seen for the STM32F401 shows the alternate function registers as AFRL and AFRH, yet the header files define them as array AFR[2].


Leave a Comment