Hello, I'm Ganesh. I'm building git-lrc, an AI code reviewer that runs on every commit. It is free, unlimited, and source-available on Github. Star Us to help devs discover the project. Do give it a try and share your feedback for improving the product.
In this article, We will be exploring inbult task function in ESP8266.
Core Logic of Task Scheduler
I want to perform two operations in ESP8266.
- Blink LED every 2.5 seconds 1 time.
- Blink LED every 5 seconds 2 times.
Let's see how to do this without using inbuilt task scheduler.
- Setup Serial communication and LED pin.
- Start Loop.
- Check if 2.5 seconds have passed since the last task activation.
- If yes, perform the blink.
- Check if 5 seconds have passed since the last task activation.
- If yes, perform the blink
#include <Arduino.h>
unsigned long t25, t50;
/**
* Performs a single blink on the built-in LED (Active Low).
* @param message Debug message to output to Serial.
*/
void performBlink(const char *message) {
Serial.println(message);
digitalWrite(LED_BUILTIN, 0); // LED ON
delay(80);
digitalWrite(LED_BUILTIN, 1); // LED OFF
delay(80);
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1); // Start with LED OFF
}
void loop() {
// Task 1: Every 2.5s (triggers at 2.5s, 5.0s, 7.5s...)
if (millis() - t25 >= 2500) {
t25 = millis();
performBlink("[2.5s] π‘");
}
// Task 2: Every 5.0s (triggers at 5.0s, 10.0s...)
// At 5s, both tasks trigger, resulting in a double blink.
if (millis() - t50 >= 5000) {
t50 = millis();
performBlink("[5.0s] π‘");
}
}
Output:
[2.5s] π‘
[5.0s] π‘
[2.5s] π‘
[2.5s] π‘
[5.0s] π‘
See as we defined to use 2.5 sec interval for first task and 5 sec interval for second task.
This also mean 2nd time 2.5 sec blink will happen after 5 sec from 1st blink.
This will create 2 times blink in 5 sec.
Using Task Scheduler
We are going to use task scheduler library to perform multiple operations in specific intervals.
Let's see how to do this using task scheduler library.
- Include task scheduler library.
- Create a scheduler object.
- Define tasks.
- Add tasks to the scheduler.
- Enable tasks.
- Run the scheduler.
#include <Arduino.h>
#include <TaskScheduler.h>
Scheduler runner;
/**
* Performs a single blink pulse.
* Note: NodeMCU V2 built-in LED is active LOW.
*/
void pulse() {
digitalWrite(LED_BUILTIN, 0); // ON
delay(80);
digitalWrite(LED_BUILTIN, 1); // OFF
delay(80);
}
void Callback(const char *msg) {
Serial.println(msg);
pulse();
}
// Task Definitions
Task t25(2500, TASK_FOREVER, []() { Callback("[2.5s] π‘"); });
Task t50(5000, TASK_FOREVER, []() { Callback("[5.0s] π‘"); });
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1); // Start OFF
runner.init();
// Add tasks to the scheduler
runner.addTask(t25);
runner.addTask(t50);
// Enable tasks
t25.enable();
t50.enable();
}
void loop() { runner.execute(); }
By this way we can perform multiple operations in specific intervals without blocking the loop function.
Output:
[5.0s] π‘
[2.5s] π‘
[5.0s] π‘
[2.5s] π‘
[2.5s] π‘
You can also see 5.0s blink is happening first and sometimes 2.5s blink is happening first.
This is because of the task scheduler.
Conclusion
By this we understood how task scheduler works in ESP8266.
And we actualy used task scheduler to perform multiple operations in specific intervals without blocking the loop function.
Any feedback or contributors are welcome! Itβs online, source-available, and ready for anyone to use.
β Star it on GitHub: https://github.com/HexmosTech/git-lrc

Top comments (0)