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

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.

HAL GPIO APIs

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.

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

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!

Khaled Magdy

I'm an embedded systems engineer doing both Software & Hardware. I'm an EE guy who studied Computer Engineering, But I'm also passionate about Computer Science. I love reading, writing, creating projects and Technical training. A reader by day a writer by night, it's my lifestyle. You can view my profile or follow me via contacts.

You may also like...

2 Responses

  1. Harshit Choudhary says:

    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. Mirza Ahsan Baig says:

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

Leave a Reply

%d bloggers like this: