STM32F103 SPL Tutorial 3 – GPIO Read Button


In this tutorial, I will explain how to use STM32F103 GPIO for reading a push button. STM32F103 GPIO can be configured in 4 different modes (input mode, output mode, analog input mode, and alternate function mode). For reading a button, we need to configure a GPIO pin in digital input mode.

STM32F103 GPIO input

GPIO Input Mode

STM32F103 GPIO has 3 digital input mode: input with internal pull-up, input with internal pull-down, and input floating. The logic voltage of STM32 is 3.3V, so the logic voltage for GPIO input pins are also 3.3V, but there are several pins that 5V tolerant. We can use 5V logic level to this 5V tolerant input pins. The table below shows I/O voltage level on STM32F103 input pins. The characteristic of a GPIO pin when it is configured in input mode is shown in figure below.

5V GPIO pins

GPIO input

  • The output driver is disabled.
  • The schmitt trigger input is activated.
  • The weak pull-up and pull-down resistors are activated or not depending on the input configuration (pull-up, pull-down, or floating).
  • The data present on the GPIO pin is sampled into the input data register every APB2 clock cycle.
  • A read access to the input data register obtains the GPIO pin state.

Button Circuit

This is the button circuit for this tutorial. I also add an LED in this circuit just for showing the state of the button (is pressed or not). For the GPIO input mode, I use the input mode with internal pull-up, therefore the button circuit is active low (when the button is pressed, the logic in input data register is “0”). The GPIO output mode for the LED is open-drain mode and also active low.

Button circuit

Example Code

This is the example code. The purpose of this code is to turn on the LED when the button is pressed and turn off the LED when it is not pressed. For the project file you can get it from here.

You can use other GPIO input modes, which are input with internal pull-down or input floating. For input floating, you must add an external pull-up or pull-down resistor. This is the example circuit for input with internal pull-down and input floating with external pull-up. When you use a pull-down resistor, the logic is active high (when the button is pressed, the logic in input data register is “1”).

Button circuit open drain

Go to the part 4 of the tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *