This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Lab05 - Hardware initialization

This exercise deals with NUCLEO I/O initialization of the User LED and User Button. The exercise also deals with the usage of these resources. The definition of the LED and Button connection is described in the board user manual. Go ahead and download the files for this lab session lab5.zip.


  • Find the CPU pin and port for the User LED (green) output control.
  • Find the CPU pin and port for the User Button input control.


The NUCLEO board contains a green LED controlled by the CPU. The LED is connected to the CPU pin XX of the port GPIOXX. The output needs to be initialized through the MX_GPIO_Init function as depicted below. The initialization should be placed into static void MX_GPIO_Init(void) function of the project.

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */

  /*Configure GPIO pin Output Level */

  /*Configure GPIO pin : LD2_Pin */
  GPIO_InitStruct.Pin = LD2_Pin;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);


The configuration for button B1 (BLUE BUTTTON), is also inside function static void MX_GPIO_Init(void).

  /*Configure GPIO pin : B1_Pin */
  GPIO_InitStruct.Pin = B1_Pin;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

The initialization of the input pin is performed in the same way as the output pin with a small difference: GPIO_InitStruct.Mode.

PIN Reading and Writing Functions

The function HAL_GPIO_ReadPin (B1_GPIO_Port, B1_Pin); will read the the status of B1 button. In order to write to a pin, the function HAL_GPIO_WritePin(GPIOA,LD2_Pin,GPIO_PIN_SET); is used

Lab Tasks

  • Write a code that will increase an integer by one if the button is pressed once. The initial value of that integer is zero. If they hold down the button, it should only increase by 1. Every time that value of an integer is changed, its new value is transmitted through USART to PC. Use picocom to receive the data.
  • Write a code that keeps the LED on while the button is pressed. If the button is released, led turns off.
  • Write a code that will print your name character by character through USART. Every time that you press a button, a character is sent through USART and nothing is sent anymore until button is pressed again.
courses/be5b99cpl/labs/lab05.txt · Last modified: 2021/10/20 11:01 by brouggeo