[image] 6v6gt: The OP's example looks close enough to the one here for the ESP32 Arduino 3.x core. I am using ESP32 core 3.1.1 think the problem is timerbegin() initializes the timer and starts it the following timerstart() attempts to start the timer again and throws the exception E (20) … Answer from horace on forum.arduino.cc
🌐
Espressif
docs.espressif.com › projects › arduino-esp32 › en › latest › api › timer.html
Timer - - — Arduino ESP32 latest documentation
They are all 64-bit (54-bit for ESP32-C3) generic timers based on 16-bit pre-scalers and 64-bit (54-bit for ESP32-C3) up / down counters which are capable of being auto-reloaded. This function is used to configure the timer. After successful setup the timer will automatically start. hw_timer_t ...
🌐
Espressif Docs
espressif-docs.readthedocs-hosted.com › projects › arduino-esp32 › en › latest › api › timer.html
Timer — Arduino-ESP32 2.0.14 documentation
#include "esp_system.h" #include "rom/ets_sys.h" const int button = 0; //gpio to use to trigger delay const int wdtTimeout = 3000; //time in ms to trigger the watchdog hw_timer_t * timer = NULL; void ARDUINO_ISR_ATTR resetModule() { ets_printf("reboot\n"); esp_restart(); } void setup() { Serial.begin(115200); Serial.println(); Serial.println("running setup"); pinMode(button, INPUT_PULLUP); //init control pin timer = timerBegin(1000000); //timer 1Mhz resolution timerAttachInterrupt(timer, &resetModule); //attach callback timerAlarm(timer, wdtTimeout * 1000, false, 0); //set time in us } void lo
Discussions

Hardware Timer ESP32 Dev Module
Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board · In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category ... More on forum.arduino.cc
🌐 forum.arduino.cc
13
0
July 19, 2024
Hardware timer, how to run once and restart again?
I had some trouble with timers and alarms on my esp32 doit devboard. Spent many hours trying to debug, end of the days its a silicon issue with the REV0 chips. Not sure if this applies to you but more information can be found here: https://github.com/espressif/arduino-esp32/issues/1313 More on reddit.com
🌐 r/esp32
8
5
April 12, 2021
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Programming timer interruption - Programming - Arduino Forum
February 6, 2025 - I'm posting here a simple project ... make this code, I hope it can be useful to someone ! This code creates an interrupt every 100ms and counts the number of ......
🌐
ESP32 Forum
esp32.com › viewtopic.php
Converting 2.0.17 Timer code to 3.0+ - ESP32 Forum
This is what I currently have: ``` askRequestTimer = timerBegin(80000000); // 80MHz timerAttachInterrupt(askRequestTimer, &onAskReqTimer); timerAlarm(askRequestTimer, 5 * 1000000, true, 0); //send out an ask every 5 secs askExpireTimer = timerBegin(80000000); timerAttachInterrupt(askExpireTimer, &onAskExpireTimer); timerAlarm(askExpireTimer, expireLength * 1000000, true, 0); timerStop(askExpireTimer); ``` I'm wondering if this is equivalent?
🌐
DFRobot Wiki
wiki.dfrobot.com › esp32 series development guide › esp32 arduino general tutorial - chapter 3
ESP32 Arduino General Tutorial - Chapter 3: Timers & Sleep | DFRobot Wiki
January 15, 2026 - They can help the system perform tasks periodically, conduct timeout detection, measure event intervals, and so on. hw_timer_t * timerBegin(uint32_t frequency); Function: Set up and start a timer Parameters: frequency: Timer frequency (in Hz) ...
🌐
ElectronicWings
electronicwings.com › esp32 › esp32-timer-interrupts
ESP32 Timer Interrupts | ESP32
Step1: initialize timerBegin() Function. ... This function returns a pointer to a structure of type hw_timer_t, which we will define as the timer global variable. We pass three values to this function, the first one is the timer to use.
🌐
GitHub
gist.github.com › futureshocked › 5327bec254a9d5afcc91fa0b673442b6
ESP32 timer interrupts, how to change duration in the loop. · GitHub
Essa é a maneira recomendada de lidar com interrupções, especialmente em plataformas sensíveis ao tempo como o ESP32. Isso evita problemas com watchdog timers e garante que a ISR seja o mais rápida e leve possível.
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › projects › programming
Hardware Timer ESP32 Dev Module - Programming - Arduino Forum
July 19, 2024 - #include #include hw_timer_t *timer ... { pinMode(ledPin, OUTPUT); // Initialize timer 0 with a frequency of 1 Hz timer = timerBegin(0, 1); // Attach an ISR function to the timer timerAttachInterrupt(timer, &onTimer, true); ...
🌐
DeepBlue
deepbluembedded.com › home › blog › esp32 timers & timer interrupt tutorial (arduino ide)
ESP32 Timers & Timer Interrupt Tutorial (Arduino IDE) – DeepBlueMbedded
February 17, 2025 - This function is used to change the prescaler (clock divider) value of the specified timer module. The initial prescaler value is usually specified when you first initialize the timer with timerBegin() function.
🌐
Upsy
upesy.com › tutorials › esp32 › esp32 programming › arduino code › basics › timers
Timer ESP32 with Arduino Code: Master the time
February 2, 2023 - The function timerBegin(uint8_t id, uint16_t prescaler, bool countUp) allows to configure the timer : The ESP32 has 4 independent timers, selected by an id between 0 and 3. Then we select the prescaler to apply to the timer clock signal.
🌐
Luis Llamas
luisllamas.es › inicio › tutoriales arduino › curso esp8266 / esp32
How to use ESP32 timers
November 25, 2024 - 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.
🌐
Reddit
reddit.com › r/esp32 › hardware timer, how to run once and restart again?
r/esp32 on Reddit: Hardware timer, how to run once and restart again?
April 12, 2021 -

