stm32 projects

Controlling External LED Using a Pushbutton with STM32 CubeIDE

Introduction

Embedded systems often require user interaction, and one of the most common ways to achieve this is through pushbuttons. In my previous article, I explained in detail how to control multiple external leds but, in this article, we will explore how to control an external LED using a pushbutton with STM32 CubeIDE. This project will demonstrate how to set up the pushbutton as an input and use it to toggle the state of an LED connected to an STM32 microcontroller. STM32 CubeIDE will be our development environment of choice, providing a streamlined development process for this project.




Understanding STM32 CubeIDE

STM32 CubeIDE is a powerful and user-friendly integrated development environment tailored specifically for STM32 microcontrollers. It combines various tools, such as STM32CubeMX for peripheral configuration and STM32CubeHAL for low-level abstraction, making it easy to develop applications for STM32 devices. Before we begin, make sure you have installed CubeIDE, which can be downloaded from the STMicroelectronics website. In my previous article, I explained in detail how to download and install CubeIDE.

About STM32f103c8t6 Blue Pill:

Pushbutton with STM32 CubeIDE

The STM32F103C8T6, commonly known as the “Blue Pill,” is a popular microcontroller board based on the STM32F103C8T6 microcontroller from STMicroelectronics. It has gained widespread popularity due to its affordability, ease of use, and strong performance. Let’s explore some key features and characteristics of the STM32F103C8T6 Blue Pill board:

  • Microcontroller: The Blue Pill is powered by the STM32F103C8T6 microcontroller, which is part of the STM32F1 series. This 32-bit ARM Cortex-M3 microcontroller operates at a clock speed of up to 72 MHz and provides a wide range of peripherals, including GPIOs, timers, UART, SPI, I2C, ADC, and more.
  • Flash Memory and RAM: The STM32F103C8T6 has 64KB of Flash memory for program storage and 20KB of SRAM for data storage. This memory capacity allows for the implementation of complex applications and firmware.
  • Board Layout: The Blue Pill board is a compact, single-sided board with a standard 2.54mm pitch, making it compatible with breadboards and various other prototyping setups. It features various GPIO pins, power pins, and debug/programming interface pins.
  • Power Supply: The board can be powered either via USB (5V). It incorporates a 3.3V voltage regulator to power the STM32 microcontroller and other components on the board.
  • USB-to-UART Interface: The board is equipped with a built-in USB-to-UART converter, which allows for easy communication and programming via the USB interface.
  • Integrated LED: The board includes an integrated LED connected to PA1, which can be used for simple visual feedback during testing and debugging.
  • Programming and Debugging: The Blue Pill board supports several programming and debugging options, including ST-LINK/V2, which is an embedded debugger/programmer. It allows users to program and debug the microcontroller using the STM32CubeIDE or other compatible tools.
  • Community Support: Due to its popularity, the Blue Pill board has a large and active community of developers and enthusiasts. This community provides ample resources, tutorials, and libraries to support beginners and experienced users alike.

Applications:

The STM32F103C8T6 Blue Pill is versatile and well-suited for a wide range of applications, including but not limited to:

  • Home automation and smart home projects
  • Industrial control and automation systems
  • Robotics and automation projects
  • Data logging and monitoring systems
  • IoT (Internet of Things) devices
  • Sensor interfacing and data processing applications
  • DIY electronics projects and prototypes



Creating Stm32 CubeIDE Project:

open your STM32 CubeIDE. Click on “File” in the menu, then choose “New,” and under “New,” click on “STM32 Project.”

Pushbutton with STM32 CubeIDE

In the “Target Selection” window, search for the specific STM32 microcontroller you are using. I am using the STM32F103C8T6, so I entered this in the “Commercial Part Number” field. Select your STM32 board and click “Next.”

Pushbutton with STM32 CubeIDE

In the “Setup STM32 Project” window, choose a name for your project. I selected “Pushbutton,” and left the other options as default. After that, click “Finish.”

Pushbutton with STM32 CubeIDE



In the “Pinout and Configuration” window, click on the “System Core” tab. Under “System Core,” click on “SYS,” and choose “Serial Wire” as the debug mode.

Pushbutton with STM32 CubeIDE

I have connected an LED to PA1, so I will select PA1 as an output GPIO.

Pushbutton with STM32 CubeIDE

Additionally, I connected a pushbutton to PB1, so I selected PB1 as an input GPIO.

Pushbutton with STM32 CubeIDE

After selecting the GPIO pins, click on the “Save” button to generate the code.

Pushbutton with STM32 CubeIDE



Led and Pushbutton with STM32 Circuit diagram:

Pushbutton with STM32 CubeIDE

