STM32F4 Discovery Led Tutorial
STM32F4 Discovery Led Tutorial

In this post i will try to explain how to work with led’s present on the STM32F411 Discovery. I will be using HAL drivers provided by the ST for driving the LED’s that means turning led on and off. Turning on and off led is nothing but driving GPIO pin High and low.

STM32F4 discovery LED schematic

There are four leds on board which are connected to PORT D of the stm32f411 microcontroller. In schematic LEDs negative side is connected to ground and positive is connected to pin numbers 12,13,14,15 respectively.

Getting started with code

In order to drive the gpio of microcontroller first include stm32f4xx_hal_gpio.c and stm32f4xx_hal_gpio.h file into your project and uncomment the HAL_GPIO_MODULE_ENABLED macro in stm32f4xx_hal_conf.h file. Uncommenting HAL_GPIO_MODULE_ENABLED macro makes the gpio library files stm32f4xx_hal_gpio.c and stm32f4xx_hal_gpio.h compile with your project and its apis available into your project which are needed to drive the gpio pins.

stm32f4xx_hal_conf.h
///////////////////////// main.c  //////////////////

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();
	
  GPIO_InitTypeDef GPIO_InitStruct = {0};
	
  __HAL_RCC_GPIOD_CLK_ENABLE();

  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
	
  while (1)
  {
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET);
		HAL_Delay(50);
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_RESET);
		HAL_Delay(50);

		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_SET);
		HAL_Delay(50);
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_15,GPIO_PIN_RESET);
		HAL_Delay(50);

		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_SET);
		HAL_Delay(50);
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_13,GPIO_PIN_RESET);
		HAL_Delay(50);

		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_SET);
		HAL_Delay(50);
		HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12,GPIO_PIN_RESET);
		HAL_Delay(50);		
  }
}

Brief explanation

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

HAL_Init This function is used to initialize the HAL Library. SystemClock_Config is used to configure the clock. This project will use the default clock configuration provided by STM cubemx.

GPIO_InitTypeDef GPIO_InitStruct = {0};
	
  __HAL_RCC_GPIOD_CLK_ENABLE();

  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD,&GPIO_InitStruct);

Before using pins of specific port we have enable clock for that port. To configure a GPIO we use the HAL_GPIO_Init function. GPIO_InitTypeDef is the C struct used to configure the GPIO. The members of GPIO_InitTypeDef is as follows.

typedef struct {
      uint32_t Pin;
      uint32_t Mode;
      uint32_t Pull;
      uint32_t Speed;
      uint32_t Alternate;
} GPIO_InitTypeDef;

This is the role of each field of the struct:

  1. Pin – it is the number of the pins we are going to configure.
  2. Mode: it is the operating mode of the pin
  3. Pull: specifies the Pull-up or Pull-Down activation for the selected pins
  4. Speed: defines the pin speed
  5. Alternate: specifies which peripheral to associate to the pin
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_SET);
HAL_Delay(50);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_14,GPIO_PIN_RESET);
HAL_Delay(50);

HAL_GPIO_WritePin() is used to set the specified GPIO pin to High or Low.
HAL_Delay function is used for generating delay in milli seconds.

In the above part of main function the four leds of board turn on and off in the circular manner.