stm32 hal blink led

STM32 HAL Blink LED Tutorial

By Sarah Williams
AD

Embarking on your journey in embedded system programming with STM32 microcontrollers? One of the first tasks you might want to tackle is blinking an LED. This simple yet insightful project helps you understand the basics of STM32 programming, HAL (Hardware Abstraction Layer), and how to interface with external hardware.

The STM32 HAL provides a convenient way to interact with the microcontroller's peripherals, making your code more organized and portable. In this guide, we'll walked through the process of making an LED blink using the STM32 HAL, ensuring that you have a solid foundation for your future projects.

Setting Up Your Development Environment

Before we dive into the code, let's ensure you have the necessary tools installed. You'll need the STM32CubeMX software to generate the necessary initialization code and project templates, as well as an IDE such as SEGGER Embedded Studio or Makefile-based environments like arm-none-eabi-gcc.

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

For this guide, we'll assume you're using Makefile and arm-none-eabi-gcc. If you're unfamiliar with these tools, we recommend checking out STM32CubeMX's online tutorials.

Creating a New Project with STM32CubeMX

Open STM32CubeMX and select your STM32 microcontroller, such as the STM32F103C8. Click 'Configure' to begin configuring your project. In the Configuration tree, select your LED pin (e.g., PC13 for an LED on a breadboard) and enable it as a 'General Purpose Output'.

Tutorial/blog to Get Start With STM32 Black Pill and STM Cube IDE to Blink External LED Using HAL Programming.
Tutorial/blog to Get Start With STM32 Black Pill and STM Cube IDE to Blink External LED Using HAL Programming.
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

Next, navigate to the 'Clock Configuration' tab and select the appropriate clock settings for your microcontroller. Finally, check the 'FreeRTOS' and 'USBFILES' middleware boxes if you want to use them in your project. Once you're done, click on 'Project' in the top menu and select your toolchain (Makefile) and project folder. Click 'Export' and wait for the code generation to complete.

Modifying the Generated Code

Navigate to your project folder and locate the 'main.c' file. This file contains the main function where your firmware will start executing. For now, we'll focus on making the LED blink by setting the pin output to 1 or 0.

AD

Find the 'while(1)' loop below the 'main()' function. Inside this loop, we'll flip the LED state using a simple delay function. You can find the 'HAL_Delay()' function in the 'stm32f1xx_hal_msp.c' file included in your project.

Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
STM32 Nucleo UART DMA with STM32CubeIDE and HAL Libraries
STM32 Nucleo UART DMA with STM32CubeIDE and 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...
STM32: First Blinking LED Project
STM32: First Blinking LED Project
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx

Blinking the LED

Now that your development environment is set up and you've generated the necessary code, let's make your LED blink! For this example, we'll make the LED blink at a rate of approximately once per second.

First, create a new function named 'LED_Toggle()' in 'main.c' to encapsulate the LED blinking logic. Inside this function, use the 'HAL_GPIO_TogglePin()' function to toggle the LED state. This function is part of the STM32 HAL library and can be found in the 'stm32f1xx_hal_gpio.c' file.

AD

Implementing the LED Toggling Function

After creating the 'LED_Toggle()' function, call it inside the 'while(1)' loop. To control the blinking rate, use the 'HAL_Delay(1000);' function, which pauses the CPU for 1000 ms (1 second).

Tutorial/blog to Get Start With STM32 Black Pill and STM Cube IDE to Blink External LED Using HAL Programming.
Tutorial/blog to Get Start With STM32 Black Pill and STM Cube IDE to Blink External LED Using HAL Programming.
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
How to Blink An LED Bulb Using ESP32 Board
How to Blink An LED Bulb Using ESP32 Board
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
Начинаем изучать STM32 или Управляем светом по-умному
Начинаем изучать STM32 или Управляем светом по-умному
STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 1 Blinking LED - Updated Oct 2017
STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 1 Blinking LED - Updated Oct 2017
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinking a LED in Multiple Sequences Using STM 32
Blinking a LED in Multiple Sequences Using STM 32
A Step by Step Guide to Get Started With STM32 Black Pill and STM Cube IDE to Blink On- Board LED Using HAL Programming
A Step by Step Guide to Get Started With STM32 Black Pill and STM Cube IDE to Blink On- Board LED Using HAL Programming
STM32 Blue Pill External Interrupts with STM32Cube IDE - HAL Libraries
STM32 Blue Pill External Interrupts with STM32Cube IDE - HAL Libraries
Начинаем изучать STM32 или Управляем светом по-умному
Начинаем изучать STM32 или Управляем светом по-умному
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
Push Button with STM32 Nucleo using STM32CubeIDE
Push Button with STM32 Nucleo using STM32CubeIDE

Your final 'main.c' file should look something like this:

AD
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();

  while (1)
  {
    LED_Toggle();
    HAL_Delay(1000);
  }
}

Building and Running Your Project

With the code modification complete, it's time to build and run your project. If you're using Makefile, navigate to your project folder in the terminal and type 'make'. If the build process is successful, you should see a '.hex' file in your project folder.

Using a program like SEGGER Embedded Studio, you can flash this '.hex' file to your STM32 board and observe the LED blinking. If you're using a different IDE, consult its documentation for instructions on how to flash the chip.

Now that you've successfully blinked an LED using the STM32 HAL, you've taken an essential first step in understanding STM32 programming and the HAL library. From here, you can build upon this knowledge to create more complex projects, such as controlling multiple LEDs, using timers, or even implementing a USB interface. Happy coding!

AD