ESP8266 Timer and Ticker Example

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.

esp8266 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_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 attach_ms 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()
{
}
//=======================================================================

Upload program and observe LED Blinking.

 

4 thoughts on “ESP8266 Timer and Ticker Example

  1. @Kossowski

    i have been using the ESP8266 and ESP32 for over 5 years now and Ticker library for almost as long.

    your code is basically sound, however i see a few issues

    1– ESP SDK print can fail some times with negative numbers or more than 64 characters.
    I have seen similar when the count rolled over and tried to print it when the value went negative.
    possible fix
    if( interruptCounter < 0 ) interruptCounter = 0;

    then print

    2 — debounce the pin, a capacitor on the pin will also help about .1 to 1Uf
    the" if" and "digitalRead" will use a few clock cycles and act as a good bebouncer

    // ISR routine for button
    void handleInterrupt() {
    if ( digitalRead(buttonPin) == LOW ) // act as a good bebouncer
    {
    interruptCounter++;
    }
    }

    ALSO install the "ESP Exception Decoder"
    https://github.com/me-no-dev/EspExceptionDecoder

    when it crashes copy and paste the "BackTrace " into the decoder (under tools) and it will show where your code crashed.

    1. Thank You for replies. Actually I found that a code works properly with LoLin ESP-12E and doesn’t work with an other producer IC. (I don’t remember at the moment a name of the producer). So I am happy with that LoLin. Case closed.

  2. I have tried to use inerrupt handler with Ticker lib unsuccesfully.
    I wanted to count a number of interrupts on pin D3 in a certain period of time.
    Either ticker or timer prodused unstable behaviour.
    Some number of interrupts counted and some stack dump occured.
    Any comments on the following sketch?

    /*
    ESP8266 Timer Example
    Hardware: NodeMCU
    Circuits4you.com
    2018
    LED Blinking using Timer
    */
    //#include
    #include

    Ticker blinker;

    #define LED 2 //On board LED

    const byte interruptPin = D3;
    volatile byte interruptCounter = 0;
    int numberOfInterrupts = 0;

    //=======================================================================
    void ICACHE_RAM_ATTR onTimerISR(){
    digitalWrite(LED,!(digitalRead(LED))); //Toggle LED Pin
    timer1_write(600000);//12us
    }

    void handleInterrupt() {
    interruptCounter++;
    }

    //=====================================================================
    // Setup
    //=====================================================================
    void setup()
    {
    Serial.begin(115200);
    Serial.println(“”);

    pinMode(LED,OUTPUT);

    pinMode(interruptPin, INPUT_PULLUP);
    attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

    //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()
    {

    if(interruptCounter>0){

    interruptCounter–;
    numberOfInterrupts++;

    Serial.print(“An interrupt has occurred. Total: “);
    Serial.println(numberOfInterrupts);
    }

    }
    //=====================================================================

Leave a Reply