stm32 blink led tutorial

STM32 Blink LED Tutorial

By Mark Davis
AD

Embarking on your microcontroller journey? The STM32 series is a powerful, versatile family that's perfect for your electronics projects. Today, we're going to kick things off with a simple yet rewarding tutorial: blinking an LED using an STM32 microcontroller. By the end of this guide, you'll have a blinking LED and a solid foundation for your upcoming projects.

Before we dive in, make sure you have the following materials: an STM32 development board (like the Nucleo-64 or Discovery kits), an LED, and some jumper wires. If you're using a specific board, be sure to check its documentation to ensure you're using the correct pins.

Setting Up the Hardware

First, let's connect our components. For this tutorial, we'll use PA5, one of the Board's most commonly used pins for LEDs. Take your LED's long leg (positive, or anode), and connect it to PA5. Then, connect the short leg (negative, or cathode) to a GND pin.

AD
STM32: First Blinking LED Project
STM32: First Blinking LED Project
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 (Blue Pill) using Arduino IDE: Blinking LED
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking
Getting Started with STM32 Nucleo in Arduino IDE - LED Blinking

Here's a simple wiring diagram:

STM32 LED Wiring Diagram
LED Anode STM32 PA5
LED Cathode STM32 GND

Using an STM32 Studio Workspace

Now, let's set up our software environment using STM32 Studio, a free IDE provided by STMicroelectronics. Download and install it, then create a new project with the same microcontroller as your board.

11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
11. Install STM32 CubeIDE and LED blink program for Windows (OpenOCD included)
External Led Blink Using STM32F401CE
External Led Blink Using STM32F401CE
Tutorial 1: LED Blinking STM32F4 Discovery Board - GPIO Pins with HAL Drivers
Tutorial 1: LED Blinking STM32F4 Discovery Board - GPIO Pins with HAL Drivers

For a Nucleo-64 board, choose "Nucleo-64 (RevB): F411RE" under the "Hardware Abstraction Layer" (HAL) category. Click 'OK' to generate your project.

Configuring the LED GPIO Pin

The STM32 HAL library provides functions to configure and control GPIO pins. Open your main.c file, and include the relevant header file:

AD
\#include "stm32f4xx_hal.h"

In the function `MX_GPIO_Init()`, setup PA5 as output:

Download and Install STM32CubeIDE - Getting Started Guide
Download and Install STM32CubeIDE - Getting Started Guide
First Project using STM32 in STM32CubeIDE - The Engineering Projects
First Project using STM32 in STM32CubeIDE - The Engineering Projects
STM32 Beginner Tutorial: Create Your First Project & Blink an LED (CubeMX + CubeIDE)
STM32 Beginner Tutorial: Create Your First Project & Blink an LED (CubeMX + CubeIDE)
STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 1 Blinking LED - Updated Oct 2017
STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 1 Blinking LED - Updated Oct 2017
ESP32 LED Blink Simulation Project for Students
ESP32 LED Blink Simulation Project for Students
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
STM32 Blue Pill GPIO Pins with STM32Cube IDE LED Blinking Tutorial
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
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);

Blinking the LED

Finally, let's make the LED blink! To do this, we'll set up a counter and blink the LED every second. First, add the following lines to your `main.c` file:

\#include "delay.h"
Universal2000DelayMS Delay;

Include the universial2000.h header file in your project. This provides delay functions that work across all STM32 architectures.

AD

Initializing the Delay Function

Next, initialize the delay function in the `MX_Misc_Init()` function:

50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
50. Install STM32CubeIDE and LED blink program with Nucleo for Windows
ESP32 LED Blink in Proteus8 Simulation - Complete Beginner Guide
ESP32 LED Blink in Proteus8 Simulation - Complete Beginner Guide
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
LED Blinking STM32F4 Discovery Board - Control GPIO with HAL Driver
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 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
STM32 Nucleo GPIO Pins with LED Blinking using STM32CubeIDE
Step-by-Step Guide to ESP32 LED Simulation in Proteus 8
Step-by-Step Guide to ESP32 LED Simulation in Proteus 8
How to Blink An LED Bulb Using ESP32 Board
How to Blink An LED Bulb Using ESP32 Board
Proteus Simulation for LED Blinking Project
Proteus Simulation for LED Blinking Project
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
Blink Led with Stm32f103c8 microcontroller keil and Stmcubemx
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
LED Blinking STM32 Nucleo with Arduino IDE - GPIO Pins
ESP32 Microcontroller LED Tutorial Using Proteus 8 Software
ESP32 Microcontroller LED Tutorial Using Proteus 8 Software
STM32 with Arduino IDE - iCircuit
STM32 with Arduino IDE - iCircuit
Blinkenlights To Bootloader: A Guide To STM32 Development
Blinkenlights To Bootloader: A Guide To STM32 Development
Arduino Nano ESP32 How to Blink an LED Using Visuino Visual Programming
Arduino Nano ESP32 How to Blink an LED Using Visuino Visual Programming
Delay.Init.Prescaler = 16;
Delay.Init.SubDivision = %%DELAY_SUB_MASK;
periph_clk_init(&Delay);

Now, let's make the LED blink!

Blinking the LED

Replace your original `while (1)` loop with the following:

AD
while (1)
{
  HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  Delay.MS(1000);
}

This code toggles the PA5 pin state - turning the LED on if it's off, and off if it's on - and delays for one second using the `Delay.MS(1000)` function. The result? A blinking LED!

Now that you've successfully blinked an LED using an STM32 microcontroller, you're ready to move on to more complex projects. Consider exploring timing functions, input devices, or even interfacing with a display. Happy coding!

AD