Skip to content
Shop

Blink an LED with STM32CubeIDE

Explore with your favorite AI

Gemini & other AI

Copy this page’s context into your AI to explore how it works and what you can build.

Open Gemini

Copy the prompt, then paste it into Gemini’s message box.

View prompt

Turn the LED on the STM32 MCU leaf on and off at one-second intervals.

This example targets AP03 STM32 MCU. Complete the STM32CubeIDE setup and prepare to program it with STM32CubeProgrammer. The project uses its included STM32 HAL and drivers; Arduino board packages and libraries are not required.

See How to run an example for obtaining complete source files and distinguishing build, upload, and execution checks.

Use the following leaves.

TypeNameQuantity
AZ62Connector Cover1
AZ01USB1
AP03STM32 MCU1
AV01CR20321
CR2032 coin cell battery1
M2 × 10 mm screw2

Assemble the leaves as shown below.

Leaf assembly for the Blink project

  1. Obtain and extract the entire STM32CubeIDE-Sample-Software repository using Code → Download ZIP. If you use Git, clone the same repository.
  2. Locate the extracted Blink folder. Use the complete project, including .project, .cproject, Core, and Drivers. main.c alone cannot be built.
  3. In STM32CubeIDE, choose File → Import... → General → Existing Projects into Workspace. Set Select root directory to the Blink folder, select the Blink project, and click Finish.
  4. Choose Project → Build Project. Check that it completes without errors and generates Debug/Blink.elf.

The download links point to the main branch, which may be updated. Use a complete set from the same source instead of mixing newly downloaded files into an older project.

The following four lines inside while (1) in Core/Src/main.c control the LED. Line numbers vary by source revision.

HAL_GPIO_WritePin(GPIOH, GPIO_PIN_3, GPIO_PIN_SET); // LED on
HAL_Delay(1000); // Wait 1000 ms
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_3, GPIO_PIN_RESET); // LED off
HAL_Delay(1000); // Wait 1000 ms
  1. Follow the STM32CubeIDE programming procedure to program the generated Debug/Blink.elf. Replace the Hello World filename in that procedure with the Blink file.
  2. After confirming successful programming, set the AP03 program switch to Run and press its reset switch.
  3. Check that the AP03 LED repeatedly stays on for one second and off for one second. A successful build alone does not confirm programming or execution.

If the LED does not blink, check the power supply, ELF file programmed, program switch position, and reset after programming, in that order. If connection fails, see the FAQ.