CREATING A FREE RTOS TASK FOR BLINKING A LED IN ESP32.

Project Title

Blinking a LED by creating a freertos task in ESP-32

Components Required

  • esp-32 board
  • leds
  • jumper wires
  • breadboard

Working Principle

First of all for we must create a task where we code for blinking a led I have created two task where i have placed different led with different delays. APart from led task there will be one more task created by kernel called idle task. The idle task run when there is no task for execution. The idle task always have lowest priority that is 0.

RTOS Working
fig 1

In our program task 1 has highest priority (i.e 10) and task 2 has second priority (i.e 9). First of all our task 1 will be in running state as it has highest priority.When the function vTaskDelay(2000 / portTICK_PERIOD_MS); is called task 1 will go in suspended state and task 2 will be in running state and the same process continuous. The idle task will run when there is no any task to execute.

Arduino FreeRTOS LED Blinking State Diagram
fig 2

Connection Diagram

fig-3


Coding

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"

extern "C" void app_main();

TaskHandle_t myTask1Handle = NULL;
TaskHandle_t myTask2Handle = NULL;

#define BLINK_GPIO1 GPIO_NUM_14
#define BLINK_GPIO2 GPIO_NUM_27

void task1(void *arg)
{
  gpio_pad_select_gpio(BLINK_GPIO1);
  gpio_set_direction(BLINK_GPIO1, GPIO_MODE_OUTPUT);
  while(1) {
  printf("Turning off the LED\n");
      gpio_set_level(BLINK_GPIO1, 0);
      vTaskDelay(1000 / portTICK_PERIOD_MS);
  printf("Turning on the LED\n");
      gpio_set_level(BLINK_GPIO1, 1);
      vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void task2(void *arg)
{
  gpio_pad_select_gpio(BLINK_GPIO2);
  gpio_set_direction(BLINK_GPIO2, GPIO_MODE_OUTPUT);
  while(1) {
  printf("Turning off the LED\n");
      gpio_set_level(BLINK_GPIO2, 0);
      vTaskDelay(2000 / portTICK_PERIOD_MS);
  printf("Turning on the LED\n");
      gpio_set_level(BLINK_GPIO2, 1);
      vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}

void app_main(void)
{

  xTaskCreate(task1, "task1", 4096, NULL,10, &myTask1Handle);
  xTaskCreate(task2, "task2", 4096, NULL, 9, &myTask2Handle);

}

Overview

fig-blinking led using esp-32
fig-blinking led using esp-32

Recommended For You

About the Author: admin

Leave a Reply

Your email address will not be published.