Page 1 of 1

ESP32 Interrupt jitter at 20kHz

Posted: Fri Mar 18, 2022 9:06 am
by Jose Silva
Hello everyone!

I would like your help to solve the following problem.

I'm using an ESP32 Dev Kit V1 connected as follows:
- Pin 4 (input) is connected to a 20kHz and 3.3V signal and to channel 1 of the oscilloscope; and
- Pin 2 (output) is connected to channel 2 of the oscilloscope.

My goal is to use interrupts and generate a signal on pin 2 (output) that follows the variations of the signal on pin 4 (input).

The image below illustrates two behaviors. The most frequent and the other one that happens occasionally.
In the first behavior, the latency is around 3 us, but sometimes there is a variation (jitter) and the rise of the output signal takes 15 us or even more to keep up with the input.
ESP32 Jitter.png
ESP32 Jitter.png (180.91 KiB) Viewed 2473 times
I would like to know how to remove this occasional behavior and keep the system stable.

The code that I'm using is below. To compile and upload I am using the latest Arduino IDE version 1.8.13.

I'm using GPIO.out_w1ts and GPIO.out_w1tc because I believe it will be faster than using digitalWrite(pin, state).

Also notice that I'm not reading the state of pin 4 (input) to be faster, because of this, sometimes the output signal gets inverted, but that's not a problem at this point.

Code: Select all

#define OUTPUT_PIN 2
#define INPUT_PIN 4

volatile bool state = false;

void IRAM_ATTR interruptFunction() {
    if (state)
        GPIO.out_w1ts = 0b100;
    else
        GPIO.out_w1tc = 0b100;
    state = !state;
}

void setup() {
    pinMode(INPUT_PIN, INPUT);
    attachInterrupt(digitalPinToInterrupt(INPUT_PIN), interruptFunction, CHANGE);
    gpio_config_t io_conf;
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pin_bit_mask = 0b100;
    gpio_config(&io_conf);
}

void loop() {
}
The settings used in the Arduino IDE are illustrated in the next image.
ESP32 IDE Settings.png
ESP32 IDE Settings.png (4.44 KiB) Viewed 2473 times
Thanks in advance for all the help.

Thank you very much,
Jose Silva.
:D

Re: ESP32 Interrupt jitter at 20kHz

Posted: Tue Jun 14, 2022 6:01 pm
by pidloop
I have a very similar situation. I am reading ADC3 with interrupt at 500 Hz (once every 2 ms). Normally it is very stable but roughly every 10-15 minutes there is a sudden change in the interrupt period about 1 ms that lasts for 10-20 ms then it disappears again. I can determine no external reason for this. This effects the FFT of my data drastically when it occurs and I would like to eliminate it. Thank you.

Re: ESP32 Interrupt jitter at 20kHz

Posted: Thu Jun 16, 2022 12:55 pm
by bobolink
One thing you might try is to dedicate an entire core to just collecting samples and then give them to the other core for processing. This method has latency due to collecting enough samples for a “frame” and doesn’t operate sample by sample. Which might be OK for an FFT.


https://github.com/bobh/ESP32AudioFramework