Guys

I have a DevKitC dev board, and I'm trying to set up a HW timer that I want to re-use under certain conditions. In other words, I don't want it to reload automatically, it should run once and then remain dormant until I enable it again. It seems to work the first time, but when I enable the timer a second time it fires immediately, without any delay. It seems I'm not resetting it properly, so that the counter starts from 0 again (?).

What I've tried is this:

initialize timer 0 in setup(), with a prescaler for 80MHz.

myTimer = timerBegin (0, 80, true);
timerAttachInterrupt (myTimer, &isrMyTimer, true);

then when I want to use the timer I enable it as follows (for a duration of 3 seconds):

timerAlarmWrite(myTimer, 3000000), false);
timerAlarmEnable(myTimer);

I am also trying to confirm the frequency to use for the timer, whether it is indeed 80MHz. I get the values as shown below for the different functions. Any thoughts on what they mean?

getCpuFrequencyMhz()		240	
getXtalFrequencyMhz()		40
getApbFrequency()		80000000
Top answer
1 of 4
2
I had some trouble with timers and alarms on my esp32 doit devboard. Spent many hours trying to debug, end of the days its a silicon issue with the REV0 chips. Not sure if this applies to you but more information can be found here: https://github.com/espressif/arduino-esp32/issues/1313
2 of 4
2
I managed to get it working. The trick that worked for me was to reset the timer (counter) before each use, with the following statement: timerRestart() Below is a little pseudo sketch, only showing the relevant statements, to give you or anyone finding this post later, an idea of what worked for me. #include // only needed to get ESP32 frequency detail hw_timer_t * tmrTEST = NULL; // timer used for TESTING unsigned long lngTimerStart = 0; // system micro-seconds at timer start int tmrDuration = 5; // run timer for 5 seconds // Interrupt Service Routine called when TEST timer fires. void IRAM_ATTR isrTimerTEST() { Serial.print(" >>>>> TEST TIMER ISR: "); Serial.print( micros() ); Serial.print(" diff="); Serial.println((micros()-lngTimerStart)/1000/1000); } void setup() { Serial.begin(115200); ... // Set up TEST timer.. tmrTEST = timerBegin (1, 80, true); // use ESP32 Timer 1, pre-scale 80 (for 80MHz freq), count up. timerAttachInterrupt (tmrTEST, &isrTimerTEST, true); // attach the function to call when timer interrupt fires. Edge. Serial.print(" >> CPU Freq: "); Serial.println(getCpuFrequencyMhz()); Serial.print(" >> XTL Freq: "); Serial.println(getXtalFrequencyMhz()); Serial.print(" >> APB Freq: "); Serial.println(getApbFrequency()); ... } void loop() { ... if (tmrDuration > 0) { // initiate TEST timer. duration in micro-seconds. False to run once. timerAlarmWrite(tmrTEST, (tmrDuration * 1000000), false); timerRestart(tmrTEST); // reset timer counter timerAlarmEnable(tmrTEST); // start the timer lngTimerStart = micros(); Serial.print(" >>>>> TimerTEST dur="); Serial.print(tmrDuration); Serial.print(" start: "); Serial.println( lngTimerStart ); } ... } Thanks for your replies and time !
🌐
Google Translate
translate.google.com › translate
ESP32 Timers & Timer Interrupt Tutorial
August 5, 2025 - Timer Configuration: ESP32 timerBegin configures the timer values
🌐
Refcircuit
refcircuit.com › home › articles › embedded programming › espressif esp32 soc › 🕰 simple timer interrupt esp32-s3 arduino work example
🕰 Simple Timer Interrupt ESP32-S3 Arduino Work Example — RefCircuit
August 5, 2024 - Introduction Let's see how to use hardware timer interrupt (RTOS not used) from the timer with real tests on the devboard and measurements using logic analyzer to confirm correctness of the code.
🌐
ESP32 Forum
esp32.com › viewtopic.php
API Timer , Boards Version 3.0.1 - ESP32 Forum
June 10, 2024 - // Set timer frequency to 1KHz timer = timerBegin(1000); // Attach onTimer function to our timer. timerAttachInterrupt(timer, &onTimer); // Set alarm to call onTimer function every second (value in microseconds). // Repeat the alarm (third parameter) with unlimited count = 0 (fourth parameter).
🌐
ESP32 Forum
esp32.com › viewtopic.php
Timers and interrupts not working as expected - ESP32 Forum
January 12, 2024 - hw_timer_t * timer = NULL; void IRAM_ATTR onTimer() { bool state = digitalRead(5); digitalWrite(5, !state); } void setup() { pinMode(5, OUTPUT); Serial.begin(115200); timer = timerBegin(0, 80, true); // Timer 0, prescaler 80, count up timerAttachInterrupt(timer, &onTimer, true); timerAlarm...