The signal is probably "bouncing" between HIGH and LOW instead of performing a clean edge, just like it happens with buttons.
Two simple solutions:
- Hardware solution: use a schmidt-trigger instead of a comparator
- Software solution: define a dead-time for the signal aquisition (disable the interrupt when the desired edge has been detected and enable it again after "dead-time")
esp8266 - I am facing a problem of firing multiple interrupts on nodemcu 1.0 on single rising edge pulse.what is problem and what are the solutions? - Arduino Stack Exchange
Multiple external interrupt with same ISR
Need some help with interrupts and timers
wifi - ESP8266 and interrupts - not possible? - Arduino Stack Exchange
Videos
Hi guys, i've been working on this for a couple of days now and my head hurts...
I am trying to do something like this:
-
Pin interrupt turns on LED
-
A 100ms timer starts while the main loop keeps running
-
When the time is up, an interrupt turns the LED off exactly after 100ms.
The impulses come at random, sometimes 2 or more in less than 1ms.
I've tried adding the turn off command inside the loop but depending on where the program is, it never turns off afterthe same amount of time.
I know that there is an option with Timer1 but i just can't get it to work...
Yes possible, but the ESP8266 only has one core and one set of peripherals. WiFi uses time, interrupts, and even the ADC peripheral, and maybe more.
This is why anything timing-critical, or time-intensive, is a pain to get to work properly on an ESP8266. It depends one what you are trying to do, of course, and also on if you can unload what you want to a peripheral the WiFi doesn't use, or if switching off WiFi is an option during parts of your code.
Generally, for applications such as yours, I would get an ESP32 or an Arduino Nano 33 IoT or any other solution that has a core for handling WiFi, and a core for you to use without restrictions. It makes development a more pleasant experience.
If, for some reason, you are bound to using an ESP8266, you would have to be more specific about what your eventual goal is; maybe a workaround can be found.
I found the solution on another forum.
You need to add the ICACHE_RAM_ATTR attribute to the interrupt so it stays in cache.
void ICACHE_RAM_ATTR CLK_ISR() {
ClkCount++;
}
Now:
Freq is: 332596 - connected for 680s, operating at 160MHz
That's all I tested to, but 330kHz is a bit better than crashing at 7kHz, lol!
edit: I've discovered that it now crashes after exactly 1200s (20mins), repetitively.
I'm still investigating why, but I believe that it's some kind of watchdog timer.