Ex.
No:8
Task scheduling process using ESP32 Dual Core with FreeRTOS
Aim:
Create two tasks and turn ON/OFF the LED based on task scheduling process using ESP32 and
FreeRTOS
Components Required:
1. One ESP32 board
2. Two LEDs
Prerequisite:
1. Connect the positive leg of LED1 to the GPIO Pin 32 in ESP32 board.
2. Connect the negative leg of LED1 to the GND pin in ESP32 board.
3. Connect the positive leg of LED2 to the GPIO Pin 25 in ESP32 board.
4. Connect the negative leg of LED2 to the GND pin in ESP32 board.
Block Diagram:
1
Basic Functions:
TaskHandle_t – Define a task handle
xTaskCreatePinnedToCore() – to create the task for core 0 and core 1
It takes sevent parameters
o Name of the function
o Name of the task
o Stack size
o Task Input parameter
o Priority of task (0 is the lowest priority)
o Task handle
o Core ID
Task1code() - code for LED to blink every 0.5 secs
o This function takes in a single global argument called a parameter.
o Display the core ID on serial monitor for the running task
o Infinite loop can be used to blink the LED with delay 0.5 seconds.
pinMode( )
Specific pin number is set as the INPUT or OUTPUT in the pinMode () function
Syntax:
pinMode (pin, mode)
digitalRead () function will read the HIGH/LOW value from the digital pin
digitalWrite () function is used to set the HIGH/LOW value of the digital pin
Code:
TaskHandle_t Task1;
TaskHandle_t Task2;
const int led_1 = 32;
const int led_2 = 25;
void setup() {
Serial.begin(115200);
pinMode(led_1, OUTPUT);
pinMode(led_2, OUTPUT);
xTaskCreatePinnedToCore(Task1code,"Task1",10000,NULL,1,&Task1,0);
delay(500);
xTaskCreatePinnedToCore(Task2code,"Task2",10000,NULL,1,&Task2,1);
delay(500);
}
void Task1code( void * parameter ){
Serial.print("Task1 is running on core ");
Serial.println(xPortGetCoreID());
for(;;){
digitalWrite(led_1, HIGH);
delay(500);
2
Ex.No:8
Task scheduling process using ESP32 Dual Core with FreeRTOS
digitalWrite(led_1, LOW);
delay(500);
}
}
void Task2code( void * parameter ){
Serial.print("Task2 is running on core ");
Serial.println(xPortGetCoreID());
for(;;){
digitalWrite(led_2, HIGH);
delay(1000);
digitalWrite(led_2, LOW);
delay(1000);
}
}
void loop() {
Working:
Two separate tasks are created and running independently.
Task scheduling function controls the LED blinking process for every 0.5 seconds and
displays the core ID on serial monitor.
Serial monitor in the Arduino IDE is used for debugging purpose.
Output:
The LED is on or off every 0.5 seconds according to the task scheduling process, and the core ID
is displayed on the serial monitor