analogRead() has strange noise on ESP32-S2 and no error checking for use of adc_oneshot_read()
Board
Unexpected Maker FeatherS2
Device Description
Unexpected Maker FeatherS2 using pin 5 (AKA A5 and IO5 in ESP terminology and based on the pinout it's ADC1 CH4)
Hardware Configuration
Pin 5 is measuring the voltage from an MCP4728 DAC. There's also a TI ADS1115 in there.
Version
v3.3.11
Type
Bug
IDE Name
Arduino IDE 2.3.10
Operating System
N/A
Flash frequency
Don't know.
PSRAM enabled
yes
Upload speed
Don't know
Description
I've been doing some ADC testing recently which includes noise analysis. When I looked at the results I noticed there was a very considerable difference between a CircuitPython program running and an Arduino sketch on a UM FeatherS2 board despite the code doing more-or-less the same thing. I have the width set to 12bits and I'm using analogRead() to keep things simpler.
I would have assumed this was just a (very) noisy ADC if I hadn't looked closely and hadn't had the CircuitPython comparison available. I started discussing this in the Adafruit Forums. I haven't looked hard but there are at least two differences I've spotted so far in the different implementations.
Averaging
The CircuitPython code takes the average of two one-shot ADC values. I struggle to see how this could offer as much noise improvement as it does over the Arduino values, though. I don't think this is a full explanation for the parlous Arduino situation.
for (int i = 0; i < NO_OF_SAMPLES; i++) {
int raw;
ret = adc_oneshot_read(adc_handle, channel, &raw);
if (ret != ESP_OK) {
continue;
}
adc_reading += raw;
sample_count += 1;
}Error checking
The Arduino code has no error checking for the execution of adc_oneshot_read.
The CircuitPython code is quite different here because it's taking multiple values (2) and only using the successful values, so it could get 2, or 1, or 0 error-free values. That certainly gives it a chance at recovery from a single occasional error.
Other things to investigate
- There may be a difference between how CircuitPython and the Arduino program send data over UART and do ADC sampling. Is it likely that concurrent
UART0ArduinoSerial1(UART1) activity during ADC sampling on an ESP32-S2 could cause this? I'm going to make a version which makes these activities sequential to rule this out. - CircuitPython has the GPIO by default with a weak pull-up (I'm guessing 48k). This gets disabled as soon as one starts using its
AnalogInobject. Perhaps there's some subtle GPIO difference going on here.
Sketch
https://github.com/kevinjwalters/arduino-examples/blob/master/uno/ana-dig-reader/ana-dig-reader.inoDebug Message
N/AOther Steps to Reproduce
Put 100mV on an analogue capable pin and take lots of measurements and see what the distribution looks like.
I have checked existing issues, online documentation and the Troubleshooting Guide
- I confirm I have checked existing issues, online documentation and Troubleshooting guide.
Source: espressif/arduino-esp32