Now it is working.
Well, sort of...
When I connect the pins to +3.3 V or GND I get full scale or zero for both ADCs.
But when I connect all pins to the same intermediate voltage, I get a big difference between both ADCs.
All pins belonging to the same ADC produce the same value.
Example: test voltage = 1.3 V (40% of Vcc obtained from 2 out of a series of 5 x 1 kohm).
Expected result: 511 * (1.3/3.3) = 201.
Results:
16:44:20.635 -> analogRead of pins...
16:44:20.635 -> Analog value pin 36 (ADC1_CH0) = 138
16:44:20.729 -> Analog value pin 39 (ADC1_CH3) = 138
16:44:20.822 -> Analog value pin 34 (ADC1_CH6) = 138
16:44:20.916 -> Analog value pin 35 (ADC1_CH7) = 138
16:44:21.010 -> Analog value pin 25 (ADC2_CH8) = 186
16:44:21.104 -> Analog value pin 26 (ADC2_CH9) = 186
16:44:21.197 -> Analog value pin 27 (ADC2_CH7) = 186
16:44:21.338 -> Analog value pin 14 (ADC2_CH6) = 186
I would expect a little bit of difference (ADC not calibrated), but that much?
The above values are obtained with 9 bits resolution, but the difference remains about the same if I increase the resolution to 10..11..12 bits. Concerning the timing: I put a short delay (100 ms) between each analogRead. It's just a bit easier on the eye. When I remove the delay, the results are the same.
Another thing I observed is that connecting GPIO12 (ADC2_CH5) to +3.3 V causes a boot error ("flash read err, 1000"). Didn't see that before, when I had all pins connected to the same rail - but maybe I didn't reboot with that setting and didn't run into that issue.
I found some posts on the espressif forum about this error (
https://www.esp32.com/viewtopic.php?t=3452), but for another board (WROVER, mine is DEVKIT).
Not sure if I should be concerned about the big deviation of the results. ADC2 results are consistently below, but reasonably close to the calculated result, but ADC1 is really off the chart.
The other thing I noticed is that I can't switch between analogRead and digitalRead for the same pins. I wouldn't need that in practice (there may be some exceptional applications for this), but since I was messing with the ports, I tried this out of curiosity.
I controlled (or rather tried to) the choice between reading analog and digital by setting GPIO36 high or low while the software was running, but I only got it to readAnalog once imediately after rebooting, after that it would just stick to readDigital, regardless of what I did on GPIO36. And all ports would always read 0, even if I connected them to +3.3V (which also explains why it wouldn't respond to changing the input on GPIO36).
Code (some of the test settings are commented out):
Code: Select all
bool analog = true;
char text[50] = {'\0'};
// put your setup code here, to run once:
void setup() {
analogSetWidth(9);
analogReadResolution(9); // set the ADC resolution
analogSetCycles(8); // successive ADC conversions to reduce noise impact
analogSetAttenuation(ADC_11db); // Global ADC setting: ADC range 0 - 3.3 V
pinMode(GPIO_NUM_36, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_36, ADC_11db); // ADC setting pin ADC1_CH0
pinMode(GPIO_NUM_39, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_39, ADC_11db); // ADC setting pin ADC1_CH3
pinMode(GPIO_NUM_34, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_34, ADC_11db); // ADC setting pin ADC1_CH6
pinMode(GPIO_NUM_35, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_35, ADC_11db); // ADC setting pin ADC1_CH7
pinMode(GPIO_NUM_32, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_32, ADC_11db); // ADC setting pin ADC1_CH4
pinMode(GPIO_NUM_33, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_33, ADC_11db); // ADC setting pin ADC1_CH5
pinMode(GPIO_NUM_25, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_25, ADC_11db); // ADC setting pin ADC2_CH8
pinMode(GPIO_NUM_26, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_26, ADC_11db); // ADC setting pin ADC2_CH9
pinMode(GPIO_NUM_27, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_27, ADC_11db); // ADC setting pin ADC2_CH7
pinMode(GPIO_NUM_14, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_14, ADC_11db); // ADC setting pin ADC2_CH6
pinMode(GPIO_NUM_12, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_12, ADC_11db); // ADC setting pin ADC2_CH5
pinMode(GPIO_NUM_13, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_13, ADC_11db); // ADC setting pin ADC2_CH4
pinMode(GPIO_NUM_2, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_2, ADC_11db); // ADC setting pin ADC2_CH2
pinMode(GPIO_NUM_4, INPUT); // set input mode
analogSetPinAttenuation(GPIO_NUM_4, ADC_11db); // ADC setting pin ADC2_CH0
Serial.begin(115200); // Arduino serial port for debugging @ 115.2 kBaud
while (!Serial) continue; // wait for serial port to connect. Needed for native USB
}
// put your main code here, to run repeatedly:
void loop() {
// use GPIO_NUM_36 to control analog or digital read of the other ports
// if input is GND, do digitalRead, else do analogRead of all other pins
// analog = (bool)digitalRead(GPIO_NUM_36);
Serial.println();
if(analog)
Serial.println("analog = true");
else
Serial.println("analog = false");
Serial.println();
if(analog) {
Serial.println("analogRead of pins...");
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC1_CH0) = %u", GPIO_NUM_36, analogRead(GPIO_NUM_36));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC1_CH3) = %u", GPIO_NUM_39, analogRead(GPIO_NUM_39));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC1_CH6) = %u", GPIO_NUM_34, analogRead(GPIO_NUM_34));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC1_CH7) = %u", GPIO_NUM_35, analogRead(GPIO_NUM_35));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC2_CH8) = %u", GPIO_NUM_25, analogRead(GPIO_NUM_25));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC2_CH9) = %u", GPIO_NUM_26, analogRead(GPIO_NUM_26));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC2_CH7) = %u", GPIO_NUM_27, analogRead(GPIO_NUM_27));
Serial.println(text);
//delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Analog value pin %u (ADC2_CH6) = %u", GPIO_NUM_14, analogRead(GPIO_NUM_14));
Serial.println(text);
//delay(100);
}
else {
Serial.println("digitalRead of pins...");
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_36, digitalRead(GPIO_NUM_36));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_39, digitalRead(GPIO_NUM_39));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_34, digitalRead(GPIO_NUM_34));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_35, digitalRead(GPIO_NUM_35));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_32, digitalRead(GPIO_NUM_32));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_33, digitalRead(GPIO_NUM_33));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_25, digitalRead(GPIO_NUM_25));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_26, digitalRead(GPIO_NUM_26));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_27, digitalRead(GPIO_NUM_27));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_14, digitalRead(GPIO_NUM_14));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_12, digitalRead(GPIO_NUM_12));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_13, digitalRead(GPIO_NUM_13));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_2, digitalRead(GPIO_NUM_2));
Serial.println(text);
delay(100);
memset(text, '\0', 40);
snprintf(text, 40, "Digital value pin %u = %u", GPIO_NUM_4, digitalRead(GPIO_NUM_4));
Serial.println(text);
delay(100);
}
// digitalRead(GPIO_NUM_36);
delay(1000); // wait a second
}
Maybe my ESP32 is kaputt?
PS: the GPIO_NUM_xx values are #defined somewhere in the esp core source. See
https://docs.espressif.com/projects/esp ... gpio_num_t.