STM32 UNO Lab 01 GPIO LEDs » Historie » Revize 4
Revize 3 (Ján Hýbl, 2026-10-30 15:13) → Revize 4/7 (Ján Hýbl, 2026-10-30 15:13)
h1. STM32-UNO - Lab 01: GPIO Configuration and LED Control
*Course:* F7ABBMTB
*Platform:* STM32-UNO
*Microcontroller:* STM32F446RET6 (STM32F4, Arm Cortex-M4)
*Tools:* STM32CubeMX 6.x, STM32CubeIDE 2.x, STM32 HAL, STLINK-V3MODS
*Language:* C
*Exercise type:* Introduction to embedded programming
*Revision:* 1.0 (30 October 2026)
[[STM32_UNO|Back to STM32-UNO documentation]]
{{toc}}
h2. 1. Learning objectives
This laboratory exercise introduces the STM32 development workflow. Students will configure two general-purpose digital outputs (GPIO), generate initialization code, write their first C program, compile it, and program the STM32-UNO board through the SWD interface using STLINK-V3MODS.
By the end of this exercise, students should be able to:
# Create a microcontroller project in STM32CubeMX.
# Configure GPIO pins as push-pull digital outputs.
# Generate source code using the STM32 HAL library.
# Edit and compile an application in STM32CubeIDE.
# Connect and use an STLINK-V3MODS programmer/debugger.
# Verify the operation of two digital outputs using the board's LED indicators.
h2. 2. Required equipment
* STM32-UNO development board with an STM32F446RET6 microcontroller.
* STLINK-V3MODS debugger/programmer and a suitable breakout/adapter for its 32 castellated contacts.
* SWD wiring or a verified programming adapter for STM32-UNO.
* USB cable for the programmer and the appropriate power cable for the STM32-UNO board.
* Windows or Linux computer with STM32CubeMX and STM32CubeIDE installed.
* Optional: multimeter to check logic levels and wiring.
*Important:* The STM32-UNO GPIO signals use 3.3 V logic. Do not connect an external 5 V output directly to a GPIO pin.
h2. 3. Software installation
Install the following software:
|_. Software |_. Purpose |_. Official download |
| STM32CubeMX (classic 6.x) | Microcontroller pinout, peripheral and clock configuration, generation of STM32CubeIDE projects | "STM32CubeMX":https://www.st.com/en/development-tools/stm32cubemx.html |
| STM32CubeIDE (Eclipse-based 2.x) | C editing, build, flashing and debugging | "STM32CubeIDE":https://www.st.com/en/development-tools/stm32cubeide.html |
| STM32CubeProgrammer (optional) | Independent flash programming and ST-LINK diagnostics | "STM32CubeProgrammer":https://www.st.com/en/development-tools/stm32cubeprog.html |
Since STM32CubeIDE 2.0, MCU configuration is carried out in the standalone STM32CubeMX application, not inside STM32CubeIDE. This guide uses the *classic STM32CubeMX 6.x* workflow with @Toolchain / IDE = STM32CubeIDE@. The separate *STM32CubeMX2* application uses a different project-generation/import workflow and is not covered by this exercise.
h2. 4. Select the two outputs
Use the two digital positions *D13* and *D12* on the Arduino-compatible STM32-UNO header. These are the intended LED control points for this introductory exercise.
|_. Board signal |_. MCU GPIO used in this draft |_. CubeMX user label |_. Mode |
| D13 / LED 1 | PC10 PC | LED_D13 | GPIO_Output |
| D12 / LED 2 | PC12 PC | LED_D12 | GPIO_Output |
*Instructor validation required before practical use:* D13 -> PA5 and D12 -> PA6 are the pin assignments of the reference NUCLEO-F446RE board. This mapping has *not yet been confirmed* against the STM32-UNO electrical schematic. Before the exercise, the instructor must check the actual STM32-UNO DIO-to-MCU connections and LED circuit. If they differ, replace the GPIO selections in this table and in the STM32CubeMX configuration. The user labels may remain @LED_D13@ and @LED_D12@, provided they match the physical board connectors.
The LED colors and their active logic levels also depend on the actual STM32-UNO circuit. A GPIO output set to HIGH is not necessarily the same as an illuminated LED.
h2. 5. Create the project in STM32CubeMX
h3. Step 1 - Launch STM32CubeMX
# Start @STM32CubeMX@.
# Select @File -> New Project@.
# Open the @MCU/MPU Selector@ tab.
h3. Step 2 - Select the MCU
# Enter @STM32F446RET6@ into the search field.
# Select the exact microcontroller model from the search results.
# Confirm the selection to open the device pinout configuration.
The target MCU is an STM32F446RET6 featuring an Arm Cortex-M4 processor, a maximum CPU clock of 180 MHz, 512 KB of Flash memory, and 128 KB of main SRAM.
h3. Step 3 - Enable SWD debugging
# Open @System Core -> SYS@.
# Set @Debug@ to @Serial Wire@.
# Keep @PA13 (SWDIO)@ and @PA14 (SWCLK)@ reserved for the debugger.
Do not reassign SWD pins to GPIO in this laboratory exercise.
h3. Step 4 - Configure the system clock
For the first exercise, keep a simple default clock configuration based on the MCU's internal HSI oscillator (16 MHz). There is no need to configure a high-speed external oscillator or the PLL for this task.
Do not enable HSE unless the actual board hardware and clock configuration have been verified. The STM32-UNO BOM indicates a 32.768 kHz low-speed crystal; that is not the same as an HSE crystal.
h2. 6. Configure GPIO outputs
# In @Pinout & Configuration@, select the first verified MCU GPIO for D13 and set it to @GPIO_Output@ (PA5 in the draft mapping).
# Select the second verified MCU GPIO for D12 and set it to @GPIO_Output@ (PA6 in the draft mapping).
# Open @System Core -> GPIO@.
# Configure the parameters shown below.
|_. Parameter |_. D13 output |_. D12 output |
| GPIO mode | Output Push Pull | Output Push Pull |
| Pull-up / Pull-down | No pull-up and no pull-down | No pull-up and no pull-down |
| Maximum output speed | Low | Low |
| GPIO output level | Low | Low |
| User label | LED_D13 | LED_D12 |
@Output Push Pull@ actively drives either LOW (approximately 0 V) or HIGH (approximately 3.3 V). The @Low@ speed setting is sufficient for LEDs and avoids unnecessarily fast transitions.
After code generation, the user labels result in symbols similar to the following, normally defined in @Core/Inc/main.h@:
<pre><code class="c">
LED_D13_Pin
LED_D13_GPIO_Port
LED_D12_Pin
LED_D12_GPIO_Port
</code></pre>
h2. 7. Generate the STM32CubeIDE project
# Open the @Project Manager@ tab in STM32CubeMX.
# Set @Project Name@ to @STM32_UNO_LED@.
# Choose a suitable project directory.
# Set @Toolchain / IDE@ to @STM32CubeIDE@.
# Use the @STM32Cube FW_F4@ firmware package and HAL drivers.
# Click @GENERATE CODE@.
# If prompted to download the STM32CubeF4 firmware package, approve the download.
h3. Import the generated project into STM32CubeIDE
# Start STM32CubeIDE.
# Select @File -> Import@.
# Choose @Import STM32 Project -> STM32CubeMX/STM32CubeIDE Project@ (the exact wording may vary slightly between versions).
# Select the directory containing the project generated by STM32CubeMX.
# Complete the import and wait for project indexing.
# Check that the project contains @Core/Src/main.c@, @Core/Inc/main.h@, and the STM32 HAL driver files.
If the @Build Project@ action is unavailable, recheck that the project was generated for @STM32CubeIDE@ and imported using the STM32-specific importer, rather than as an arbitrary directory.
h2. 8. Write the first program
Open @Core/Src/main.c@ and find the @while (1)@ loop in the @main()@ function. Paste only the *statements* below into the section bounded by @/* USER CODE BEGIN 3 */@ and @/* USER CODE END 3 */@ within the existing loop. Do not copy or duplicate the @while (1)@ loop itself.
h3. Task A - Set both outputs HIGH
<pre><code class="c">
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
LED_D13_Pin,
GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
LED_D12_Pin,
GPIO_PIN_SET);
</code></pre>
After the program starts, both selected outputs remain at logical HIGH. Depending on how the onboard LEDs are connected, HIGH may turn on one LED color, turn off another, or produce a different visible indication.
h3. Task B - Alternate the two outputs
Replace the statements from Task A with the following code inside the same user-code section:
<pre><code class="c">
/* State 1: D13 = HIGH, D12 = LOW */
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
LED_D13_Pin,
GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
LED_D12_Pin,
GPIO_PIN_RESET);
HAL_Delay(500);
/* State 2: D13 = LOW, D12 = HIGH */
HAL_GPIO_WritePin(LED_D13_GPIO_Port,
LED_D13_Pin,
GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_D12_GPIO_Port,
LED_D12_Pin,
GPIO_PIN_SET);
HAL_Delay(500);
</code></pre>
The program sets opposite states on the two outputs, then reverses them every 500 ms. One complete cycle takes 1000 ms (1 second).
*Why use USER CODE sections?* STM32CubeMX regenerates initialization code when the hardware configuration changes. Custom application code stored outside the designated @USER CODE BEGIN@ / @USER CODE END@ sections may be overwritten.
h2. 9. Connect the STLINK-V3MODS programmer
The STLINK-V3MODS communicates with the STM32 microcontroller over SWD. Unlike a typical ST-LINK dongle with a cable header, STLINK-V3MODS has a 32-contact castellated edge connection intended for an adapter or host PCB. Use a properly designed breakout; do not assume it has the STDC14 pinout of STLINK-V3MINI.
h3. SWD signal mapping
|_. STLINK-V3MODS contact |_. Signal |_. STM32F446RET6 target |
| 4 | T_SWDIO | PA13 (SWDIO) |
| 13 | T_SWCLK | PA14 (SWCLK) |
| 8 | GND | GND |
| 30 | T_VCC (input) | 3.3 V target voltage reference |
| 31 | T_NRST | NRST (recommended) |
| 5 | GNDDetect | Optional connection detection; consult adapter design |
*Power and wiring precautions:*
* Power the STM32-UNO via its approved USB power input or another verified board power source.
* Connect the target GND and programmer GND together.
* STLINK-V3MODS contact 30 (@T_VCC@) is a *voltage-sense input*, not a 3.3 V output.
* Contact 22 of STLINK-V3MODS is a separate 5 V output. *Do not connect it to the board's 3.3 V rail* in this exercise.
* For @Connect Under Reset@ to work, connect the NRST signal.
* Confirm all connections against the STM32-UNO electrical schematic and the STLINK-V3MODS user manual (UM2502) *before powering the assembly*.
*Instructor validation required:* The physical STM32-UNO SWD connection point and its contact numbering have not been established from the available PCB renders, Gerber files, and BOM. The instructor must document the board-side SWD connector or test pads and provide a verified adapter before students perform this step.
h2. 10. Build and flash the firmware
# Connect the powered STM32-UNO to the STLINK-V3MODS through the verified SWD adapter.
# Connect STLINK-V3MODS to the computer using USB.
# In STM32CubeIDE, save @main.c@ (@Ctrl+S@).
# Choose @Project -> Build Project@ or click the hammer icon.
# Check the console: the build should complete with *0 errors*.
# Open @Run -> Debug Configurations@.
# Select or create the STM32 debug configuration for @STM32_UNO_LED@.
# Select the @ST-LINK@ debugger and the @SWD@ interface.
# Use a conservative SWD clock (for example, around 1 MHz) if the adapter or connection is new.
# Click @Debug@. The IDE should program the target Flash memory and enter the debug session.
# If the CPU is paused at @main()@, click @Resume@ (typically @F8@) to execute the firmware.
Subsequent firmware changes can be built and downloaded through the same debug configuration.
h2. 11. Verify the result
For Task B, verify the following sequence:
|_. Time in the cycle |_. D13 |_. D12 |
| 0-500 ms | HIGH (3.3 V) | LOW (0 V) |
| 500-1000 ms | LOW (0 V) | HIGH (3.3 V) |
| Repeats every 1000 ms | HIGH / LOW alternating | LOW / HIGH alternating |
Observe the corresponding onboard LEDs. Their *colors and on/off polarity are board-dependent*; the electrical output states in the table are the primary acceptance criterion. If necessary, verify the output voltage using a multimeter connected between a DIO pin and GND.
h2. 12. Troubleshooting
|_. Problem |_. Suggested checks |
| @No ST-LINK detected@ | USB data cable, operating-system drivers, STLINK-V3MODS firmware and USB connection. |
| @No target detected@ | Board power, common GND, SWDIO, SWCLK, T_VCC voltage reference, verified adapter wiring. |
| SWD connection fails | Reduce SWD frequency; verify SYS/Serial Wire configuration; try Connect Under Reset if NRST is wired. |
| Flash programming succeeds, but the program does not run | Resume execution from a breakpoint; confirm the firmware was built for STM32F446RET6. |
| Output pins do not change | Verify MCU-to-DIO mapping and that GPIO initialization is generated. |
| LEDs behave opposite to expectations | Inspect the LED driver circuit; the indicator may be active-low or may contain two LED colors. |
| Application enters @Error_Handler()@ | Verify the clock configuration; avoid enabling an HSE oscillator that is not fitted. |
h2. 13. Independent assignments
After completing Task B:
# Change the delay from 500 ms to 100 ms and verify the new period.
# Modify the program so that both outputs are always in the same logic state and switch together.
# Create two independent blinking periods: D13 changes state every 200 ms and D12 every 1000 ms. Solve this extension without relying on a single blocking @HAL_Delay()@ sequence; consider @HAL_GetTick()@.
h2. 14. Completion checklist
* [ ] The STM32CubeMX project targets the correct MCU.
* [ ] Two verified STM32-UNO DIO signals are configured as GPIO outputs.
* [ ] STM32CubeIDE builds the project with no errors.
* [ ] STLINK-V3MODS successfully programs the STM32-UNO over SWD.
* [ ] Both outputs switch with the expected timing.
* [ ] The student can explain @GPIO_PIN_SET@, @GPIO_PIN_RESET@ and @HAL_Delay()@.
h2. 15. References
* "STM32CubeMX - STMicroelectronics":https://www.st.com/en/development-tools/stm32cubemx.html
* "STM32CubeIDE - STMicroelectronics":https://www.st.com/en/development-tools/stm32cubeide.html
* "STM32CubeProgrammer - STMicroelectronics":https://www.st.com/en/development-tools/stm32cubeprog.html
* "STM32F446xC/E datasheet - STMicroelectronics":https://www.st.com/resource/en/datasheet/stm32f446re.pdf
* "UM2502 - STLINK-V3MODS / STLINK-V3MINI user manual":https://www.st.com/resource/en/user_manual/dm00555046-stlink-v3mods-and-stlink-v3mini-mini-debuggers-programmers-for-stm32-stmicroelectronics.pdf
* "STM32CubeIDE 2.0 - separate STM32CubeMX workflow":https://community.st.com/stm32cubeide-mcus-28/stm32cubeide-2-0-0-released-159542
h2. Instructor notes (not part of the assessed student task)
Before releasing this lesson as an executable wiring exercise, verify and update: (1) the actual D13/D12 to MCU GPIO mapping; (2) the onboard LED electrical polarity and any required LED jumpers; (3) the physical STM32-UNO SWD pad/header location and cable pin numbering. Keep this note visible until all three items have been validated on real hardware.
PINOUT configuration:
DIO:
PC10 connector num13
PC12 connector num12
PD2
PB3
PC13
PA1
PB2
PB1
PB4
PB0
PB5