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 –
- Pin_13 from Port C has been selected to read the input from the push button.
- 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:
- 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’.
- 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.
- 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();.
- I have used another HAL API to reset the LED pin (Pin_5 in port A) if the push button is not pressed.
- 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 */
}
- In this logic, the LED is turned ON for 1 second and then turned OFF if the push button is pressed.
- 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.