STM32 LED Blinking Using HAL Programming in Wokwi
- shaiktauheed1010
- 2 days ago
- 6 min read
Introduction
The STM32 family of microcontrollers, developed by STMicroelectronics, is widely used in embedded systems due to its high performance, low power consumption, and extensive peripheral support. These microcontrollers are commonly found in applications such as industrial automation, Internet of Things (IoT) devices, robotics, consumer electronics, automotive systems, and medical equipment.
One of the most fundamental projects for learning embedded systems is LED blinking. Although simple, this project introduces several core concepts required for STM32 programming, including GPIO (General Purpose Input/Output) configuration, system clock initialization, Hardware Abstraction Layer (HAL) programming, and the execution of an embedded application within an infinite loop.

In this project, an external LED is connected to the PB3 GPIO pin of the STM32 Nucleo-L031K6 development board through a 220 Ω current-limiting resistor. The application is developed using the STM32 HAL library and simulated using the Wokwi online simulator, eliminating the need for physical hardware while providing an accurate representation of the circuit's behavior.
The LED is programmed to toggle its state every 500 milliseconds using the HAL_GPIO_TogglePin() and HAL_Delay() functions. This project serves as an excellent starting point for beginners who wish to understand STM32 HAL programming before progressing to more advanced topics such as timers, communication protocols, sensors, and real-time operating systems.
Project Overview
The STM32 LED Blinking project demonstrates how to control an external LED using the STM32 Nucleo-L031K6 microcontroller and the STM32 Hardware Abstraction Layer (HAL) library. It is one of the most fundamental embedded systems projects and serves as an introduction to GPIO programming on STM32 microcontrollers.
In this project, the PB3 GPIO pin is configured as a digital output. An external LED connected through a 220 Ω current-limiting resistor is periodically toggled using the HAL_GPIO_TogglePin() function, while the HAL_Delay() function generates a 500-millisecond delay between each state change. The complete circuit is designed and simulated using the Wokwi online simulator, allowing the project to be implemented without physical hardware.
By completing this project, learners gain practical experience in configuring GPIO pins, initializing the STM32 HAL library, understanding the role of the system clock, and developing a basic embedded application. These concepts form the foundation for more advanced STM32 projects involving sensors, communication interfaces, displays, motors, and real-time embedded systems.
Components Required
Component | Quantity | Description |
STM32 Nucleo-L031K6 Development Board | 1 | Acts as the main controller for executing the program and controlling the LED. |
LED (Light Emitting Diode) | 1 | Serves as the visual output device to indicate the GPIO state. |
220 Ω Resistor | 1 | Limits the current flowing through the LED to protect it from damage. |
Jumper Wires | As Required | Used to establish electrical connections between the development board and external components. |
Wokwi Online Simulator | 1 | Used to design, simulate, and test the project virtually without physical hardware. |
Software & Development Tools
The following software and development tools were used to design, program, and simulate the STM32 LED Blinking project.
Software/Tool | Purpose |
STM32CubeIDE | Used to write, compile, and debug the STM32 HAL program. |
STM32 HAL (Hardware Abstraction Layer) | Provides high-level APIs that simplify interaction with the STM32 microcontroller peripherals. |
Wokwi Online Simulator | Used to design the circuit, simulate the hardware, and verify the program without requiring a physical development board. |
Circuit Description
The circuit used in this project is simple and consists of an STM32 Nucleo-L031K6 development board, an external LED, a 220 Ω current-limiting resistor, and jumper wires. The complete circuit diagram is shown in Figure 1.
The PB3 pin of the STM32 Nucleo-L031K6 is configured as a GPIO output pin. One terminal of the LED is connected to the PB3 pin through a 220 Ω resistor, while the other terminal is connected to the GND (Ground) pin of the development board. This configuration allows the microcontroller to control the LED by changing the logic level of the GPIO pin.

The 220 Ω resistor is connected in series with the LED to limit the current flowing through it. Without this resistor, excessive current could damage both the LED and the GPIO pin of the microcontroller. The resistor ensures safe and reliable operation of the circuit.
When the PB3 pin is driven HIGH, current flows from the GPIO pin through the resistor and LED to ground, causing the LED to illuminate. When the pin is driven LOW, the current flow stops, and the LED turns OFF. By repeatedly changing the pin's output state, the LED blinks at regular intervals.

