|Previous Tutorial||Tutorial 4||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.
Required Components For LABs
All the example code/LABs/projects in the course are going to be done using those boards below.
- Nucleo32-L432KC (ARM Cortex-M4 @ 80MHz) or (eBay)
- Blue Pill STM32-F103 (ARM Cortex-M3 @ 72MHz) or (eBay)
- ST-Link v2 Debugger or (eBay)
|QTY||Component Name||🛒 Amazon.com||🛒 eBay.com|
|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:
- My Digital Storage Oscilloscope (DSO): Siglent SDS1104 (on Amazon.com) (on eBay)
- FeelTech DDS Function Generator: KKMoon FY6900 (on Amazon.com) (on eBay)
- Logic Analyzer (on Amazon.com) (on eBay)
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 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
Step2: Choose The Target MCU & Double-Click Its Name
Step3: Click On The Pin You Want To Configure As An Output & Select Output Option
Let it be A8 pin for example!
Step4: Set The RCC External Clock Source
Step5: Go To The Clock Configuration
Step6: Set The System Clock To Be 72MHz Or Whatever You Want
Step7: Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using
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.
static void MX_GPIO_Init(void);
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.
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.
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.
So, let’s take a closer look at the GPIO_WritePin() function as we’ll be using it as well.
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.
// LED ON
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
// LED OFF
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
Prototyping & Testing
Step0: Refer To The Blue Pill Board Schematic & Pinout
Step1: Connect The ST-Link To The USB Port & SWD Pins On Board
Step2: Click The Debug Button To Compile The Code & Flash It To The Board & Start A Debugging Session
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.
|Previous Tutorial||Tutorial 4||Next Tutorial|
5 thoughts on “STM32 GPIO Write Pin Digital Output LAB”
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.
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
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.
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);
// LED OFF
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
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
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.