STM32

STM32 HAL GPIO Read Pin (GPIO Input Example)

In this tutorial, we’ll create an STM32 HAL GPIO Read Pin Example project. You’ll learn all the steps to configure the STM32 GPIO Input Pin Read Example in STM32CubeMX, flash the code from STM32CubeIDE to the blue pill STM32F103C8T6 board, and start testing with a push button & LED. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 HAL GPIO Read Pin Example (hal_gpio_readpin)
  2. STM32 HAL GPIO Read Pin Example (CubeMX Configurations)
  3. STM32 HAL GPIO Read Pin Example Code Explained
  4. STM32 GPIO Input Example Testing
  5. Wrap Up

STM32 HAL GPIO Read Pin Example (hal_gpio_readpin)

Objectives of This STM32 HAL GPIO Read Pin Example Project:

  • Configure GPIO Input & Output Pins Within CubeMX Tool
  • Use HAL_GPIO_ReadPin() To Read The Input Push Button State
  • Use HAL_GPIO_WritePin() To Change The Output Pin State

STM32 HAL GPIO Read Pin Example (CubeMX Configurations)

Step #1

Open STM32CubeMX, create a new project, and select the STM32F103C8T6 target microcontroller. Note that the STM32 BluePill board has two common target microcontrollers (STM32F103C8T6 & STM32F103C6T6). So you need to select the exact target microcontroller on your hardware board.

This example project should work flawlessly on any STM32 target microcontroller, you just need to select the target MCU that matches your hardware board.

Step #2

Go to the RCC clock configuration page and enable the HSE external crystal oscillator input.

Push Button Pin: Click on the PA9 GPIO pin and select it to be in GPIO_Input mode. Note: you can use any other pin you want instead.

LED Pin: Click on the PA8 GPIO pin in the “Pinout View” and select it to be in GPIO_Output mode. Note: you can use any other pin you want instead.

STM32 RCC External Clock Selection CubeMX

Step #3

Go to the clock configurations page, and select the HSE as a clock source, PLL output, and type in 72MHz for the desired output system frequency. Hit the “ Enter” key, and let the application solve for the required PLL dividers/multipliers to achieve the desired clock rate.

The reason behind this: using the external onboard oscillator on the BluePill board provides a more accurate and stable clock, and using a 72MHz as a system clock pushes the microcontroller to its limits, so we get the maximum performance out of it. As long as we don’t care about the application’s power consumption.

STM32-Blue-Pill-Proteus-Library-Simulation-Clock-Configuration

Step #4

Finally, go to the Project Manager, give your project a name, select the toolchain/IDE to be STM32CubeIDE, and click on the Generate Code button.

The STM32CubeMX tool will generate the initialization code & the project main files and it’ll prompt you to open the project in STM32CubeIDE. Select, open project, and let’s move to the next step.

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.

Step #5

Copy the code below into your main.c file replacing the auto-generated code from the beginning of the fill till the main function. You should leave everything else under the main() function in the main.c file as is.

STM32 HAL GPIO Read Pin Example Code (HAL_GPIO_ReadPin)

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);
	  }
  }
}

STM32 HAL GPIO Read Pin Example Code Explained

The CubeMX tool has 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 HAL_GPIO_ReadPin() function.

HAL GPIO Pin Read Function Description

In this STM32 GPIO Read Input Pin Example, we’ve read the pin state for the push button (A9) and used it to drive the LED pin (A8) High or Low depending on the push button input pin state. It’s a very simple application and you can modify it as you want to meet your application’s needs. Let’s now move to the testing of this example project on a real STM32 blue pill board.

STM32 GPIO Input Example Testing

Step #1

Refer To The Blue Pill Board Schematic & Pinout

Blue pill pinout

Step #2

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

STM32 GPIO HAL Led Blinking

Step #3

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

STM32 HAL GPIO LED Blinking
❕ Note

If the debugger GDB server returns an error 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.

Step #4

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.

Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazon & AmazonAliExpresseBay
1Resistors KitAmazon & AmazonAliExpresseBay
1Capacitors KitAmazon & AmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazon & AmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazon & AmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now