stm32 blink led example

STM32 Blink LED Tutorial: Simple Example

By Daniel Garcia
AD

Hello, aspiring developers! Let's dive into one of the most fundamental examples in the world of embedded systems: blinking an LED with an STM32 microcontroller. This essential tutorial will guide you through setting up your development environment, and we'll explore the core concepts of embedded programming along the way. So, let's get started with this illuminating journey!

First, ensure you have the required hardware: an STM32 development board (such as the STM32F103C8T6, commonly known as Blue Pill), an LED, and some breadboard Jumper wires. For software, you'll need the STM32CubeIDE, an Integrated Development Environment (IDE) provided by STMicroelectronics. Download and install it, accepting the default settings.

Setting Up Your STM32 Development Environment

To start our LED blinking adventure, you must set up your development environment correctly. Launch STM32CubeIDE, and open the Example Explorer by going to Window > Perspective > Example Explorer. You'll find numerous examples here, but we're interested in the bottom-right corner: Hello World example.

AD
STM32 : Basics of HAL with LED Blink example using STM32CUBE IDE
STM32 : Basics of HAL with LED Blink example using STM32CUBE IDE
External Led Blink Using STM32F401CE
External Led Blink Using STM32F401CE
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking

Drag and drop this example into your workspace. You'll see a new project named HelloWorld created. Expand this project, and you'll notice folders for Inc, Inc_Lib, Src, and others. These are the core libraries and source files for your project.

Understanding Project Structure

The Src folder contains the main.c file, which is the entry point of your STM32 program. Other files, like stm32f1xx_hal_msp.c and stm32f1xx_hal_msp.h, are part of the STM32 HAL (Hardware Abstraction Layer) library, which simplifies hardware interactions.

STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
Stm32f103 microcontroller Led on/off control with Push Button as Input
Stm32f103 microcontroller Led on/off control with Push Button as Input
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED

The Inc folder is where you'll include necessary header files, while the považ_Src folder contains your application source files. For now, focus on the main.c file for our LED blinking tutorial.

Configuring Your STM32 Board

Before we blink our LED, let's tell the IDE which board we're using. In the HelloWorld project, go to Project > Properties > Toolchain Settings > STM32 System > Select MCU. Choose your board from the dropdown menu, then click Apply and OK.

AD

Now, let's proceed to the heart of our LED blinking project!

LED Blinking With STM32F103R6 Using SysTick
LED Blinking With STM32F103R6 Using SysTick
Download and Install STM32CubeIDE - Getting Started Guide
Download and Install STM32CubeIDE - Getting Started Guide
STM32: First Blinking LED Project
STM32: First Blinking LED Project
STM32F103C8T6 Blue Pill SysTick LED Blinking
STM32F103C8T6 Blue Pill SysTick LED Blinking
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial

Blinking an LED with STM32

A fundamental concept in embedded programming is working with General-Purpose Input/Output (GPIO) pins. LEDs are connected to these pins, so let's learn how to control them. In this example, we'll blink an LED connected to the PB6 pin of your STM32 board.

First, include the necessary header file in the main.c file:

AD
```c #include "stm32f1xx_hal_gpio.h" ```

Initializing GPIO for an LED

In the main function, add the following code to initialize GPIOA and configure PB6 as output:

LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
ESP-IDF: ESP32 Blink LED Example (VS Code) | Random Nerd Tutorials
ESP-IDF: ESP32 Blink LED Example (VS Code) | Random Nerd Tutorials
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinkenlights To Bootloader: A Guide To STM32 Development
How to Blink An LED Bulb Using ESP32 Board
How to Blink An LED Bulb Using ESP32 Board
Blink Blonk
Blink Blonk
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
STM32F103 PCB STM32 MCU family developer board schematic STM32F103 PCB STM32 MCU family deve...
Blinking a LED in Multiple Sequences Using STM 32
Blinking a LED in Multiple Sequences Using STM 32
Getting Started with STM32 Blue Pill Using Arduino IDE
Getting Started with STM32 Blue Pill Using Arduino IDE
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
Getting Started With Stm32f103c8 With Mbed+STLink V2
Getting Started With Stm32f103c8 With Mbed+STLink V2
```c GPIO_InitTypeDef GPIO_InitStruct = {0}; // Enable GPIOA clock GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); ```

Here, we enable the clock for GPIOA, then configure PB6 as an output with no pull-up or pull-down resistor and a low-speed Push-Pull (PP) mode.

Blinking the LED

The core of our LED blinker lies in the infinite loop of the main function. Add the following code inside this loop:

AD
```c while (1) { HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_6); HAL_Delay(500); } ```

This simple yet powerful line toggles the PB6 pin between 0 (LED off) and 1 (LED on) every 500 milliseconds, creating the blinking effect.

Now, let's compile and run our project. Click on the Run or press Ctrl + F11. After flashing the code to your STM32 board, you should see the LED connected to PB6 blinking as intended. Congratulations! You've just blinked an LED with an STM32 microcontroller.

Remember, this is just the beginning of your journey in the exciting world of embedded systems. As you dive deeper, you'll explore more complex projects, but you'll find that the fundamental concepts you've learned here – such as configuring GPIO and using loops – form the backbone of embedded programming. Happy coding!

AD