Language: EN

esp32-timers

How to use ESP32 timers

A Timer is a counter that can measure the elapsed time in system clock units. They allow running functions or interruptions at specific times or after a certain elapsed time.

These devices allow measuring time intervals and running tasks at precise moments, which is especially useful in applications that require synchronization, event control, or execution of periodic tasks.

However, it is important not to abuse them and 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, with a simple “blink without delay”.

Advantages and Applications of Timers

Some applications where you would 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 (such as motors or relays) at 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, which can be used for various applications. Depending on the ESP32 model, we will find 2 to 4 timers, but the usage is identical in 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 is reset after reaching the comparison value.

Attaching the Interrupt

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

Setting the Comparison Value

With timerAlarmWrite(), the value at which the Timer should count before generating an interruption or executing an action is established.

Enable the Timer

Finally, the Timer is enabled using timerAlarmEnable().

Code Example

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

hw_timer_t *timer = NULL;

volatiel void has_expired = false;
void IRAM_ATTR timerInterrupcion() {
 has_expired = true;
}

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

  timer = timerBegin(0, 80, true); // Timer 0, clock divider 80
  timerAttachInterrupt(timer, &timerInterrupcion, 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 triggered
    digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN)); // Toggle the state of the LED
    has_expired = false; 
  }
  
}

In this example, a Timer is configured to generate an interrupt every second. When the interrupt is triggered, the timerInterrupcion() function is executed, toggling the state of the built-in LED.