Task2.2D

by roshni4549 in Circuits > LEDs

18 Views, 0 Favorites, 0 Comments

Task2.2D

Screenshot 2024-04-13 122754.png
Screenshot 2024-04-13 121917.png


In this tutorial, we'll explore how to program the STM32 Black Pill development board to blink an external LED using the STM32CubeIDE and the Hardware Abstraction Layer (HAL) library. we will setting up the development environment, configuring GPIO pins, and writing code to control the external LED.

Supplies

  • STM32 Black Pill development board (STM32F103C8T6)
  • USB to UART converter (if not built-in)
  • External LED
  • Resistor (optional, depending on the LED)
  • Jumper wires
  • STM32CubeIDE installed on your computer
  • USB cable

Setting Up STM32CubeIDE

Screenshot 2024-04-13 122014.png


  1. Download and install STM32CubeIDE from the STMicroelectronics website.
  2. Launch STM32CubeIDE and create a new STM32 project.
  3. Select the appropriate board (STM32F401C) and configure your project settings.

Configuring GPIO Pins

Screenshot 2024-04-13 122051.png


  1. Identify the GPIO pin connected to the external LED on your Black Pill board.
  2. In the "Pinout & Configuration" tab of STM32CubeIDE, configure the identified GPIO pin as an output.
  3. Save the pin configuration.

Wiring the External LED


  1. Connect the anode (+) of the external LED to the configured GPIO pin on the Black Pill board.
  2. Connect the cathode (-) of the LED to ground (GND) on the Black Pill board.
  3. Optionally, insert a resistor between the GPIO pin and the LED to limit the current if necessary.


Writing HAL Code

  1. Navigate to the "Src" folder in your project and open the main.c file.
  2. Include the necessary HAL libraries at the top of the file:
c

Copy code
#include "main.h" #include "stm32f1xx_hal.h"

int main(void)

{

HAL_Init();

SystemClock_Config();


// Initialize GPIO pin for external LED

GPIO_InitTypeDef GPIO_InitStruct;

__HAL_RCC_GPIOA_CLK_ENABLE(); // Change GPIOA to appropriate port

GPIO_InitStruct.Pin = GPIO_PIN_5; // Change GPIO_PIN_5 to appropriate pin

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // Change GPIOA to appropriate port


// Main loop

while (1)

{

// Toggle LED state

HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); // Change GPIOA and GPIO_PIN_5 to appropriate port and pin


// Delay

HAL_Delay(1000);

}

}

Build and Flash


  1. Build your project in STM32CubeIDE to ensure there are no errors.
  2. Connect your STM32 Black Pill board to your computer using a USB cable.
  3. Press the "Run" button in STM32CubeIDE to flash the code onto the board.

Testing


Once the code is flashed onto the STM32 Black Pill board, the external LED should start blinking at a 1-second interval.

Conclusion


In this tutorial, we've covered the basics of setting up STM32CubeIDE, configuring GPIO pins, and writing HAL code to control an external LED connected to the STM32 Black Pill development board.