esp32-timers

How to use ESP32 timers

  • 3 min

A Timer is a counter that can measure elapsed time in system clock units. They allow executing functions or interrupts at specific moments or after a certain amount of time has passed.

These devices allow measuring time intervals and executing tasks at precise moments (which is especially useful in applications requiring synchronization, event control, or execution of periodic tasks).

However, it’s important not to overuse them, and to use them only in situations where time precision is crucial.

99% of the tasks where a timer is used could be done with a soft timer (a software timer), with a simple “blink without delay”.

Some applications where you might consider using Timers on the ESP32 include:

  • Signal Generation: Generate square wave signals, PWM (Pulse Width Modulation), or other periodic signals.
  • Actuator Control: Activate and deactivate actuators (like motors or relays) at very, very precise intervals.
  • Time Measurement: Measure the duration of events, such as the speed of an object or the time between pulses.
  • Synchronous Communication: Coordinate real-time communications between devices.

Using Timers in ESP32

The ESP32 offers several internal Timers that can be used for various applications. Depending on the ESP32 model, you will find 2 to 4 timers (but the usage is identical across all models).

To use Timers in the ESP32 under the Arduino environment, general steps are followed:

Timer Initialization

First, the desired Timer must be initialized. This is done using the timerBegin() function which takes as arguments the Timer number (0, 1, 2, etc.), the clock divider, and whether the Timer resets after reaching the comparison value.

Attach the Interrupt

The interrupt handler function is attached to the Timer using timerAttachInterrupt(). This function sets which function will be executed when the Timer reaches the comparison value.

Configure the Comparison Value

With timerAlarmWrite(), the value the Timer must count to before generating an interrupt or executing an action is set.

Enable the Timer

Finally, the Timer is enabled using timerAlarmEnable().

Code Example

Below is an example of how to use a Timer in the ESP32.

hw_timer_t *timer = NULL;

volatile void has_expired = false;
void IRAM_ATTR timerInterrupt() {
 has_expired = true;
}

void setup() {
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);

  timer = timerBegin(0, 80, true); // Timer 0, clock divisor 80
  timerAttachInterrupt(timer, &timerInterrupt, true); // Attach the interrupt handling function
  timerAlarmWrite(timer, 1000000, true); // Interrupt every 1 second
  timerAlarmEnable(timer); // Enable the alarm
}

void loop() {
  if(has_expired)
  {
     // Tasks to perform when the Timer interrupt is activated
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Toggle the state of the LED
    has_expired = false; 
  }
  
}
Copied!

In this example,

  • A Timer is configured to generate an interrupt every second.
  • When the interrupt is triggered, the timerInterrupt() function executes, toggling the state of the built-in LED.