Getting started with FreeRTOS with ESP32

Have you ever wanted to run ESP32 on a time constraint mode, or wanted to run two loops at the same time, well the solution is FreeRTOS

BeginnerProtip2 hours1,586
Getting started with FreeRTOS with ESP32

Things used in this project

Hardware components

Espressif ESP32 Development Board - Developer Edition
Espressif ESP32 Development Board - Developer Edition
×1
5 mm LED: Red
5 mm LED: Red
×1
Resistor 330 ohm
Resistor 330 ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematics for the circuit

Schematics for Matrix Multiplexing Circuit

Code

Code for Multiplexing LED Matrix

C/C++
int a[32][8]={{1,1,1,1,1,1,1,1},
              {1,1,1,1,1,1,1,1},
              {1,1,1,1,1,1,1,1},
              {1,1,1,0,0,1,1,1},
              {1,1,1,0,0,1,1,1},
              {1,1,1,1,1,1,1,1},
              {1,1,1,1,1,1,1,1},
              {1,1,1,1,1,1,1,1},

              {1,1,1,1,1,1,1,1},
              {1,1,1,1,1,1,1,1},
              {1,1,0,0,0,0,1,1},
              {1,1,0,1,1,0,1,1},
              {1,1,0,1,1,0,1,1},
              {1,1,0,0,0,0,1,1},
              {1,1,1,1,1,1,1,1},
              {1,1,1,1,1,1,1,1},

              {1,1,1,1,1,1,1,1},
              {1,0,0,0,0,0,0,1},
              {1,0,1,1,1,1,0,1},
              {1,0,1,1,1,1,0,1},
              {1,0,1,1,1,1,0,1},
              {1,0,1,1,1,1,0,1},
              {1,0,0,0,0,0,0,1},
              {1,1,1,1,1,1,1,1},

              {0,0,0,0,0,0,0,0},
              {0,1,1,1,1,1,1,0},
              {0,1,1,1,1,1,1,0},
              {0,1,1,1,1,1,1,0},
              {0,1,1,1,1,1,1,0},
              {0,1,1,1,1,1,1,0},
              {0,1,1,1,1,1,1,0},
              {0,0,0,0,0,0,0,0}};



int i=0;

int a1=13;
int a2=12;
int a3=14;
int a4=27;
int a5=26;
int a6=25;
int a7=33;
int a8=32;

int b1=18;
int b2=19;
int b3=21;
int b4=22;
int b5=15;
int b6=2;
int b7=4;
int b8=5;

#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif

