ESP32 Interrupt stops working while loop() code executes

WilFerg
Posts: 3
Joined: Fri Feb 06, 2026 9:23 am

ESP32 Interrupt stops working while loop() code executes

Postby WilFerg » Fri Feb 06, 2026 10:25 am

I use a ESP32 WROOM Devkit board using Arduino. The ESP32 is connected to a DS3231 RTC that interrupts the ESP once per second. However, if any procedure is called from the loop(), then the interrupt stops until the procedure has been completed. I am not sure if this is a coding problem or whether task priorities have not been defined sufficiently. For test purposes, I toggle a LED each time when the ISR is called. While the loop is executed, the LED flashes at the expected rate. No interrupt disable code is coded anywhere. However, as an example, when executing the kbGetMonth() procedure below, the LED stops blinking while that code is running, and resumes blinking when returning to the loop(). I am sure the presence of the LED code within the ISR does not create issues in itself. If I remove the LED code and print the seconds of the clock from within the loop(), the clock stops while executing any of the options within the switch{}. Code size is around 700 lines but here are some code snippets:

Code: Select all

#define Timer_int_PIN 13       // GPIO13

volatile bool timer_int = false;    // DS3231 Timer 1 Hz interrupt signalling flag
volatile int seconds_since_interrupt = 0;
volatile bool LED_state;

void IRAM_ATTR isr_Timer1() {          // This interrupt fires at a 1Hz rate
    timer_int = true;				    // The IRAM_ATR neither causes or resolves the problem.
    seconds_since_interrupt++;
    LED_state = !LED_state;
    digitalWrite(LED,LED_state);        
    return;
    
setup() {
  attachInterrupt(Timer_int_PIN, isr_Timer1, FALLING);
}

void loop() {		// Top section maintains an internal 24h clock
    if (timer_int) {    // If RTC 1Hz clock interrupt has been triggered then do timer service
        DS3231RTC.checkIfAlarm(1);  // Clear Alarm 1 flag in the RTC chip
        timer_int = false;          // reset timer interrupt flag
        seconds_since_interrupt = 0;
        if (tminfo.tm_sec > 59) {         // each minute  
          tminfo.tm_sec = tminfo.tm_sec - 60; // in case the delay from interrupt to this code was > 1 sec and < 60 sec
          tminfo.tm_min++;              
          if (tminfo.tm_min > 59) {     // each hour
            tminfo.tm_min = 0;
            tminfo.tm_hour++;
            if (tminfo.tm_hour > 23) {  // each day at midnight 
              tminfo.tm_hour = 0;       // Run a local 24h clock, updated from the RTC once a day
              get_RTC_time();           // i.e. the  RTC is only called once a day at midnight to update the time
            }      
          }
    }  
    }  // end of timer interrupt service
    if(BTPort.available()) {			// BTPort is a serial connection over Bluetooth classic.
      c = BTPort.read(); 
      switch (c) {
//    several other cases..... Here is one:
        case 'm': kbGetMonth();		// Get month and year from BT terminal
                  extractMonthData();	// extract data from SD card
                  break;   
      } // switch   
    }  // if BTPort
} // loop
I have done extensive searches and reading around ESP32 interrupts, but I have not come across any useful knowledge. If anyone as any suggestion on how to fix the interrupt, I would be extremely thankful.
Thank you for your time, will

WilFerg
Posts: 3
Joined: Fri Feb 06, 2026 9:23 am

Re: ESP32 Interrupt stops working while loop() code executes

Postby WilFerg » Fri Feb 06, 2026 11:13 am

In the called function, the controller asks for keyboard input to specify the month and year for which data are extracted.
I am fairly certain it is the serial input (a virtual keyboard) that is not interrupted. The input is dependent on:
on BTPort,available() which looks like it cannot be interrupted while (available() is false) and therefore waiting for input.

lbernstone
Posts: 1131
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 Interrupt stops working while loop() code executes

Postby lbernstone » Fri Feb 06, 2026 4:15 pm

If you want the code to be executing immediately after the ISR is triggered, make it asynchronous to loop() by adding another task that is dedicated to servicing it at a higher priority than loop.
Honestly, if you just want it to update from the RTC once a day (or however frequently), I would use a Ticker to directly call that instead of doing interruptions every minute.

User avatar
Basalt
Posts: 29
Joined: Wed Aug 16, 2023 7:59 pm

Re: ESP32 Interrupt stops working while loop() code executes

Postby Basalt » Fri Feb 06, 2026 10:31 pm

Additionally to lbernstone's recommendation:
I see SD Card being used, maybe reading/writing this blocks your timer interrupt

Who is online

Users browsing this forum: No registered users and 2 guests