Project

RTOS Project - To Turn ON an LED for 1 second and then Turn it OFF when a push button is pressed on Nucleo F401RE board using Hardware Abstraction Layer APIs

Project Objective – By using STM32 IDE Nucleo F401RE board, I have done a project to turn ON a LED for 1 second when a push button is pressed.

I/O selection –

  1. Pin_13 from Port C has been selected to read the input from the push button.
  2. Pin_5 from Port A has been selected to write the output to the LED LD_2.

Steps taken to write the code for the project:

  1. I have used Hardware Abstraction Layer API to read the signal from the push button which is Pin_13 in Port C of the microcontroller and store the value in a variable named ‘button_val’.
  2. After checking whether the button value is 0 or 1, I have used another HAL API to write to Pin_5 which is the LED (LD_2) pin and set it.
  3. To ensure that the pin is set for just 1 second, I have used a delay of 1000 microseconds. For this, I have used another HAL API called HAL_Delay();.
  4. I have used another HAL API to reset the LED pin (Pin_5 in port A) if the push button is not pressed.
  5. The part of the code which executes the above mentioned steps is given below:

while (1)

{

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

//read the status of GPIO Button pin

button_val=HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin);

// check if it is high or led

if(button_val==0)

{

//if pressed turn on led

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , SET);

HAL_Delay(1000);

}

else

{

//if not pressed turn off led

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , RESET);

}

}

/* USER CODE END 3 */

}

  1. In this logic, the LED is turned ON for 1 second and then turned OFF if the push button is pressed.
  1. I have attached the entire main.c file as well as a video of the Nucleo board which shows the LED turning on for 1 second and then turning OFF when the push button is pressed.
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.