stm32 hal blink led example

STM32 HAL: Blink LED Example

By John Jackson
AD

Embarking on a journey to explore and program STM32 microcontrollers is an exciting venture for electronics enthusiasts. A fundamental operation, often the first step in this journey, is to make an LED blink. This hands-on tutorial will guide you through blinking an LED using STM32's HAL (Hardware Abstraction Layer) library, a crucial tool that simplifies the interaction with hardware components. By the end of this article, you'll have a fully functional LED blinking example, setting a solid foundation for more complex projects.

Before diving into the code, ensure you have the necessary tools and components. You'll need an STM32 development board, an LED (any color works), and a breadboard to connect them. Additionally, you must have installed the STM32CubeMX software and the STM32CubeF4 toolchain, which includes the HAL library for the STM32F4 series microcontrollers.

The Hardware Setup

Connecting your LED to the STM32 board is a straightforward process. For this example, let's use PA5, a digital output port on the STM32F4 Discovery board. This port is connected to a green LED on the board, simplifying breadboarding. If you're using a different board or LED, adjust the port accordingly.

AD
STM32 : Basics of HAL with LED Blink example using STM32CUBE IDE
STM32 : Basics of HAL with LED Blink example using STM32CUBE IDE
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
Three External LED's Blinking Using HAL Programming
Three External LED's Blinking Using HAL Programming

1. Connect one end of your LED's cathode (negative leg) to a GND (ground) pin on your STM32 board.

2. Connect the other end, the anode (positive leg), to the PA5 pin on the board.

STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
External Led Blink Using STM32F401CE
External Led Blink Using STM32F401CE
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial

Creating a New Project in STM32CubeMX

Launch STM32CubeMX and choose your target MCU (STM32F4xx for our example). Configure the clock settings, avoiding changes unless necessary. In the Project Configurations pane, tick the 'Middlewares' option, then select 'HAL' to include it in your project.

In the Clock Configuration panel:

AD
  • Select the SYSCLK source oscillators and values.
  • Configure the HSE (high-speed external) oscillator if needed.
  • Options for the HSI (high-speed internal) oscillator are usually adequate for starting.

Generating the Code

Once configurations are set, click 'Projects' and select your desired project name and save location. Click 'Generate' to create the project files.

Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
STM32: First Blinking LED Project
STM32: First Blinking LED Project
Bare-Metal STM32: Blinky And The Secret Of Delay Functions
Bare-Metal STM32: Blinky And The Secret Of Delay Functions
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
STM32 Nucleo UART DMA with STM32CubeIDE and HAL Libraries
STM32 Nucleo UART DMA with STM32CubeIDE and HAL Libraries

Open the generated project in your preferred IDE (e.g., SEGGER Embedded Studio or Makefile). Notice the main.c file containing a basic main function provided by the generated HAL library.

Blinking an LED with HAL

In this section, you'll utilize HAL to make your LED blink. By the end, you'll have a working LED blinking example using the HAL library.

AD

Including the Necessary Headers and Defining the LED Pin

Add the following header at the beginning of your main.c file.

STM32 Blue Pill External Interrupts with STM32Cube IDE - HAL Libraries
STM32 Blue Pill External Interrupts with STM32Cube IDE - HAL Libraries
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
How to Blink An LED Bulb Using ESP32 Board
How to Blink An LED Bulb Using ESP32 Board
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
Начинаем изучать STM32 или Управляем светом по-умному
Начинаем изучать STM32 или Управляем светом по-умному
Blinking a LED in Multiple Sequences Using STM 32
Blinking a LED in Multiple Sequences Using STM 32
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinkenlights To Bootloader: A Guide To STM32 Development
Push Button with STM32 Nucleo using STM32CubeIDE
Push Button with STM32 Nucleo using STM32CubeIDE
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
Blink Blonk
Blink Blonk
Program \
Program \
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
Начинаем изучать STM32 или Управляем светом по-умному
Начинаем изучать STM32 или Управляем светом по-умному
Four ways to make leds blink
Four ways to make leds blink

#include "stm32f4xx_hal.h"

AD

Define the PA5 as an output pin in the following manner:

#define LED_PIN GPIO_PIN_5
#define LED_PORT GPIOA

Initializing GPIO in the 'main' Function

Inside the 'main' function, initialize the HAL by evolving the SystemClock_Config() function. Then, initialize the GPIO with the following code:

GPIO_InitTypeDef GPIO_InitStruct = {0};

__HAL_RCC_GPIOA_CLK_ENABLE();

HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);

GPIO_InitStruct.Pin = LED_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct);

Blinking the LED

To blink the LED, use a simple while loop indefinitely setting the LED state high then low with a small delay in between:

while (1)
{
  HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
  HAL_Delay(500);
}

Building and Running the Code

Compile the generated code and load the HEX file onto your STM32F4 discovery board. After a successful upload and reset, your LED should be blinking on and off every half a second.

Congratulations! You've successfully blinking an LED using the STM32 HAL library. This foundational exercise sets you up to explore more interactions with peripherals and complexities in STM32 programming. Happy tinkering!

AD