stm32 blink example

STM32 LED Blink Tutorial

By Robert Young
AD

The STM32 is a popular series of 32-bit microcontroller units (MCUs) by STMicroelectronics, widely used in embedded systems due to their performance, power efficiency, and robustness. A common task for beginners is to create a simple blinking LED project. This article guides you through an STM32 blink example, optimizing your learning experience and providing essential insights.

Before diving into the code, ensure you have the required hardware and software. You'll need an STM32 development board (like the STM32F103C8T Discovery board), an LED, and a USB cable. For software, install STMicroelectronics' STM32CubeMX tool for generating initialization code, and a C/C++ compiler such as makefile-based toolchains or platform-specific IDEs like SEGGER Embedded Studio or IAR Embedded Workbench.

Setting Up the Hardware

The STM32 board comes with built-in LEDs, usually connected to PA5 for LD2 and PA3 for LD3. To keep it simple, we'll be using LD2 for this example.

AD
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
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...

No additional hardware configuration is required for this basic blink example. We'll be using the built-in LED (LD2) connected to PA5, which is activated by setting the output bit to 1.

Identifying the LED Pin

First, it's crucial to identify the LED pin. In STM32CubeMX, click on 'Configuration' and go to 'Pinout & Configuration' > 'LED2'. You'll find that it's connected to PA5.

Download and Install STM32CubeIDE - Getting Started Guide
Download and Install STM32CubeIDE - Getting Started Guide
External Led Blink Using STM32F401CE
External Led Blink Using STM32F401CE
STM32 Guide #2: Registers + HAL (Blink example)
STM32 Guide #2: Registers + HAL (Blink example)

Once identified, enable the clock for GPIOA and set PA5 as output in the generated code ( initiated and performed by STM32CubeMX).

Modifying the Main Function

The main functionality of blinking the LED lies in the 'while' loop inside the 'while (1)' block.

AD

To make the LED blink, an toggle operation is performed on the GPIO_PINixel using bitwise NOT (!). Initially, the output bit is set to 1, turning the LED on. After a delay, it's toggled to 0, turning the LED off. This operation repeats, creating the blinking effect.

STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 : Basics of HAL with LED Blink example using STM32CUBE IDE
STM32 : Basics of HAL with LED Blink example using STM32CUBE IDE
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32: First Blinking LED Project
STM32: First Blinking LED Project
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver

Implementing Delays for Blinking

Delays are integral to the blink effect. The HALlibDelay() function generates delays in milliseconds.

Halfduplex communication protocols, like UART, require smaller delays. For full-duplex protocols like SPI, more significant delays can be implemented to avoid contention.

AD

Using HAL_Delay Function

The HAL_Delay() function can be used for simple delays. It provides a 1ms delay using a unitary access on DWT (Data Watchpoint and Trace) register, enabling accurate and minimal delay generation without requiring additional code.

STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
Getting Started with STM32 Blue Pill Using Arduino IDE
Getting Started with STM32 Blue Pill Using Arduino IDE
Blink Blonk
Blink Blonk
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
Blinking a LED in Multiple Sequences Using STM 32
Blinking a LED in Multiple Sequences Using STM 32
LED Blinking With STM32F103R6 Using SysTick
LED Blinking With STM32F103R6 Using SysTick
BLINKIG
BLINKIG
USB multimedia keys on Arduino STM32
USB multimedia keys on Arduino STM32
STM32 Blue Pill External Interrupts with STM32Cube IDE - HAL Libraries
STM32 Blue Pill External Interrupts with STM32Cube IDE - HAL Libraries
Canaduino STM32 boards with MicroPython
Canaduino STM32 boards with MicroPython
STM32F103C8T6 Blue Pill SysTick LED Blinking
STM32F103C8T6 Blue Pill SysTick LED Blinking
Push Button STM32 Blue Pill STM32Cube IDE - Read Digital Input Pins
Push Button STM32 Blue Pill STM32Cube IDE - Read Digital Input Pins

For larger delays, use the HAL_Delay() macro within a loop. For instance, for a 1000ms (1 second) delay, simply call HAL_Delay(1000).

Using SysTick Timer

For more complex timing mechanisms, use the SysTick timer, a dedicated part of the core for delay generation. It's more resource-efficient than using loop-based delays, especially for long delays.

AD

Enable the SysTick timer in your project's configuration. In the delay function (like the HAL_Delay()), increment a counter until the desired delay in milliseconds is reached. The SysTick HAL driver provides functions like HAL_SysTick_Counter_Start() and HAL_SysTick_Counter_Stop() for starting and stopping the timer.

Now that you've explored an STM32 blinking LED project, you're ready to advance to more complex tasks. Extend this project by incorporating user inputs, varying blink speeds, or even using different LEDs for a chase effect. Your learning journey has only just begun!

AD