stm32 projects

Utilizing GPIO Pins for controlling external LEDs with STM32F103C8T6 using STM32CubeIDE

Introduction:

In the realm of embedded systems development, the GPIO (General-Purpose Input/Output) pins of STM32 microcontrollers have earned their reputation for their power, versatility, and ease of use. In our previous article, we delved into the fundamentals of STM32CubeIDE, guiding readers through the steps of downloading, installing, and performing onboard LED testing – a crucial first milestone in any STM32 project. Building upon that foundation, we now set our sights on the next exciting article: utilizing GPIO pins for controlling external LEDs with STM32F103C8T6 using STM32CubeIDE.

In this article, we will explore how to harness the GPIO pins of the blue pill STM32F103C8T6 microcontroller to control external LEDs. These GPIO pins are highly flexible and can be configured as either input or output, making them ideal for interfacing with various external devices. By utilizing STM32CubeIDE’s intuitive interface and powerful HAL library functions, we will demonstrate how to set specific STM32 GPIO pins as outputs and generate specific patterns to control external LEDs.

With the knowledge gained from our previous LED testing, we will now venture beyond the onboard LEDs and interact with external components, unlocking even more possibilities for innovative embedded systems projects. So, let’s embark on this journey to master the art of using GPIO pins for controlling external LEDs with STM32 microcontrollers, taking full advantage of their capabilities and pushing the boundaries of embedded systems development.




Amazon Links:

STM32 Blue Pill

ST-Link V2

Jumper wires

Breadboard

Usb cable

Leds

10k Resistor

330ohm Resistor

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

STM32F103C8T6 Output GPIO Pins:

stm32 GPIO Pins

The STM32F103C8T6, also known as the Blue Pill board, is a popular and affordable development board featuring the STM32F103C8 series microcontroller from STMicroelectronics. This microcontroller is based on the ARM Cortex-M3 core and is part of the STM32F1 series. The board offers a range of GPIO (General Purpose Input Output) pins that can be used as both input and output pins. In this article, we will focus on the output GPIOs of the STM32F103C8T6 and how to control them using STM32CubeIDE.

The STM32F103C8T6 provides a total of 32 GPIO pins, labeled as GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, and GPIOF. Each port offers a different number of GPIO pins, making it versatile for various projects. These GPIO pins can be individually configured as digital outputs, allowing you to control external devices such as LEDs, relays, motors, and more.



To use a GPIO pin as an output, the following steps are typically taken:

GPIO Pin Initialization: First, the specific GPIO pin must be initialized using the appropriate GPIO port (A, B, C, etc.). In STM32CubeIDE, this is usually done through the “HAL_GPIO_Init()” function, which configures the pin’s mode, speed, and pull configuration.

Set Output Level: Once the GPIO pin is initialized as an output, you can set its output level (high or low) using the “HAL_GPIO_WritePin()” function. This will turn the external component connected to the GPIO pin ON or OFF accordingly.

Toggle Output Level: Alternatively, you can toggle the output level of the GPIO pin using the “HAL_GPIO_TogglePin()” function. This is often used to create blinking effects with LEDs or to change the state of a device.

The STM32CubeIDE provides a user-friendly graphical interface to configure and control GPIO pins, simplifying the development process. Additionally, the STM32 HAL (Hardware Abstraction Layer) library abstracts low-level hardware access, making it easier to work with the microcontroller’s peripherals, including GPIOs.

In conclusion, the STM32F103C8T6 offers a range of GPIO pins that can be configured as outputs, allowing developers to control external components in their projects. Whether you’re a beginner exploring the world of microcontrollers or an experienced developer working on advanced applications, understanding how to use the output GPIOs of the STM32F103C8T6 opens the door to a wide range of exciting possibilities.



Circuit diagram:

stm32 GPIO Pins

In this circuit setup, we have connected four LEDs to the blue pill STM32F103C8T6 microcontroller using GPIO pins PB6, PB7, PB8, and PB9. To prevent excessive current flow through the LEDs and protect them from damage, each LED is accompanied by a 330-ohm current-limiting resistor. The LEDs’ anodes, denoted by their longer leads, are individually connected to their respective GPIO pins through the resistors. The cathodes, identified by their shorter leads, are directly linked to the ground (GND) pin of the microcontroller. By configuring these GPIO pins as outputs in the STM32CubeIDE code, we can control the LEDs’ states. Setting the corresponding GPIO pin to logic high (1) will turn ON the respective LED, while setting it to logic low (0) will turn it OFF. This setup enables us to experiment with various LED control functionalities, such as blinking or toggling, using the STM32F103C8T6 microcontroller. As we delve into our projects, it’s essential to ensure proper connections and adhere to safety measures when working with electronic circuits.



STM32CubeIDE Project Creating:

To get started with STM32CubeIDE, open the software and follow the steps below. First, click on the “File” menu, and from the dropdown, select “New” -> “STM32 Project.”

stm32 GPIO Pins

In the “Target Selection” window, search for the specific STM32 part number you are using, such as the STM32F103C8T6. Once you’ve selected the appropriate STM32 device, click “Next.”

stm32 GPIO Pins

Name your project and leave all options as default and click “Finish.”

stm32 GPIO Pins

STM32CubeIDE will generate the necessary libraries for your project.

stm32 GPIO Pins




Once the “Pinout & Configuration” window opens, navigate to “System Core,” click on “SYS” in the dropdown menu, and select “Serial Wire” as the debug mode.

stm32 GPIO Pins

Configuring GPIOs is essential; you can do this by clicking on PB9 and choosing “GPIO_Output.” Repeat this step for PB8, PB7, and PB6. Save your project to keep the progress.

stm32 GPIO Pins

stm32 GPIO Pins

Click on the save button.