The pinout diagram illustrates the available GPIO pins of the STM32 Nucleo-L031K6 development board. In this project, GPIO pin PB3 is configured as the output pin to control the external LED.
The circuit is simulated using the Wokwi online simulator, which accurately represents the hardware connections and enables verification of the program without requiring physical components.
Working Principle
The STM32 LED Blinking project operates by continuously changing the state of the PB3 GPIO pin at fixed time intervals. The program follows a simple execution sequence that begins with system initialization and continues indefinitely inside the main program loop.
When the program starts, the HAL library and the system clock are initialized to prepare the STM32 microcontroller for operation. After the initialization process is completed, the GPIO pin connected to the external LED is configured as a digital output.
The main functionality of the program is executed inside an infinite while(1) loop. Within this loop, the HAL_GPIO_TogglePin() function changes the output state of the PB3 pin. If the LED is currently OFF, it is turned ON; if it is ON, it is turned OFF.
After each toggle operation, the HAL_Delay(500) function pauses the program for 500 milliseconds before the next toggle occurs. This repeated sequence produces the continuous blinking effect observed during the simulation.
Code Explanation
Macro Definitions

The following macro definitions are used to simplify the program and improve its readability.
LED_PORT specifies the GPIO port (**GPIOB**) to which the external LED is connected.
LED_PIN defines **PB3** as the GPIO pin used to control the LED.
LED_PORT_CLK_ENABLE enables the clock for GPIOB before the pin is configured.
Using these macros makes the code easier to understand and allows the GPIO configuration to be modified easily if a different pin is used in the future.
GPIO Initialization

The initGPIO() function configures PB3 as a digital output pin. It enables the GPIOB clock and initializes the pin in push-pull output mode, allowing the STM32 microcontroller to control the external LED.
Main Function

The main() function begins by initializing the HAL library and configuring the system clock. It then calls the initGPIO() function to prepare the LED pin for output.
After initialization, the program enters an infinite while(1) loop where:
HAL_GPIO_TogglePin() changes the state of the LED.
HAL_Delay(500) introduces a 500-millisecond delay before the LED state is toggled again.
This process repeats continuously, causing the LED to blink.
Source Code
The complete source code for this project is written in C using the STM32 HAL library. The program initializes the microcontroller, configures the system clock and GPIO, and continuously toggles the output pin connected to the external LED to produce the LED blinking effect.
Simulation Results
The STM32 LED Blinking project was successfully simulated using the Wokwi online simulator. After compiling and running the program, the external LED connected to PB3 blinked continuously with a 500-millisecond delay between each ON and OFF state.

The successful simulation verifies that the GPIO pin was correctly configured as a digital output and that the STM32 HAL-based program controlled the LED as intended.
Simulation Video
Applications
The STM32 LED Blinking project serves as a fundamental building block for many embedded system applications. Some common applications include:
GPIO testing and verification.
Learning STM32 HAL programming.
Status and power indication in embedded devices.
Debugging embedded applications using visual indicators.
Foundation for developing advanced projects involving sensors, communication modules, and actuators.
Advantages
Simple and beginner-friendly project for learning STM32 programming.
Demonstrates GPIO control using the STM32 HAL library.
Can be simulated without physical hardware using Wokwi.
Easily expandable for more advanced embedded applications.
Provides a strong foundation for future STM32 development.
Future Enhancements
This project can be extended by incorporating additional features such as:
Controlling multiple LEDs with different blinking patterns.
Using push buttons to control the LED.
Implementing timer interrupt-based LED blinking.
Controlling LED brightness using PWM.
Integrating sensors and communication peripherals for real-world embedded applications.
Conclusion
This project demonstrated the implementation of an LED blinking application using the STM32 Nucleo-L031K6 development board and the STM32 Hardware Abstraction Layer (HAL) library. By configuring the PB3 GPIO pin as a digital output and periodically toggling its state, the project illustrated the fundamental concepts of GPIO programming and HAL-based embedded development.
The successful simulation in the Wokwi online simulator confirmed the correct operation of the circuit and program. As one of the most fundamental STM32 projects, it provides an excellent foundation for exploring more advanced embedded system applications involving timers, communication interfaces, sensors, displays, and real-time operating systems.

Comments