ESP-IDF : Using timers in ESP32
|Timers are very important peripherals in embedded systems. If your application depends on precise timing, then timers are only thing that are up for that job.
ESP32 provides four timers in two groups. All the timers are 64 bit each with 16 bit prescaler.
ESP32 timer API features :
- configure as up-counter or down-counter
- configure a match value (alarm value) , we can provide ISR function which will be executed on match
- initial value for counter can be set
- can be configured to auto reload the counter value on match
What are we doing here
We are going to user a timer to generate accurate 500Hz square wave with 50% duty cycle. You can easily increase the frequency of the wave with out loosing precision (I am using this example to explain the precision of timers, if you just need a square wave you can simply use PWM API)
You can see in the above image that we have generated 500Hz square wave
Program:
Using the timers in ESP32 is very simple. first we need to configure the timer with the required settings, we will be using timer 0 in group 0
we need to pass timer group, timer id and configuration structure to the timer_init function. Then we can set the initial value for the counter using the following function
Match value(at which counter value, the interrupt will be triggered) can specified using timer_set_alarm_value
we have initialized the timer and we set the alarm value also , now we need to enable interrupts and specify the ISR
and finally start the timer
full code is available on this git repo
Is it possible to get micro second precision using timers in ESP32
yes it is possible to get microsecond precision using esp32 64 bit timers, note that..we execute the isr too frequently other modules may not get the processor time at all
i want to synchronise zero crossing, with timer. how i can do. can you support me.
Hi Raj,
yes, you can use ESP32 timers to sync with zero cross detectors. You need to hookup a zero cross detector to a interrupt pin of ESP32.
you can reference following blog for zero cross detector (you need to use 3v3 instead of 5v trigger)
http://icircuit.net/zero-cross-detector/273
Hey I wanted to Set the bits of event group from Timer Interrupt , is it possible ??
yes, you can set eventgroup bits from timer interrupt.
https://www.freertos.org/xEventGroupSetBitsFromISR.html
Hi, I need to perform a program, that reads an analog value with an analog digital converter every 2ms.
The read values should afterwards be saved on a microSD card and be published to the web (Google Cloud IOT Core).
A good time management for the 2 ms is very important, so I thought, that I want to create a timer for this purpose and run it in one separate task… Is it possible to specify, that the timerhandling is done by a specific task? Because then I could write the results to a queue and perform the handling of writing to the sd card and publishing to the web…
Do you think, this is the right approach or do you have any suggestions?
Many thank in advance.
Thomas Lederer
Hi, yes you can have a separate task taking the readings and use IPC to communicate with task that is writing to sd card. You might want to test if you can do 500 writes to the card in a second first
Hi,Your Post is a Great help,I am making a alarm functionality using esp32, so I will get the real time from the sntp and will set alarm using the TIMER API,
Thanks for the post.
Hi , gautam have you done ? using timer group example have you implemented the alarm . If so can you share your code