STM32 GPIO Write Pin Digital Output LAB

Previous Tutorial Previous Tutorial Tutorial 4 Next Tutorial Next Tutorial
STM32 GPIO Pin Write (Digital Output)
STM32 Course Home Page 🏠

 

 

In this LAB, we’ll configure a GPIO pin to be output. Then, we’ll do the first LED blinking with the STM32 blue pill board. You’ll learn all the steps to configure the STM32 CubeMX and flash the code from CubeIDE to the board and start testing.

[toc]


   Required Components For LABs   

 

All the example code/LABs/projects in the course are going to be done using those boards below.

QTY Component Name 🛒 Amazon.com 🛒 eBay.com
2 BreadBoard Amazon eBay
1 LEDs Kit Amazon Amazon eBay
1 Resistors Kit Amazon Amazon eBay
1 Capacitors Kit Amazon Amazon eBay & eBay
2 Jumper Wires Pack Amazon Amazon eBay & eBay
1 9v Battery or DC Power Supply Amazon Amazon Amazon eBay
1 Micro USB Cable Amazon eBay
1 Push Buttons Amazon Amazon eBay

★ Check The Full Course Complete Kit List

Some Extremely Useful Test Equipment For Troubleshooting:

Affiliate Disclosure: When you click on links in this section and make a purchase, this can result in this site earning a commission. Affiliate programs and affiliations include, but are not limited to, the eBay Partner Network (EPN) and Amazon.com.


   LAB Objectives   

 

LAB Number 1
LAB Title LED Blinking

Configure GPIO Output Pin Within CubeMX Tool

Use HAL_GPIO_Write To Change The Pin State

And Use The HAL_Delay() & Know How It Works

 


   STM32 CubeMX Configurations   

 

Step1: Open CubeMX & Create New Project

STM32 CubeMX Create New Project

Step2: Choose The Target MCU & Double-Click Its Name

STM32 CubeMX Choose The Part Step

Step3: Click On The Pin You Want To Configure As An Output & Select Output Option

Let it be A8 pin for example!

STM32 GPIO Pin Select In CubeMX

Step4: Set The RCC External Clock Source

STM32 RCC External Clock Selection CubeMX

Step5: Go To The Clock Configuration

Step6: Set The System Clock To Be 72MHz Or Whatever You Want

STM32 Clock Tree Configuration CubeMX

Step7: Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using

Generate Initialization Code From STM32 CubeMX

 

Then, open the project in the IDE you’re using. And head over to the main.c file. So we can start writing the application code and have a look at the initialization code generated by the STM32 CubeMX tool.

 


   The Application Code In CubeIDE   

 

The main.c file in the source code directory within our projects is as shown below.

Both functions SystemClock_Config() and MX_GPIO_Init() are generated by CubeMX to configure the system clock as we’ve done in the GUI before and the GPIO pin which we’ve selected to be an output pin. The implementation of both functions is found in the file after the main function.

We call each of them before the main loop while(1) as well as the HAL_Init function. The HAL_Init must be called at the beginning of your application. Its functionality is clarified in the HAL Documentation as shown below.

HAL Init Function Documentation

And most importantly it initializes the SysTick timer, whose ticks are used by the HAL_Delay(). The SysTick timer is set to tick @ 1000Hz or every 1mSec. So the HAL_Delay function will give you multiples of milliseconds delay.

HAL Delay Documentation

Besides the delay function, we also need to know the HAL APIs for controlling the GPIO pins. To do basic stuff like pin read or write or port read/write, and so on.

So we’ll head over again to the HAL documentation and search for the GPIO chapter, where we’ll find this listing for the available APIs. The APIs are hyperlinked in the documentation file, so you can click the name of the function to go directly to its detailed description.

HAL GPIO APIs

So, let’s take a closer look at the GPIO_WritePin() function as we’ll be using it as well.

HAL GPIO Pin Write

After reading the documentation and getting familiar with the available APIs, you are ready to go. In our blinking LED example, we won’t need more than the GPIO_WritePin and HAL_Delay functions. And here is the full application code for this LAB.

 

 


   Prototyping & Testing   

 

Step0: Refer To The Blue Pill Board Schematic & Pinout

Blue pill pinout

Step1: Connect The ST-Link To The USB Port & SWD Pins On Board

STM32 GPIO HAL Led Blinking

Step2: Click The Debug Button To Compile The Code & Flash It To The Board & Start A Debugging Session

STM32 HAL GPIO LED Blinking

Step3: You Can Stop The Debugging Session Or Keep It Going. But You Need To Restart The MCU Once To Run The New Application At The Booting Process.

Download LAB Project

 

 

Previous Tutorial Previous Tutorial Tutorial 4 Next Tutorial Next Tutorial

 

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

5 thoughts on “STM32 GPIO Write Pin Digital Output LAB”

  1. I tried to blink the onboard LEDs of my STMF4Discoveryboard with the STM32F407V MCU. They light up, but they don’t blink.

    I set the pins in CubeMX as GPIO_OUTPUT pins and used the same code as you.

    while (1)
    {

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET);
    HAL_Delay(10);

    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
    HAL_Delay(10);
    }

    Do you have an idea why they don’t blink?

    • Oh, I noticed that I had 10ms in HAL_Delay function…

      How ever I changed it to 1000ms but it still doesn’t blink.

      • Are you sure about initializing the HAL driver?
        the HAL_Init() function does some initializations work of which is the SysTick timer that is being used by the HAL_Delay function
        The SysTick is by default gets configured for a period of 1ms and that’s how the HAL_Delay work.
        So make sure that it’s called before the while(1) loop and also the GPIO pin initialization in output mode has to be done before writing to the pins.

  2. I followed this tutorial using CubeMX, Cube IDE and the STM32F407G-DISC1 board. I could not get the off board LED to blink at all. Since it’s hard to see the circuit in your videos or pictures, I assume that I got the circuit wrong, so I switched to trying to blink the onboard LED’s by editing the .ioc file and setting ‘PD12’ as a GPIO_Output in the pinout configuration and regenerating the code.

    I tried both your code
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
    HAL_Delay(100);
    // LED OFF
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
    HAL_Delay(100);

    and this
    HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
    HAL_Delay(100);

    Everything compiles and uploads without errors but nothing happens.

    COM1 blinks red/green

    If you could help that would be great as I cannot find anyone else with the same issue after hours of googling.
    Thanks in advance

    • Hi Jules,
      It sounds like an issue with the core of the system. Becuase the onboard led should work ok if there is no error in code.
      My guess is maybe the clock configuration is not properly done in cubemx. I also think that forgetting to init_hal will cause an issue like this becuase it initializes the systick timer that makes all delay functions work properly.
      Make sure that the gpio pins are initialized. Their clock should be enabled and stuff like that. If you’re using cumbeMX generated code, then it should be handled by default.
      Hmmm… i’m trying to guess what could go wrong with an onboard led!
      It’s definitely something in the configurations either of the cpu clock or the gpio pin.

Comments are closed.