This is the Series of tutorials on the STM32 Microcontroller. The aim of this series is to provide easy and practical examples that anyone can understand. In this tutorial, we are going to see the STM32F407 GPIO Tutorial using STM32CubeIDE.
This tutorial aims to help beginners to understand the basics of STM32 GPIO programming using the higher-level abstractions, making it suitable for those who want to learn the fundamentals of microcontroller interaction. If you want to learn GPIO programming without HAL, you can read this Baremetal STM32 GPIO Tutorial.
You can also read, Getting Started with STM32 RTOS, STM32 GPIO RTOS tutorial, and STM32 Bootloader Tutorial.
Table of Contents
Prerequisites
Before starting this STM32F407 GPIO Tutorial, Please go through the below tutorials.
Hardware Requirements
- STM32F407 Discovery Board (ARM Cortex M4)
- LED
- Pushbutton
STM32F407 GPIO Tutorial using STM32CubeIDE
Writing On a Port Pin
Writing data to a designated port refers to transmitting data from the controller to any connected peripheral device. Here, an LED visualizes the data leaving the parallel port.
Prototype
HAL_GPIO_WritePin (GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
Reading from a Port
The reading operation involves retrieving data from any peripheral device. In this case, we are using a simple push-button (switch) to provide input to the port pins.
Prototype
HAL_GPIO_WritePin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
STM32F407VGT6 Discovery Board
This STM32F407VGT6 Discovery Bboard comes with a 32-bit STM32F407VGT6 microcontroller which belongs to the F4 family of ST microcontrollers and is based on ARM Cortex-M4 architecture. Microcontrollers that belong to STM32F407xx support up to 140 GPIO pins with interrupt capability out of which up to 136 fast GPIOs up to 84 MHz and up to 138 5 V-tolerant GPIOs. STM32F407VGT6 microcontroller which comes with a microcontroller has 82 GPIO pins. In the STM32F4 discovery board, only 5 GPIO ports are available to use on the two connector headers from PORT A, PORT B, PORT C, PORT D, and PORT E and a total of 80 GPIO pins are available to use from two headers.
LED Interfacing
The Discovery board features four user LEDs connected to the PD12, PD13, PD14, and PD15 pins of PORTD, with each LED linked via a current-limiting resistor.
- LD3: Orange LED is a user LED connected to the GPIO pin PD13 of the STM32F407VGT6
- LD4: Green LED is a user LED connected to the I/O PD12 of the STM32F407VGT6
- LD5: Red LED is a user LED connected to the I/O PD14 of the STM32F407VGT6
- LD6: Blue LED is a user LED connected to the I/O PD15 of the STM32F407VGT6
Here we are using onboard LED from Port D and pin 12, Further, we will see how to create a project using STM32cube IDE. To configure pin PD12 as an LED in STM32CubeIDE, follow these steps:,
Project Creation
- Open STM32CubeIDE and create a new STM32 project for your target microcontroller.
- Once the project is created, open the
.iocfile to access the configuration settings. - In the Pinout & Configuration tab, find the pin labelled PD12
- Click on PD12 to configure it. In the dropdown menu, select “GPIO_Output“. This sets the pin as a digital output.
- In the Configuration tab (on the left side), click on “GPIO” to adjust settings.
- You can configure the output level (default is usually low), speed, pull-up/pull-down resistors, etc., as per your requirements.
- In this tutorial I configured as no pull-up and no pull-down. Once all the configurations are done save with CTRL+S
STM32F407 GPIO Tutorial – Source Code
- Open the
main.cfile in theCore/Srcdirectory. In themain()function, you can toggle the LED with something like:
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET); // Turn LED on
HAL_Delay(1000);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); // Turn LED off
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
You can download the complete project from GitHub
Output
[To Be Added]
Push-button Interfacing
To use a push button with the STM32F407 Discovery Board, we will configure the GPIO pins of the STM32F407VG microcontroller as a digital input pin. With the help of LED Interfacing, we learned to use GPIO pins as digital output pins by blinking the onboard LEDs of the Discovery board. In this Push button Interfacing, we will control onboard LEDs of the STM32F407 Discovery board with an onboard push button.
The discovery board has a user button connected to Pin 0 of the PORTA. We will use this push button as a digital input to manage the onboard LEDs on the board.
When interfacing a push button with a microcontroller, our goal is to detect whether the button is pressed or not. Depending on the circuit configuration, pressing or releasing the button produces either a logic high or logic low output.
In the below schematic diagram, you will see that the onboard user push button is connected to the PA0 digital pin through a pull-down resistor. This setup means that when the button isn’t pressed, the PA0 pin will read an active low signal. When the button is pressed, the PA0 pin will read an active high signal.
Controlling LEDs with Push Button STM32F407 Discovery Board
To demonstrate, we will use a push button to control the onboard LED of the STM32 board.
- The pin A0 will read the input from the push button. If the state is active high, it will turn on the onboard LED of the STM32407 Discovery board. Since the LED is connected to the PD12 pin in a No pull-up and No pull-down configuration, it will typically show an active high output.
- When the push button is pressed, the LED will turn on; otherwise, it will stay off.
- Now, we will see how to configure the pushbutton as input. We already know the LED configuration from the previous example.
Further, we will see how to create a project using STM32cube IDE. To configure pin PA0 as an push button in STM32CubeIDE, follow these steps:
Project Creation
- Open STM32CubeIDE and create a new STM32 project for your target microcontroller.
- Once the project is created, open the .ioc file to access the configuration settings.
- In the Pinout & Configuration tab, find the pin labelled PA0
- Click on PA0 to configure it. In the dropdown menu, select “GPIO_Input“. This sets the pin as a digital output.
- Click on PD12 to configure it. In the dropdown menu, select “GPIO_Output“.
STM32F407 GPIO Tutorial – Source Code
- Open the
main.cfile in theCore/Srcdirectory. In themain()function, you can toggle the LED with something like:
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == 1) // if Button pressed
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET); // Turn on the LED
}
else
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET); // Turn off the LED
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
You can download the complete project from GitHub
Output
[To Be Added]
If you want to learn about other STM32 Microcontrollers GPIO Tutorials, You can read the below tutorials:
- STM32F411CE Black Pill board Tutorial
- STM32F103CBT6 Blue Pill board Tutorial
- STM32F767Zi Nucleo board Tutorial
You can also read the below tutorials.
Embedded Software Developer who is passionate about Embedded Systems.
Discover more from EmbeTronicX
Subscribe to get the latest posts sent to your email.



