STM32F103 SPL Tutorial 2 – GPIO Write LED

STM32F103 GPIO

STM32F103 GPIO are generic pins that can be configured as input or output. In this tutorial, I will explain how to use STM32F103 GPIO for controlling an LED on/off. STM32F103 GPIO can be configured in 4 different modes (input mode, output mode, analog input mode, and alternate function mode). For controlling an LED on/off, we need to configure a GPIO pin in output mode.

STM32F103 GPIO

GPIO Output Mode

There are 2 output modes for GPIO, output open drain and output push-pull. The logic voltage of STM32F103 GPIO is 3.3V, so the pin output voltage is 3.3V. This is the characteristic of a GPIO pin when it is configured in output mode:

GPIO output

  • The output driver is enabled.
  • In open drain mode, a “0” in the output data register activates the N-MOS while a “1” in the output data register leaves the port in Hi-Z. (the P-MOS is never activated in open drain mode).
  • In push-pull mode, a “0” in the output data register activates the N-MOS while a “1” in the output data register activates the P-MOS.
  • The schmitt trigger input is activated.
  • The weak pull-up and pull-down resistors are disabled.
  • 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 gets the GPIO state in open drain mode.
  • A read access to the output data register gets the last written value in push-pull mode.

LED Circuit

This is the LED circuit that I use in this tutorial. The LED configuration is active low. When the PA0 is low or “0”, the LED will turn on. When the PA0 is high or “1”, the LED will turn off. The output mode that I use is output push-pull mode so, when I write “0” to the output data register, the N-MOS will turn on, therefore the LED will turn on (current can flow from Vdd to Vss through LED and resistor). When I write “1” to the output data register, the P-MOS will turn on, therefore the LED will turn off (current can’t flow from Vdd to Vdd).

LED circuits

Example Code

To configure PA0 in output push-pull mode, we must do this steps:

  1. Enable the peripheral clock for GPIOA. GPIOA is connected to APB2 bus.
  2. Initialize GPIOA by using GPIO_InitTypeDef struct.

This the code to make an LED blinking, for the project file you can get it from here.

It is also possible to use output open drain mode to control the LED. The LED circuit is the same. When I write “0” to output data register, the N-MOS will turn on, therefore the LED will turn on (current can flow from Vdd to Vss through LED and resistor). When I write “1” to output data register, PA0 is in Hi-Z (current can’t flow because PA0 is Hi-Z/floating), therefore the LED will turn off.

Go to the part 3 of the tutorial.

Share this:

4 thoughts on “STM32F103 SPL Tutorial 2 – GPIO Write LED

  1. jesus hernandez says:

    Hi!, i have a question. I used this code with all pins of the board but the pins A15, B3, B4 not work, do you know if these pin need another configuration?

  2. Umar says:

    Hello have you used the led present on the board itself. I mean the led connected to Pin PC13. I have tried to toggle this led using standard function provided in St example. But led stays on and doesn’t toggle. Kindly help in this regard.

Leave a Reply

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