STM32 Guide: GPIO and Buttons
By: Sanskar Biswal
Tags: C Programming, Stm32, Electronics, Tutorial, Embedded Systems

Fig.1 STM32 GPIO

STM32 is a diverse family of 32-bit microcontroller used in a wide variety of applications today, ranging from simple toys to even complex control systems like home appliances. As a result, being able to develop applications on this platform is an essential skill. With that in mind, let us start with learning how to develop apps using simple GPIO and Buttons.

Prerequisites

This is not a tutorial for C programming, therefore going forward I shall assume a good understanding of C and it basics.

If you are a beginner to STM32 CubeIDE, I suggest you to refer to another blog I have written on getting started with CubeIDE.

It suggested but not necessary to have access to a STM32 discovery kit, preferably the STM32F407-Discovery Board. If you have another board, you can still follow along as the core concepts and code syntax will remain the same across target devices.

STM32 GPIO and HAL Library

The STM32 HAL

Hardware Abstraction Library(HAL) is a part of developer libraries provided by STMicroelectronics for ease of development. The abstraction library is a set of functions and definitions which make it possible for developers to focus on getting their codes running instead of being bogged down with setting all the plethora of registers and bits to get a simple I/O operation possible. Using the functions in the HAL library, developers can simply call a single function to perform operations like read I/O data, or even perform complex processes like SPI or I2C.

STM32 GPIO

GPIO stands for General Purpose Input-Output. All pins on a STM32 microcontroller operate as a GPIO. These pins however can perform only a pre-defined set of operations depending on how the microcontroller registers for the GPIO is initialized. These modes of operations are:

Digital Input

Digital Output

Analog I/O

External Interrupt

Note: The HAL Documentation for STM32F4 is available in this link: https://www.st.com/resource/en/user_manual/dm00105879-description-of-stm32f4-hal-and-ll-drivers-stmicroelectronics.pdf

LED’s and Button on Discovery Kit

Fig.2 STM32 Board Button + LED

The STM32F4 — Discovery Kit has 4 user accessible LED and a single Push-Button. In the image, it is the blue button.

The LED’s are connected to pins 12,13,14 and 15 on Port D of the GPIO.

The push-button is on Pin 0 of Port A. Since, these values are fixed, in this tutorial, we will follow the same convention.

Note: If you have a different configuration, all you need is to follow this tutorial while using the correct port and pin numbers. Thanks to STM32 HAL, the function call will remain the same.

HAL GPIO Functions

HAL_GPIO_ReadPin(gpio-port, gpio-pin)

HAL_GPIO_TogglePin(gpio-port, gpio-pin)

HAL_GPIO_WritePin(gpio-port, gpio-pin, pin-state)

There obviously are other functions for GPIO like locking and interrupts, but in this tutorial we will be focusing on only these and in most cases, you will be using only these as well.

Toggle an LED

The red LED on the board is at pin GPIOD.12. This information is available on the datasheet for the discovery board. I will provide a link in the Sources section of the article.

Here, the things to note are that our port is GPIOD and pin is GPIO_Pin_12. This is as per the HAL conventions. gpios’ are referred as GPIOx and pins as GPIO_PIN_x. If you peek into their definitions, you will find that GPIOD is actually a 32-bit memory location as is GPIO_Pin_12.

Also, we need to add a delay after each toggle. If we don’t do this, the LED will toggle so fast that our eyes will not be able to perceive it. For that, we use HAL_Delay(x) function. x here stands for the number of milliseconds of delay.

Thus, in the while loop of the main function, we add the following code.

HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);

HAL_Delay(500);

Hit Fn-11 key to start building and debugging. The LED should toggle with half second interval.

LED Control with Button Input

A more practical approach is turning an LED on when a button is pressed. This an example of real-time application of microcontroller and this same design can be upgraded to more complex solutions with relative ease.

The process flow involves turning the LED off before loop execution, then wait for button input. If the button GPIO is 1(logical HIGH), then the LED turns on, else LED remain off.

The following code snippet is a demo:

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12,GPIO_PIN_RESET);

while{

if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == 1){

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);

}else{

HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);

}

}

Hit Fn-11 key to start building and debugging. The LED should turn on each time the button is pressed and turns off when the button is released.

The above code snippet reads the button pin each execution cycle and writes the corresponding output to the LED. This however, consumes a lot of power as there is unnecessary code execution if no button is pressed or released. The better way to do this is by checking and writing LED state when the button is pressed, ie when there is an interrupt from the button. A blog about using interrupts will be out soon, going into the details of this process.

Conclusion

In this tutorial, we discussed the methods for using Buttons and LED’s in our hardware setup. The same code will work of any GPIO pin connected to the LED or the button.

Additionally, this was the first introduction to the HAL library.

Sources: