The post explains how to use stm32 pins as input using keil and stmcubemx ide. A simple on/off tutorial with button as input is made to explain the coding and operation. When a button is pressed the LED lights up and when the button is released the LED turns off. The project is quite simple. Before starting the tutorial, I recommend doing the tutorial on how to get started with the stm32f103 microcontroller, keil ide and stumcubemx code initialization. This will introduce how to initialize/configure and align the stmcubemx and keil ide to start working with stm32f103 microcontrollers. You won't understand this project if you don't have an introduction to stmcubemx and keil ide.
- Getting started with stm32f103, keil and stmcubemx
The project circuit is quite simple. I connected the led to pin#pc-13 of the stm32f103c8t6 and the button to pin#pa-10.
|
Statement of Stm32f103 led and button pins in stmcubemx
|
stm32f103 gpios as input/output and pull-up/pull-down resistor configuration
The Pc-13 LED pin is declared as output and the Pa-10 button as input. Pa-10 pin as an internal pull up resistor. I activated and connected my button directly to ground. In the top image you can see that GPIO PA10 is in input mode and PC-13 is in output Push Pull mode. The initial output level on the PC-13 LED pin is declared high. Output frequency for Led pin set to average. If you don't understand anything at this level, follow the tutorial mentioned above. Explains all of this in depth.
|
|
The Stm32f103 microcontroller supports internal and external clock sources. Internally it has 2 RC oscillators HSI (high internal speed) and LSI (low internal speed). Internal high speed is clocked at 8Mhz and internal low speed is clocked at 40khz. Externally stm32f103 supports HSE (external high speed) and LSE (external low speed). External high speed supports maximum input clock of 16 MHz and external low speed supports maximum input clock of 1000 kHz. Stm32f103 has an internal PLL circuit from which we can generate 72Mhz clock signal. We can go above 72 MHz, but since the stm32 works with a maximum clock of 72 MHz, we went above 72 MHz. In this tutorial I am using a high-speed 8Mhz internal clock source. The settings for HSI (High Speed Internal) are shown below.
|
stm32f103 led with button circuit diagram
|

After generating the stmcubemx initialization code it is time to write our desired logic “Led on/off with push button”. It is a simple code if we write it in c or c++, but in keil it all depends on the syntax supported by the compiler and the drivers/libraries we are using. I'm working with Hal drivers released by stmicroelectronics. These Hal drivers are directly installed on our PC when we download the stmcubemx software package. When we generate code from stmcubemx to keil, the necessary hal drivers and libraries are copied directly to the project folder.
The while(1) loop in the main function is where we write our code. I write the below code for our logic and it is working perfectly and as per the desired output.
if(HAL_GPIO_ReadPin(Ibutton_GPIO_Port, Ibutton_Pin)==GPIO_PIN_RESET) //Checks if the button was pressed
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_SET); //If pressed LED turns on
other
HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin, GPIO_PIN_RESET); //Otherwise LED off
The HAL_GPIO_ReadPin function reads the pin status and returns the status. It takes 2 parameters to work, one on the pin port and the second on the pin number. In our case, the port is Ibutton_GPIO_Port and the pin number is Ibutton_Pin.
HAL_GPIO_WritePin writes to the specified pin. Make it high or low. It needs 3 parameters of port, pin and data. In our case, the port is Led_GPIO_Port, the pin is Led_Pin and the data is SET/RESET.
- GPIO_PIN_RESET – Write 0 to pin, makes pin low
- GPIO_PIN_SET – Write 1 to the pin, makes the pin high


