![]() |
Previous Tutorial | Tutorial 5 | Next Tutorial | ![]() |
|||
STM32 GPIO Pin Read (Digital Input) | |||||||
STM32 Course Home Page 🏠 |
In this LAB, we’ll configure a GPIO pin to be output. Another one to be an input. Then, we’ll write a simple application to switch an LED ON when a Switch is pressed. 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.
- 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 |
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:
- 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 Objectives
LAB Number | 2 |
LAB Title | LED & Push Button |
Configure GPIO Output Pin & Input Pin Within CubeMX Tool
Use HAL_GPIO_ReadPin To Read The Push Button State
Use HAL_GPIO_Write To Change The Pin State
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! (The LED Pin)
Step4: Click On The Pin You Want To Configure As An Input & Select Input Option
Let it be A9 pin for example! (The Push Button Pin)
Step5: Set The RCC External Clock Source
Step6: Go To The Clock Configuration
Step7: Set The System Clock To Be 72MHz Or Whatever You Want
Step8: 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 CubeMX tool has now generated the initialization C-Code for us. There is nothing special about it except the GPIO init function shown down below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); /*Configure GPIO pin : PA8 */ GPIO_InitStruct.Pin = GPIO_PIN_8; 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); /*Configure GPIO pin : PA9 */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; // <----- This Option HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } |
As you can see, the input pin (A9) is set to Hi-Z or High-Impedance mode, it’s called no-pull (no pull-up or down). You can, however, set the pin to pull-up or pull-down depending on your preference.
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_ReadPin() 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 this LAB, we’ll read the pin state for the push button (A9) and decide whether to drive the LED pin (A8) High or Low. It’s a very simple application as you can tell by looking at the code down below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); while (1) { // IF Button Is Pressed if(HAL_GPIO_ReadPin (GPIOA, GPIO_PIN_9)) { // Set The LED ON! HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); } else { // Else .. Turn 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
Note: if the debugger GDB server returns error back to you over and over. Then, don’t try starting the debug session again. Until you move the BOOT0 pin jumper from 0 to logic 1. Then try debugging again, it should work just as fine. But don’t forget to set the BOOT0 pin back to 0 so it starts your application on start-up after restarting the microcontroller.
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 5 | Next Tutorial | ![]() |
your tutorials are so nice. i have learned pic microcontrollers and now focusing on stm32.
it helps alot.
you should make youtube videos on these tutorials. that will be very helpful
thank you very much
Your teaching style is superb and your content is amazing.
Thanks a lot for all of your effort.
God Bless you