Blinking LED with STM32CubeMX and HAL 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.
Prerequisites
- Previous Tutorial: Step1: Tools Installation and First test..
Hardware
The hardware requirements to start the application are the following:
- NUCLEO-L476RG board (64-pin)
- USB Type-A to Mini-B cable
Literature
- 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
- Run STM32CubeMX tool.
- Click New Project or Menu -> File -> New Project.
- From Board Selector section, filter to select and use NUCLEO-L476RG board:
2. Pinout Configuration
Verify in Pinout tab, under SYS peripheral, that Serial Wire is selected as Debug interface:
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).
File:Info.png | 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:
File:Info.png | 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
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
4. GPIO Configuration
4min
To configure the GPIOs, click the GPIO button in the Configuration Tab to open the GPIO Configuration window.
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.
Click Apply then OK to close the window.
5. Configure project and generate source code
4min
Switch to the Project Manager tab to configure the project. In the 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.
To generate the project in STM32CubeIDE:
6. Edit main.c to toggle the LED
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.
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_GPIO_TogglePin (GPIOA, GPIO_PIN_5);
HAL_Delay (100); /* Insert delay 100 ms */
7. Build the project
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
2min
Click on the Debug toolbar icon to start the debug session (or in the menu, select Run > Debug).
Click on Resume icon 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.