• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Problem using interrupts with ESP32 in light sleep mode

Q&A Forum › Category: ESP32 › Problem using interrupts with ESP32 in light sleep mode
0 Vote Up Vote Down
Ron asked 5 years ago
  1. void IRAM_ATTR rainTip() {  //tip is 30ms wide LOW pulse   
  2.   portENTER_CRITICAL(&mux);  
  3.    ++tips;
  4.   rtc_gpio_deinit(GPIO_NUM_13);  
  5.   portEXIT_CRITICAL(&mux);    
  6. }
  7.      
  8. void setup (void)        
  9. {  
  10.       Serial.begin(115200);  
  11.       delay (500);      
  12.       pinMode(rcvTip, INPUT);              
  13.       attachInterrupt (digitalPinToInterrupt(rcvTip), rainTip, FALLING);      
  14.    
  15.       gpio_wakeup_enable((GPIO_NUM_13),GPIO_INTR_LOW_LEVEL);   //Low on GPIO_13 allows interrupt
  16.       esp_sleep_enable_gpio_wakeup();                                         
  17. }
  18.  
  19. void loop() {            
  20.        Serial.println(“Total tips = ” + String(tips));
  21.        delay(100);    
  22.        esp_sleep_enable_timer_wakeup(10*1e6);  //
  23.        esp_light_sleep_start();  //Cannot use deep_sleep with GPIO_interrupt API per Espressif
  24.        Serial.println(“End of Loop”);        
  25. }

After a bucket tip, I get multiple interrupts (erratically) instead of just one per bucket tip. I have checked interrupt signal with ‘scope and only see one interrupt pulse. 
I suspect the gpio_wakeup command is not used properly.

Question Tags: A single interrupt results in multiple loops through the ISR
9 Answers
0 Vote Up Vote Down
Ron answered 5 years ago

I changed the portEnter/Exit statements in the ISR to portENTER_CRITICAL_ISR(&mux) ;and
portEXIT_CRITICAL_ISR(&mux);  These changes helped. It is more stable. Now I get exactly two additional delayed interrupts for a total of three. However, should only see one interrupt.
Must be additional problems in the software.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Ron.
Have you tried using another GPIO?

0 Vote Up Vote Down
Ron answered 5 years ago

Thanks for input, Sara. Changing the ISR statements really helped. Now, I have a basic logic error in the program. The interrupt is correctly logged and then the program incorrectly goes through the Loop twice more, each time adding one more bogus tip. I need to fix this and should be able to do it.

0 Vote Up Vote Down
Ron answered 5 years ago

Basic question is how does “gpio_wakeup_enable” command interact with ESP32 that is in timer sleep mode?

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Ron.

Accordingly to the documentation, it seems that you’re using GPIO wake up properly: https://github.com/espressif/esp-idf/blob/master/docs/en/api-reference/system/sleep_modes.rst#gpio-wakeup-light-sleep-only

Have you tried putting all you code that goes in the loop into the setup() and see what happens? Like putting your timer wake up in the setup()?

I’m not very familiar with light sleep, so I’m not sure if there’s something that is interfering with the interrupt.

When it comes to the timer wake up I’m not sure if it resets or if it continues “counting” the time for the next wake up.

Regards,
Sara

0 Vote Up Vote Down
Ron answered 5 years ago

I will try putting all of the code in setup. This is required for Deep Sleep but not for Light sleep. However, it is worth a tryt.
There must be a standard way of dealing with interrupts while an esp32 is in sleep mode. This seems like a common problem.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi.
I know that for light sleep you can put code in the loop. When the ESP32 wakes up, it goes back to where it left in the code. However, I don’t know what happens if you put everything in the setup(). Does it still detect interrupts? It should because they are in the setup.

I haven’t experimented with interrupts while in light sleep mode, so I don’t know what might be the problem.
I’m sorry that I can’t help much.

If you find out something, please share.

Regards,
Sara

0 Vote Up Vote Down
Ron answered 5 years ago

Sara, the ULP cpu on the esp32 looks perfect for my application. It can record bucket tips while the esp32 is in deep sleep. No need for interrupts! 
Maybe Rui can do a tutorial on the ULP sometime. It looks pretty difficult for me to use but will try. I can think of other applications for it.

0 Vote Up Vote Down
Sara Santos Staff answered 5 years ago

Hi Ron.
Yes, it seems a bit difficult. I guess that’s why we haven’t taken the time to take a look at it yet.
But that’s definitely something we need to investigate and write about.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Learn Raspberry Pi Pico/Pico W with MicroPython eBook – Version 1.2 May 26, 2025
  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.