Problems reading ADC1115 by interrupt

I had a similar problem described in topic ESP32 crash on reading ADS1115 on interrupt using an interrupt routine to take ADC1115 readings every time the ADC1115 triggers an interrupt.

My goal was to calculate RMS voltage from a 60HZ sine wave coming from a current sensor to calculate the current.

I used the suggestion gfvalvo provided as a guide where the interrupt would only signal an RTOS task at priority 7 that data is available.
The signaled task would read the available data and put it in an RTOS queue.
The main RTOS loop task at priority 1 would then read in what's in the queue and process it.

I found that when I had the ADC1115 switch MUX in the main RTOS loop task, once in a while the ADC1115 seemed to ignore the change of inputs. I even modified the Adafruit library for the ADC1115 to read back the MUX setting from the ADC1115 right after switching inputs and it often came back not a match. According to a post in TI forum, worst that should happen when switching MUX while in continuous mode is that you might get one extra sample from the previous input setting after having switched to the new input setting but issue seems worse than that.

My fix, or workaround was to forgo the queue, put all the code processing the current based on the RMS voltage calculation all in the task that the interrupt was signaling. As long as I set the SPS to no higher than 475SPS, even the mean and root calculation would fit between interrupts that trigger every 2.1mS (1/475).

Any ideas why when I had the MUX switch inputs in a different task than the task that read the data from the ADC, the MUX wasn't switching?

Another problem I came up with was that when I first powered up my circuit, I was getting no readings, though no errors starting the ADC1115 instance using the Adafruit library, but would be fine if I then had the ESP32 reboot without removing power.
I figured it might be a timing issue between when the ADC was ready to operate to when the ESP32 tried to use it but I couldn't find a way to mitigate that problem such as delaying 1ms before and after beginning the ADC object.
Any ideas about that?

PS. I had to increase the stack size of the RTOS tasks by 10x from what gfvalvo had in his example, otherwise I got a stack overrun kernel panic.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.