In this circuit diagram, we have a pushbutton and an LED connected to an STM32 microcontroller. The pushbutton is connected to the PB1 pin of the STM32, along with a 10k-ohm resistor. The pushbutton is a two-terminal mechanical switch, and one terminal is connected to the ground pin of the STM32, while the other is connected to the resistor. The resistor’s second leg is connected to the 3.3V supply, providing a pull-up effect. When the pushbutton is not pressed, the pull-up resistor pulls the PB1 pin to a logic HIGH state, maintaining a stable input.

On the other side of the circuit, we have an LED connected to the A1 pin of the STM32, along with a 330-ohm resistor. The LED is a two-terminal component, and one terminal is connected to the A1 pin through the resistor, while the other is connected to the ground (GND). This arrangement ensures that the LED is properly current-limited to protect it from excessive current flow and potential damage. When the A1 pin is set to a logic HIGH state, the LED turns ON, and when it is set to a logic LOW state, the LED turns OFF.

Together, this simple circuit allows us to control the LED’s state using the pushbutton. When the pushbutton is pressed, it changes the PB1 pin’s state to logic LOW, turning OFF the LED. And when the pushbutton is released, the pull-up resistor restores the PB1 pin’s state to logic HIGH, turning ON the LED. This circuit is a basic example of how to use a pushbutton to control an LED with an STM32 microcontroller.




Controlling led with pushbutton using stm32 CubeIDE Program:



Program Explanation:

This is our main code the rest of the code in STM32 CubeIDE is automatically generated.

This code snippet demonstrates a simple program in STM32 CubeIDE for debouncing a button and controlling an LED based on its state. The main loop runs indefinitely, constantly monitoring the button connected to GPIO_PIN_1 of GPIOB (GPIO port B). It uses the variable prevButtonState to store the previous state of the button, initialized to GPIO_PIN_SET (indicating the button is released).

During each iteration of the loop, it reads the current state of the button into the variable currentButtonState. If there is a change in the button state compared to the previous state, the program proceeds with debouncing. It introduces a 50-millisecond delay to allow for any mechanical noise or bouncing to settle. After the delay, it reads the button state again to ensure a stable and accurate input.

If the button is detected as being pressed (active low) after debouncing, the program toggles the state of the LED connected to GPIO_PIN_1 of GPIOA (GPIO port A). This means if the LED was off, it will be turned on, and if it was on, it will be turned off.

The debouncing technique ensures that the LED’s state changes only when the button press or release is stable, ignoring any transient noise. By doing so, the program prevents unintended and rapid toggling of the LED, providing a more reliable user experience with the button-LED interface.

Copy the full code or just copy the “while” section code and paste it.



Stm32 CubeIDE Project Uploading:

Before uploading the project code, ensure to connect the ST-Link V2 with the STM32 microcontroller. The connection diagram is provided below:

Pushbutton with STM32 CubeIDE

Connect the SWDIO (Serial Wire Debug Input/Output) pin of the ST-Link V2 to the SWDIO pin on the STM32 microcontroller. This allows bidirectional communication between the ST-Link V2 and the microcontroller.

Connect the SWCLK (Serial Wire Clock) pin of the ST-Link V2 to the SWCLK pin on the STM32 microcontroller. This clock signal synchronizes data transmission between the two devices.

Establish a common ground reference by connecting the GND pin of the ST-Link V2 to the GND pin on the STM32 microcontroller.

Provide power to the STM32 microcontroller by connecting the 3.3V pin of the ST-Link V2 to the 3.3V power supply on the STM32 board.

Once the connection between Jab ST-Link V2 and STM32 is complete, connect the ST-Link V2 to your PC or laptop, and then click on the “build” button.

Pushbutton with STM32 CubeIDE

Once the build processing is complete, click on the green play button. After clicking the play button, the “Edit Launch Configuration Properties” window will open.

Pushbutton with STM32 CubeIDE

In this window, navigate to the “Debugger” menu and select “Debug Probe” as “ST-Link (OpenOCD).”

Pushbutton with STM32 CubeIDE

Then, click on the “Show Generator Options” button and choose “Software System Reset” as the reset mode.



Pushbutton with STM32 CubeIDE

Pushbutton with STM32 CubeIDE

Once you’ve made these selections, click on the “Apply” button to save all the changes. Finally, press the “OK” button to upload the program.

Pushbutton with STM32 CubeIDE

When you see the message “shutdown command invoked” on your console, disconnect the ST-Link V2 from your PC or laptop. Then, reconnect it to your PC, laptop, or any other 5V power source. Afterward, proceed to practically test your project.

Pushbutton with STM32 CubeIDE




Conclusion

In this article, we explored how to control an external LED using a pushbutton with STM32 CubeIDE. We discussed the project setup, the code required to read the pushbutton state, and how to toggle the LED accordingly. With the versatility and ease-of-use of STM32 CubeIDE, you can now integrate user interactions into your STM32-based embedded systems effortlessly. This opens up a wide range of possibilities for creating interactive and responsive applications.

Leave a Reply

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

Back to top button