stm32 GPIO Pins



After clicking the “Save” button, a prompt will appear, asking, “Do you want to generate code?” Click “Yes” to allow STM32CubeIDE to generate the code, and wait for the process to complete.

stm32 GPIO Pins

Again click on the yes button.

stm32 GPIO Pins

STM32CubeIDE generated code will look like this At this point, you can control an external LED.

stm32 GPIO Pins

Simply paste the below code into the “while” section of your project to single external led

Single Led Controlling using STM32CubeIDE



Code explanation:

This line of code is using the HAL (Hardware Abstraction Layer) library function HAL_GPIO_WritePin to set the state of a specific GPIO pin. In this case, it is setting the GPIO pin 9 of GPIO Port B (GPIOB) to a logic high state (1). This typically means the pin is being set to a voltage level corresponding to a logical “1” (high) signal, which could be 3.3V or 5V, depending on the STM32 board’s operating voltage.

After setting GPIOB pin 9 to high, the code uses the HAL_Delay function to introduce a time delay of 500 milliseconds (half a second). During this delay, the program will pause its execution, and GPIOB pin 9 will remain in the logic high state.

After the 500-millisecond delay, this line of code sets GPIO pin 9 of GPIO Port B (GPIOB) to a logic low state (0). This means the pin is being set to a voltage level corresponding to a logical “0” (low) signal, typically 0V.

Following the second GPIO pin write operation, another HAL_Delay function is called to introduce another 500-millisecond delay. During this delay, GPIOB pin 9 will remain in the logic low state.

In summary, the provided code toggles (turns on and off) the state of GPIO pin 9 of GPIO Port B (GPIOB) with a 500-millisecond delay between each state change. This could be used to control an external device, like an LED, connected to GPIO pin 9. The result would be an LED blinking on for half a second and then off for half a second, repeatedly.

stm32 GPIO Pins

And click the “Build” button.

stm32 GPIO Pins

A successful build will be indicated by a message in the console.

stm32 GPIO Pins

To run the project and test the LED control, click the “Run” button.

stm32 GPIO Pins



The “Edit Configuration” window will appear, allowing you to configure the debugger. Click on the debugger menu and Choose “ST-LINK(OpenOCD)” from the debug probe dropdown menu.

stm32 GPIO Pins

Click on “Show Generator Options” and select “Software System Reset” as the reset mode.

stm32 GPIO Pins

stm32 GPIO Pins

Apply these changes

stm32 GPIO Pins

and proceed by clicking “OK.”

stm32 GPIO Pins




Once the project runs successfully, a message will be displayed in the console.

stm32 GPIO Pins

To ensure everything works as expected, disconnect and reconnect the ST-LINK v2 from your laptop or PC.

stm32 GPIO Pins

Congratulations! With these steps, you’ve successfully controlled a single external LED.

To extend this functionality and control multiple external LEDs, so paste the below-modified code in the while section into your project main.c file. Click the “Run” button again to execute the updated code and see the results.

multiple Leds Controlling using STM32CubeIDE



Code explanation:

This STM32 code demonstrates a simple pattern of turning on and off multiple GPIO pins connected to GPIO Port B (GPIOB) on the STM32F103C8T6 microcontroller. Each set of four HAL_GPIO_WritePin lines controls four different GPIO pins (GPIO_PIN_9, GPIO_PIN_8, GPIO_PIN_7, and GPIO_PIN_6) on GPIO Port B. The code follows a specific sequence of setting these pins to either a logic high state (1) or a logic low state (0) and introducing delays between state changes using HAL_Delay.

Here’s a step-by-step explanation of the code:

  • The first set of HAL_GPIO_WritePin lines set GPIO_PIN_9, GPIO_PIN_8, GPIO_PIN_7, and GPIO_PIN_6 to logic high (1), one after the other. This turns these four GPIO pins on simultaneously.
  • After setting these four pins to high, there is a HAL_Delay(500) function call, introducing a 500-millisecond delay. During this delay, all four pins will remain in the logic high state.
  • The second set of HAL_GPIO_WritePin lines set GPIO_PIN_9, GPIO_PIN_8, GPIO_PIN_7, and GPIO_PIN_6 to logic low (0), one after the other. This turns these four GPIO pins off simultaneously.
  • After setting these four pins to low, there is another HAL_Delay(500) function call, introducing another 500-millisecond delay. During this delay, all four pins will remain in the logic low state.
  • The code then repeats the same sequence of turning on and off the four GPIO pins in the same manner, creating a blinking pattern.

The pattern followed by the code is as follows:

  • All four GPIO pins are turned on (high).
  • A delay of 500 milliseconds occurs.
  • All four GPIO pins are turned off (low).
  • Another delay of 500 milliseconds occurs.
  • All four GPIO pins are turned on (high) again.
  • Another delay of 500 milliseconds occurs.
  • A different pattern is followed to turn on and off the GPIO pins.
  • The sequence repeats.

This code can be used to control external devices connected to these GPIO pins in a specific blinking pattern, such as LEDs blinking in a specific sequence.

stm32 GPIO Pins


Conclusion

In this article, we have learned how to configure and use Blue pill STM32F103C8T6 GPIO pins to interface with external devices. Additionally, we explored how to create an STM32 project STM32CubeIDE from scratch.

Throughout this article, we demonstrated the process of controlling external LEDs as an example of external devices using STM32F103C8T6 GPIO pins in STM32CubeIDE. However, you can apply the same principles to connect and control various other devices by utilizing these GPIO pins.

By properly configuring the GPIO pins, you can set them as inputs to read signals from external sensors or devices, or as outputs to drive signals to control external components. This flexibility allows you to design and implement a wide range of applications using STM32 microcontrollers.

Leave a Reply

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

Back to top button