# Using GPIO on STM32

## 0. Pin Map

On the STM32F446RET6 Nucleo board, the LD2 LED is connected to PA5, and the USER Button is connected to PC13.

> Note: the LED is active high.

We will demonstrate how to set up a generic GPIO output pin with the LED, and a generic GPIO input pin with the user button.

## 1. Configure STM32

First, set the configuration from the [Starter Project](/notes/stm32/getting-started-stm32-edition/going-through-a-starter-project.md).

Click on **PA5**, set it to "GPIO\_Output".

![](/files/rL00RC7NOpd4paaovKAA)

Click on **PC13**, set it to "GPIO\_Input".

![](/files/O7XCNhzCmdkKahpdGfmF)

Save the .ioc file and generate code.

## 2. Code

First, add the code from the [Starter Project](/notes/stm32/getting-started-stm32-edition/going-through-a-starter-project.md).

In `main.c`, add the following code

```c
  /* USER CODE BEGIN 2 */
  char str[64];
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);
    HAL_Delay(250);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
    HAL_Delay(250);

    uint8_t val = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
    sprintf(str, "button value: %d\r\n", val);
    HAL_UART_Transmit(&huart2, (uint8_t *)str, strlen(str), 100);
  }
  /* USER CODE END 3 */
```

![](/files/85TNCRhPM5MoKDl3yzv5)

After saving, upload the code

## 3. Result

We can see LED LD2 blinking, and when we press/release the USER Button, the serial output changes.

![](/files/TK2LdxoLNACv0DaBzJeh)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://tk233.gitbook.io/notes/stm32/system-core/gpio.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
