stm32 led blink example code

Stm32 Led Blink Example Code

By Joshua Miller
AD

When it comes to low-power microcontroller boards, the STM32 is a popular choice among hobbyists and professionals alike. One of the most basic and essential functions of an STM32 development board is controlling LEDs. Blinking an LED is often one of the first tasks new users strive to perform, serving as a practical introduction to programming and hardware interaction. This article walks you through a comprehensive guide to blinking an LED using the STM32, providing examples and detailed explanations.

Before diving into the code, ensure you have the necessary hardware: an STM32 development board (like the STM32F103C8T or the Blue Pill), an LED, and some jumper wires. Connect your LED to a digital output pin on the STM32 board, for instance, GPIO_13 (Pin PA.15).

Setting Up the Development Environment

To start programming the STM32, you first need to set up a development environment.ST-LINK/V2 debugger/programmer, or use a third-party tool like OpenOCD, J-Link, or the native ST-LINK interface on your STM32F4x9/STM32F4x5 series.

AD
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
External Led Blink Using STM32F401CE
External Led Blink Using STM32F401CE
STM32: First Blinking LED Project
STM32: First Blinking LED Project

Install the appropriate toolchain (GCC for ARM) and use an integrated development environment (IDE) like TrueSTUDIO, IAR Embedded Workbench, or Atollic TrueSTUDIO. For this guide, we'll assume you're using the free академической версии of TrueSTUDIO.

Creating a New Project

Launch TrueSTUDIO and create a new project. Select the appropriate target (e.g., STM32F103x8) and add main.c as the main source file. Ensure the project settings point to the correct toolchain and provide the necessary libraries for your STM32 board.

Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver

Double-check your toolchain settings, including the syscall delegate, memory layout, and optimization flags.

Including Necessary Headers

In your main.c file, include the necessary headers for handling GPIO functionality:

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

This header file ensures that you have access to the various STM32F103x8 registers and functions, enabling you to interact with the device hardware.

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...
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
ESP32 LED Blink Simulation Project for Students
ESP32 LED Blink Simulation Project for Students

Initializing the LED Pin

The first task is to configure the GPIO pin connected to your LED. GPIO_13 is located on the PORT A, specifically on bit 15 (PA.15).

To initialize this pin as a general-purpose output (GPIO), follow these steps:

AD

Configuring the RCC

Enable the clock for PORT A and the GPIO functionality:

3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
3. STM32. Програмування STM32F103. GPIO - Avislab - сайт для палких паяльників
LED Blinking Project Using 8051 Microcontroller - The Engineering Projects
LED Blinking Project Using 8051 Microcontroller - The Engineering Projects
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
How To Blink a LED Using Arduino
How To Blink a LED Using Arduino
ESP32 LED Blink in Proteus8 Simulation - Complete Beginner Guide
ESP32 LED Blink in Proteus8 Simulation - Complete Beginner Guide
Let’s code with STM32 NUCLEO
Let’s code with STM32 NUCLEO
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinkenlights To Bootloader: A Guide To STM32 Development
ESP32 and ESP8266 GPIO Programming with MicroPython - LED Blinking
ESP32 and ESP8266 GPIO Programming with MicroPython - LED Blinking
Blink Blonk
Blink Blonk
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
How to Blink An LED Bulb Using ESP32 Board
How to Blink An LED Bulb Using ESP32 Board
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
STM32F103C8T6 Blue Pill: high resolution pinout and specs - Mischianti
STM32F103C8T6 Blue Pill: high resolution pinout and specs - Mischianti
```c RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; ```

This action informs the microcontroller that you intend to use PORT A for GPIO operations.

Configuring the GPIO Port

Set the PA.15 pin as a general-purpose output:

AD
```c GPIOA->CRL &= ~(0xF << (15 * 4)); GPIOA->CRL |= (0x3 << (15 * 4)); // 0b0011 - Output mode, max speed 50 MHz ```

This configuration Places PA.15 in output mode with a high-speed setting (50 MHz).

Blinking the LED

Now that the LED pin is configured, you can create a simple blinking pattern.

To generate a delay, you can use the SysTick timer, an independent 24-bit down counter controlled by the dedicated SysTick clock source. The SysTick clock is a high-frequency clock shared between SysTick and the core debugger. For the STM32F103x8, this clock runs at approximately 8 MHz.

Initializing the SysTick Timer

Initialize the SysTick timer with a specific delay in milliseconds:

```c void init_systick(uint16_t ms) { SysTick->CTRL = 0; SysTick->LOAD = (SystemCoreClock / 1000) * ms - 1ul; SysTick->VAL = 0ul; SysTick->CTRL = SysTick_CTRL_CLKSOURCE | SysTick_CTRL_ENABLE; } ```

In this function, first disable the SysTick timer, then set the SysTick LOAD register to the desired delay in system clock cycles. Finally, reload the SysTick COUNT register and enable the SysTick timer.

Blinking the LED

Create a function to blink the LED using the SysTick delay:

```c void blink_led(uint16_t delay_ms) { while (1) { GPIOA->ODR ^= (1 << 15); // Toggle PA.15 (LED pin) init_systick(delay_ms); } } ```

Here, the ~ while(1) loop continuously toggles the LED state (on and off) with a specified delay in milliseconds.

Implementing the Main Function

Finally, in the main function, call the blink_led function with an appropriate delay:

```c int main(void) { init_systick(100); // Delay for 100 ms blink_led(500); // Blink the LED every 500 ms while (1) { /* Avoid compiler warnings */ } } ```

Ensure you include a simple infinite while loop at the end of the main function to avoid compiler warnings about predicted return values.

The STM32 LED blink example is a simple yet powerful project that introduces you to fundamental concepts in STM32 programming. Once you grasp the basics, you can build more complex projects, such as PWM-controlled LED dimming, LED animations, or interfacing with external sensors and displays. Happy coding!

AD