Step2 Blink LED

Blinking LED with STM32CubeMX and HALClock.png 30min

Target description
This tutorial shows how to use the STM32CubeMX tool to initialize the peripherals, build and generate your starting projects with the initialization C code using HAL libraries.

After this tutorial, you should be able to:

  • create and configure the STM32CubeMX project and generate the initialization code
  • program and use HAL functions to blink a LED on the NUCLEO-L476RG board.


The hardware requirements to start the application are the following:

  • NUCLEO-L476RG[1] board (64-pin)
  • USB Type-A to Mini-B cable


  • STM32L476xx Datasheet
  • UM1724 User manual STM32 Nucleo-64 boards
  • UM1884 Description of STM32L4/L4+ HAL and low-layer drivers
  • UM1718 User manual STM32CubeMX for STM32 configuration and initialization C code generation

In this tutorial, we explain step-by-step how to blink a LED on the NUCLEO-L476RG board, using the STM32CubeMX tool, HAL, and STM32CubeIDE. Steps to follow:

1. Create New Project using STM32CubeMX

Clock.png 5min

  • Run STM32CubeMX tool.
  • Click New Project or Menu -> File -> New Project.
  • From Board Selector section, filter to select and use NUCLEO-L476RG board:
    Board selector
    • Check Nucleo64 type.
    • Check STM32L4 on MCU Series.
    • Select NUCLEO-L476RG board using Board selector:
      Select a board thanks to filters
    • Click Start Project to continue.
    • Answer Yes Initialize all peripherals with their default Mode ? popup.

2. Pinout Configuration

Clock.png 5min

Verify in Pinout tab, under SYS peripheral, that Serial Wire is selected as Debug interface:
Pin Out Tab
The corresponding pins PA13 and PA14 are assigned and configured automatically.
When a board is selected, STM32CubeMX allows automatically the pinout setting for the board with the pin assignments for the communication interfaces, LEDs, and other functions.
(To configure LED pins, check in the STM32 Nucleo-64 boards User Manual and STM32L476xx Datasheet which LED pins to use).

Info white.png Information
To see alternate pins for a signal, drag and drop the signal to a pin while keeping the Ctrl key pressed.

This example shows the use of the green LED pin LD2 present on the NUCLEO-L476RG board as GPIO_Output.
To verify that LD2 is set to GPIO_Output mode:

  • Type “LED” in the Find field and check that LD2 (green Led) is enabled to PA5 pin as GPIO_Output.
  • When found, the pin that matches the search criteria blinks on the Chip view.
  • Click on Chip view to stop the blinking. The signals can be set directly from the pinout view:
    Pin Out
Info white.png Information
Pinout search field allows the user to search for a pin name, signal name, or signal label in the Pinout view. When found, the pin or set of pins that matches the search criteria blinks on the Chip view. Click on the Chip view, to stop blinking.

3. Clock Configuration

Clock.png 4min
In the Clock Configuration tab, check that STM32CubeMX automatically configures the internal oscillator in the clock system with PLL @80MHz and proposes the PLL configuration as follows:
1. HSI selected in PLL Source Mux (HSI – High Speed Internal clock)
2. PLLCLK selected in the System Clock Mux
3. HCLK set to 80
Clock Tab

4. GPIO Configuration

Clock.png 4min
To configure the GPIOs, click the GPIO button in the Configuration Tab to open the GPIO Configuration window.
Pin Out

In the GPIO Tab, select Pin Name column PA5 to display the corresponding GPIO parameters and configuration to drive the NUCLEO-L476RG LED:

  • GPIO Output level: it is set to Low by default and can be changed to High.
  • GPIO mode automatically configures the pins with the relevant alternate function and GPIOs into Output Push Pull mode.
  • GPIO Pull-up/Pull-dow set to No pull-up and no pull-down by default can be configured when other choices are allowed.
  • GPIO Maximum output speed set to Low by default for the power consumption optimization can be changed to a higher frequency to fit the application requirements.
  • User Label is a name assigned to a GPIO. The GPIO can be found under this name via the Find menu.

Pin Out
Click Apply then OK to close the window.

5. Configure project and generate source code

Clock.png 4min
Switch to the Project Manager tab to configure the project. In the Project tab:

  • Fill the Project Name and Project Location fields
  • Set Toolchain/IDE to STM32CubeIDE.
    Project Tab

In the Code Generator tab, ensure that the following options are checked:

  • In STM32Cube Firmware Library Package section: Copy all used libraries into the project folder.
  • In Generated files section: Keep user code when regenerating the C code option, which only applies to the user sections within the STM32CubeMX generated files.
    Code generation

To generate the project in STM32CubeIDE:

  • Click on Generate Code.
    Generate Code
  • Click on Open Project to open the project with STM32CubeIDE.
    Open Project

6. Edit main.c to toggle the LED

Clock.png 4min
In STM32CubeIDE, from the Project Explorer tab, open the main.c file, in Src folder and add the adequate functions for the LED blinking, using HAL functions with the STM32CubeL4 firmware package.

To get an idea about the usage of HAL functions, refer to the UM1884 “Description of STM32L4/L4+ HAL and low-layer drivers” user manual, which provides the common and generic functions to use.
HAL help

The user code can be added in the main.c file, inside the while (1) loop between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ section (this will preserve your code after regeneration).

For the LED toggling , use these functions:

HAL_Delay (100);   /* Insert delay 100 ms */

Code to blink LED

7. Build the project

Clock.png 2min
To power the NUCLEO-L476RG, use its CN1 connector to connect in with a computer through a USB Type-A to Mini-B cable.
Click on the project from the project explorer, then right click and select Build Project, to compile the project (or click on Build button on the toolbar).

8. Debug the project

Clock.png 2min

Click on the Build toolbar icon Built Button.png, then on the Debug button Debug Button.png to build the project and start the debug session (or in the menu, select Project > Build Project then Run > Debug).
Click on Resume icon Run Button.png to continue the execution.
Now watch the green LED (LD2) toggling on the Nucleo-L476RG board.

Now you are able to:

  • create a new project using STM32CubeMX.
  • configure a project in STM32CubeMx and generate the initialization code.
  • update the project code in STM32CubeIDE using HAL functions.
  • execute a project in debug mode.
  • make a LED blink.

9. References

Previous step Arrow left.png Arrow right.png Next step