There are two timers in ESP8266 Timer0 and Timer1, one timer is used by its WiFi functions. We get only one timer to work. To avoid crash issues I recommend use of Ticker instead of Timer. Ticker performs same function as timer.
In this tutorial we will see both Timer and Ticker examples
ESP8266 Ticker Example
Ticker is library for calling functions repeatedly with a certain period. Ticker is os_timerEach Ticker calls one function. You can have as many Tickers as you like, memory being the only limitation. A function may be attached to a ticker and detached from the ticker. There are two variants of the attach function: attach and attach_ms. The first one takes period in seconds, the second one in milliseconds.
LED Blinking using ESP8266 Ticker
This program demonstrates LED blinking ticker example. This function starts timers similar to attach interrupt blinker.attach(0.5, changeState); to stop timer use blinker.detach();
In this tutorial we will see both Timer and Ticker examples
ESP8266 Ticker Example
Ticker is library for calling functions repeatedly with a certain period. Ticker is os_timer Each Ticker calls one function. You can have as many Tickers as you like, memory being the only limitation. A function may be attached to a ticker and detached from the ticker. There are two variants of the attach function: attach and attach_ms. The first one takes period in seconds, the second one in milliseconds.
LED Blinking using ESP8266 Ticker
This program demonstrates LED blinking ticker example. This function starts timers similar to attach interrupt blinker.attach(0.5, changeState); to stop timer use blinker.detach();
To use Ticker os_timer we need Ticker.h Timer Library
/*
Ticker ESP8266
Hardware: NodeMCU
Circuits4you.com
2018
LED Blinking using Ticker
*/
#include <ESP8266WiFi.h>
#include <Ticker.h> //Ticker Library
Ticker blinker;
#define LED 2 //On board LED
//=======================================================================
void changeState()
{
digitalWrite(LED, !(digitalRead(LED))); //Invert Current State of LED
}
//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(115200);
Serial.println("");
pinMode(LED,OUTPUT);
//Initialize Ticker every 0.5s
blinker.attach(0.5, changeState); //Use <strong>attach_ms</strong> if you need time in ms
}
//=======================================================================
// MAIN LOOP
//=======================================================================
void loop()
{
}
//=======================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
Ticker ESP8266
Hardware: NodeMCU
Circuits4you.com
2018
LED Blinking using Ticker
*/
#include <ESP8266WiFi.h>
#include <Ticker.h> //Ticker Library
Ticker blinker;
#define LED 2 //On board LED
//=======================================================================
void changeState()
{
digitalWrite(LED, !(digitalRead(LED))); //Invert Current State of LED
}
//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(115200);
Serial.println("");
pinMode(LED,OUTPUT);
//Initialize Ticker every 0.5s
blinker.attach(0.5, changeState); //Use <strong>attach_ms</strong> if you need time in ms
}
//=======================================================================
// MAIN LOOP
//=======================================================================
void loop()
{
}
//=======================================================================
Upload the program and see LED starts blinking at every 0.5 seconds.
ESP8266 Timer Example
Hardware Timer0 is used by WiFi Functions. We can use only Timer1. Use of timer instead of Ticker gives advantage of precision timing and You can get timer interrupt in micro seconds.
/*
ESP8266 Timer Example
Hardware: NodeMCU
Circuits4you.com
2018
LED Blinking using Timer
*/
#include <ESP8266WiFi.h>
#include <Ticker.h>
Ticker blinker;
#define LED 2 //On board LED
//=======================================================================
void ICACHE_RAM_ATTR onTimerISR(){
digitalWrite(LED,!(digitalRead(LED))); //Toggle LED Pin
timer1_write(600000);//12us
}
//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(115200);
Serial.println("");
pinMode(LED,OUTPUT);
//Initialize Ticker every 0.5s
timer1_attachInterrupt(onTimerISR);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
timer1_write(600000); //120000 us
}
//=======================================================================
// MAIN LOOP
//=======================================================================
void loop()
{
}
//=======================================================================
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
ESP8266 Timer Example
Hardware: NodeMCU
Circuits4you.com
2018
LED Blinking using Timer
*/
#include <ESP8266WiFi.h>
#include <Ticker.h>
Ticker blinker;
#define LED 2 //On board LED
//=======================================================================
void ICACHE_RAM_ATTR onTimerISR(){
digitalWrite(LED,!(digitalRead(LED))); //Toggle LED Pin
timer1_write(600000);//12us
}
//=======================================================================
// Setup
//=======================================================================
void setup()
{
Serial.begin(115200);
Serial.println("");
pinMode(LED,OUTPUT);
//Initialize Ticker every 0.5s
timer1_attachInterrupt(onTimerISR);
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_SINGLE);
timer1_write(600000); //120000 us
}
//=======================================================================
// MAIN LOOP
//=======================================================================
void loop()
{
}
//=======================================================================
Upload program and observe LED Blinking.