1-STM32 GPIO: LED BLINK
- ramazanycel
- May 7, 2024
- 4 min read
Updated: May 7, 2024
The STM32L476 LED blink project showcases a fundamental aspect of embedded systems: controlling peripherals using microcontrollers. In this case, we utilize the STM32L476 microcontroller to blink a LED, a quintessential beginner project in the realm of embedded systems.
In this session, we will together solve three examples. I will explain the detailed solution for two of them, and for the third one, I will guide you on how to do it, and I'll expect the answer from you
The STM32L476 microcontroller comes in various packages with different pin configurations. For this project, let's consider the STM32L476RG microcontroller, which is commonly found in development boards like the STM32L4 Discovery kit. This microcontroller features numerous GPIO (General-Purpose Input/Output) pins that can be configured for various purposes, including controlling LEDs.
For the LED blinking project, we typically connect an LED to one of the GPIO pins configured for output. In this example, let's connect an LED to Pin PA5. PA5 corresponds to Pin 5 on Port A of the microcontroller.
Required Parts For Example:
STM32L476 development board.
LEDs (Light Emitting Diodes).
220 Ohm Resistor for current limiting.
Breadboard (optional).
Working:
Initialization: First, we initialize the GPIO pin connected to the LED as an output pin. This is done by configuring the corresponding GPIO port (in this case, Port A) and pin (PA5) for output mode.
Blinking Logic: Once the GPIO pin is configured, we set the state of the GPIO pin at regular intervals. This action turns the LED on or off.
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, RESET);
Delay: To control the blinking frequency, we introduce a delay between each toggle operation. This delay determines how quickly or slowly the LED blinks. By adjusting the duration of this delay, we can change the blinking rate of the LED.
HAL_Delay(500); //500 ms
Infinite Loop: The blinking process continues indefinitely as long as the microcontroller remains powered on. This is achieved through an infinite loop in the code, ensuring that the LED keeps blinking until the power is turned off or the program is halted.
CUBEIDE Configuration Steps:
Open CUBEIDE and initiate a new project.
Select the appropriate STM32L476 microcontroller from the available options.
Configure GPIO pins for LED output. Set up PA5 as OUTPUT.
Set up the system clock to 72 Mhz.
Generate initialization code through CUBEIDE.
Adjust project settings as needed and build the project.
"Your time is limited, don't waste it living someone else's life." Steve Jobs.
CODE:
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
while (1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, RESET);
HAL_Delay(500);
}
}
Result
The STM32L476 LED blink project demonstrates the practical implementation of GPIO (General Purpose Input/Output) pin control on the STM32L476 microcontroller to achieve a blinking effect on an LED.
Through the provided code and configuration steps, users gain insight into initializing GPIO pins, setting up the system clock, and generating delays.
Upon successful execution of the project, the LED connected to the specified GPIO pin blinks at a regular interval, showcasing the interaction between software and hardware components in embedded systems development.
This example serves as a foundation for beginners to delve deeper into microcontroller programming and explore more complex projects.
3 LEDs BLINK Example
Let's make our example a little more challenging now. In the previous example, we used one LED. Now, what I'm asking from you is to use 3 LEDs, each flashing on and off with one-second intervals. You can try to do it yourself without looking here if you want. Then continue reading to check. Let's begin.
This project demonstrates a simple LED blinking project using the STM32L476 microcontroller. Instead of blinking just one LED, we'll expand the project to blink three LEDs independently.
It serves as a practical introduction to GPIO pin manipulation, multitasking, and resource management on the STM32 platform.
Required Parts For Example:
STM32L476 development board.
Three LEDs (different colors for better visual distinction).
220 Ohm Three Resistors for current limiting.
Breadboard (optional).
CUBEIDE Configuration Steps:
Set up a new project in CUBEIDE.
Select the STM32L476 microcontroller variant.
Configure GPIO pins for each LED. Set PA5, PA6, PA7 as OUTPUT.
Configure system clock as 72 MHZ.
Generate initialization code and project configuration.
CODE:
Initialize GPIO pins for LED control.
Implement a loop to toggle the LED state at a specific interval.
Example code snippets illustrating GPIO initialization and LED toggling.
Explanation of each code segment to aid understanding.
int main(void)
{
HAL_Init();
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
while (1)
{
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_6, RESET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, RESET);
HAL_Delay(500);
}
}
Result:
LED blinks at the specified rate.
Demonstration of the project in action, either through a video or images.
Conclusion highlighting the successful implementation of the LED blink project and the foundational knowledge gained in microcontroller programming.
Now it's your turn.
What I'm asking from you is to light up and turn off 3 LEDs alternately, forwards and backwards. If you have any questions, you can ask them in the comments, or if those who solve the problem can post their solutions in the comments, we can help those who couldn't do it.
Comments