fbpx

GPIOs control of components (LED,Push button)

Controlling LED and Push Button in STM32F411RE Nucleo Board: A Complete Guide

The STM32F411RE Nucleo board is a widely used development board, popular for its flexibility and processing power. One of the most common beginner-level projects is LED control using push buttons, which demonstrates how to manipulate GPIOs in embedded systems. In this guide, we will walk you through how to control an LED on the STM32F411RE Nucleo board using a push button.

This tutorial will cover the necessary steps for configuring GPIOs and writing the embedded C code to control an LED via a push button. By the end of this article, you’ll be comfortable with basic GPIO configuration and LED control.

Table of Contents:

  1. Introduction to STM32F411RE and GPIOs
  2. Pin Mapping: LED and Push Button on Nucleo Board
  3. Setting Up STM32CubeIDE and STM32CubeMX
  4. GPIO Configuration for LED and Push Button
  5. Writing the Code: Controlling LED via Push Button
  6. Real-World Applications of LED and Button Control
  7. In hindsight

1. Introduction to STM32F411RE and GPIOs

The STM32F411RE microcontroller, part of the STM32F4 series, is based on the ARM Cortex-M4 core. It features advanced GPIO capabilities, making it ideal for embedded systems requiring fast, real-time processing. GPIO (General Purpose Input/Output) pins are crucial in this microcontroller, allowing you to interface with external peripherals like LEDs, buttons, motors, and more.

In this project, we’ll use GPIO pins to control the onboard LED using a push button. This basic project provides an introduction to the following tasks:

  • Configuring GPIOs as inputs and outputs.
  • Reading input from the button.
  • Writing output to the LED.

2. Pin Mapping: LED and Push Button on Nucleo Board

Before we start with the configuration, it’s essential to understand the pin mapping of the STM32F411RE Nucleo board.

  • Onboard LED: The STM32F411RE Nucleo board has an onboard LED connected to pin PA5.
  • Push Button: You can use an external push button connected to pin PC13.

Here’s a summary of the pin connections:

  • LED Pin (PA5) – Digital output pin to control the LED.
  • Button Pin (PC13) – Digital input pin to read the push button’s state.

3. Setting Up STM32CubeIDE and STM32CubeMX

Before writing the code, we need to set up the environment:

Step 1: Install STM32CubeIDE

Download and install STM32CubeIDE, which integrates both STM32CubeMX (a graphical pin configuration tool) and the development environment. This makes it easier to configure pins and write embedded C code.

Step 2: Create a New Project in STM32CubeMX

  • Open STM32CubeIDE and select File > New > STM32 Project.
  • Choose the STM32F411RE microcontroller or select the Nucleo board in the Board Selector tab.
  • Click Start Project.

4. GPIO Configuration for LED and Push Button

Once the project is created, configure the GPIOs for the LED and Push Button.

Step 1: Configure GPIO for the LED (PA5)

  • In STM32CubeMX, locate PA5 and set its Mode to GPIO_Output. This allows the microcontroller to control the LED.

Step 2: Configure GPIO for the Button (PC13)

  • Locate PC13 and set it to GPIO_Input. This pin will read the state of the push button.
  • Enable the Pull-up Resistor for the button input. This ensures that when the button is not pressed, the pin reads a known high state.

Step 3: Generate the Initialization Code

Once the configuration is complete, click Project > Generate Code. This will generate the initialization code for the GPIO setup.

Here is a brief sample overview of what the generated code will look like in the main.c file:

/* GPIO Initialization Function */ void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* Enable GPIO clocks */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOC_CLK_ENABLE(); /* Configure GPIO pin for LED (PA5) */ GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Configure GPIO pin for Button (PC13) */ GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); }

5. Writing the Code: Controlling LED via Push Button

With the GPIOs initialized, the next step is to write the code that controls the LED based on the button press. The logic is simple:

  • When the button is pressed, turn the LED ON.
  • When the button is not pressed, turn the LED OFF.

Here’s the main code sample snippet for controlling the LED via the button:

int main(void) { /* Initialize the HAL Library */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); /* Infinite loop */ while (1) { /* Read the button state */ if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) { /* Button is pressed, turn the LED ON */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); } else { /* Button is not pressed, turn the LED OFF */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); } } }

Code Explanation:

  • HAL_GPIO_ReadPin() is used to read the state of the push button. The function returns GPIO_PIN_RESET when the button is pressed (assuming active low).
  • HAL_GPIO_WritePin() is used to control the LED on PA5. The LED turns on when the button is pressed and turns off otherwise.

6. Real-World Applications of LED and Button Control

Controlling an LED with a button is more than just a beginner-level project. It serves as the foundation for many real-world embedded applications:

  • User Feedback: LEDs are often used to provide visual feedback in systems. For example, an LED can indicate the system’s state based on user input from a button.
  • Embedded UI: Buttons and LEDs are frequently used in embedded user interfaces, where a button press toggles device modes or turns on/off features.
  • Interrupt-driven Systems: This project can be expanded to handle button presses using interrupts for more responsive systems.

7. In hindsight,

Controlling an LED using a push button on the STM32F411RE Nucleo board is a fundamental project that introduces GPIO configuration, basic embedded programming, and user input handling. With the STM32CubeIDE and STM32CubeMX tools, setting up GPIOs becomes seamless, allowing you to focus on building the application logic.

From here, you can explore more advanced topics like handling multiple buttons, adding debounce logic, or using interrupts for even more efficient button handling. This simple LED control project is a gateway to more complex embedded systems development with the STM32F411RE microcontroller.

By following this guide, you will have learned how to manipulate GPIOs in STM32F411RE to control peripherals like LEDs using a push button. This knowledge serves as a foundation for building more sophisticated embedded systems, such as user interfaces, device controllers, and more.

To learn further, please click on the link and register yourself to learn…https://link.growthflow.ai/widget/form/FDFJBysxtKRdh8Y1jWng?notrack=true

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping