STM32 GPIO Pin Read LAB – Digital Input

Previous Tutorial Previous Tutorial Tutorial 5 Next Tutorial 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.


   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 🛒 🛒
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

   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

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! (The LED Pin)

STM32 GPIO Pin Select In CubeMX

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)

STM32 GPIO HAL Pin Read Push Button

Step5: Set The RCC External Clock Source

STM32 RCC External Clock Selection CubeMX

Step6: Go To The Clock Configuration

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

STM32 Clock Tree Configuration CubeMX

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

STM32 GPIO HAL Pin Read Push Button 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 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.

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.

HAL GPIO Pin Read Function Description

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.


   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

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.

Download LAB2 Project



Previous Tutorial Previous Tutorial Tutorial 5 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
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?

2 thoughts on “STM32 GPIO Pin Read LAB – Digital Input”

  1. 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

  2. Your teaching style is superb and your content is amazing.
    Thanks a lot for all of your effort.
    God Bless you

Comments are closed.