void toggleLED_1(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b1,HIGH);
    digitalWrite(a1,a[0+i][0]);
    digitalWrite(a2,a[0+i][1]);
    digitalWrite(a3,a[0+i][2]);
    digitalWrite(a4,a[0+i][3]);
    digitalWrite(a5,a[0+i][4]);
    digitalWrite(a6,a[0+i][5]);
    digitalWrite(a7,a[0+i][6]);
    digitalWrite(a8,a[0+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}

void toggleLED_2(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b2,HIGH);
    digitalWrite(a1,a[1+i][0]);
    digitalWrite(a2,a[1+i][1]);
    digitalWrite(a3,a[1+i][2]);
    digitalWrite(a4,a[1+i][3]);
    digitalWrite(a5,a[1+i][4]);
    digitalWrite(a6,a[1+i][5]);
    digitalWrite(a7,a[1+i][6]);
    digitalWrite(a8,a[1+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}
void toggleLED_3(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b3,HIGH);
    digitalWrite(a1,a[2+i][0]);
    digitalWrite(a2,a[2+i][1]);
    digitalWrite(a3,a[2+i][2]);
    digitalWrite(a4,a[2+i][3]);
    digitalWrite(a5,a[2+i][4]);
    digitalWrite(a6,a[2+i][5]);
    digitalWrite(a7,a[2+i][6]);
    digitalWrite(a8,a[2+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}

void toggleLED_4(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b4,HIGH);
    digitalWrite(a1,a[3+i][0]);
    digitalWrite(a2,a[3+i][1]);
    digitalWrite(a3,a[3+i][2]);
    digitalWrite(a4,a[3+i][3]);
    digitalWrite(a5,a[3+i][4]);
    digitalWrite(a6,a[3+i][5]);
    digitalWrite(a7,a[3+i][6]);
    digitalWrite(a8,a[3+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}
void toggleLED_5(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b5,HIGH);
    digitalWrite(a1,a[4+i][0]);
    digitalWrite(a2,a[4+i][1]);
    digitalWrite(a3,a[4+i][2]);
    digitalWrite(a4,a[4+i][3]);
    digitalWrite(a5,a[4+i][4]);
    digitalWrite(a6,a[4+i][5]);
    digitalWrite(a7,a[4+i][6]);
    digitalWrite(a8,a[4+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}

void toggleLED_6(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b6,HIGH);
    digitalWrite(a1,a[5+i][0]);
    digitalWrite(a2,a[5+i][1]);
    digitalWrite(a3,a[5+i][2]);
    digitalWrite(a4,a[5+i][3]);
    digitalWrite(a5,a[5+i][4]);
    digitalWrite(a6,a[5+i][5]);
    digitalWrite(a7,a[5+i][6]);
    digitalWrite(a8,a[5+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}
void toggleLED_7(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b7,HIGH);
    digitalWrite(a1,a[6+i][0]);
    digitalWrite(a2,a[6+i][1]);
    digitalWrite(a3,a[6+i][2]);
    digitalWrite(a4,a[6+i][3]);
    digitalWrite(a5,a[6+i][4]);
    digitalWrite(a6,a[6+i][5]);
    digitalWrite(a7,a[6+i][6]);
    digitalWrite(a8,a[6+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}

void toggleLED_8(void *parameter)
{
  while(1)
  {
    digitalWrite(a1,1);
    digitalWrite(a2,1);
    digitalWrite(a3,1);
    digitalWrite(a4,1);
    digitalWrite(a5,1);
    digitalWrite(a6,1);
    digitalWrite(a7,1);
    digitalWrite(a8,1);
    digitalWrite(b1,0);
    digitalWrite(b2,0);
    digitalWrite(b3,0);
    digitalWrite(b4,0);
    digitalWrite(b5,0);
    digitalWrite(b6,0);
    digitalWrite(b7,0);
    digitalWrite(b8,0);

    digitalWrite(b8,HIGH);
    digitalWrite(a1,a[7+i][0]);
    digitalWrite(a2,a[7+i][1]);
    digitalWrite(a3,a[7+i][2]);
    digitalWrite(a4,a[7+i][3]);
    digitalWrite(a5,a[7+i][4]);
    digitalWrite(a6,a[7+i][5]);
    digitalWrite(a7,a[7+i][6]);
    digitalWrite(a8,a[7+i][7]);
    vTaskDelay(2/portTICK_PERIOD_MS);

  }
}
void frameswitcher(void *parameter)
{
  while(1)
  {
  i=8;
  vTaskDelay(500/portTICK_PERIOD_MS);
  i=16;
  vTaskDelay(500/portTICK_PERIOD_MS);
  i=24;
  vTaskDelay(500/portTICK_PERIOD_MS);
  i=0;
  vTaskDelay(500/portTICK_PERIOD_MS);
  }
}
void setup()
{
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  pinMode(a5,OUTPUT);
  pinMode(a6,OUTPUT);
  pinMode(a7,OUTPUT);
  pinMode(a8,OUTPUT);
  pinMode(b1,OUTPUT);
  pinMode(b2,OUTPUT);
  pinMode(b3,OUTPUT);
  pinMode(b4,OUTPUT);
  pinMode(b5,OUTPUT);
  pinMode(b6,OUTPUT);
  pinMode(b7,OUTPUT);
  pinMode(b8,OUTPUT);
  xTaskCreatePinnedToCore(toggleLED_1,"Toggle 1",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_2,"Toggle 2",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_3,"Toggle 3",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_4,"Toggle 4",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_5,"Toggle 5",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_6,"Toggle 6",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_7,"Toggle 7",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggleLED_8,"Toggle 8",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(frameswitcher,"Frame Switcher",1024,NULL,1,NULL,app_cpu);
}
void loop()
{
  
}

Code for the Blinking LED

C/C++
#if CONFIG_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else 
static const BaseType_t app_cpu = 1;
#endif

void toggle_LED_1(void *parameter)
{
  while(1)
  {
    digitalWrite(13,HIGH);
    vTaskDelay(500/portTICK_PERIOD_MS);
    digitalWrite(13,LOW);
    vTaskDelay(500/portTICK_PERIOD_MS);
  }
}

void toggle_LED_2(void *parameter)
{
  while(1)
  {
    digitalWrite(13,HIGH);
    vTaskDelay(300/portTICK_PERIOD_MS);
    digitalWrite(13,LOW);
    vTaskDelay(300/portTICK_PERIOD_MS);
  }
}
void setup() 
{
  pinMode(13,OUTPUT);
  xTaskCreatePinnedToCore(toggle_LED_1,"Toggle 1",1024,NULL,1,NULL,app_cpu);
  xTaskCreatePinnedToCore(toggle_LED_2,"Toggle 2",1024,NULL,1,NULL,app_cpu);
}

void loop() {

}

Credits

Electronics Club, CFI, IITM

Electronics Club, CFI, IITM

2 projects • 6 followers
We are a group of electronics enthusiasts whose mission is to explore the various realms of electronics and spread our knowledge.
Santhosh V me21b180

Santhosh V me21b180

2 projects • 2 followers
Thanks to Digikey.

Comments