Hi, I have a problem with reading the analog values of the ADC2 in my esp-wroom-32. I'm using 10 pins distributed across the two ADC (6 on the AD1, all working perfectly, and 4 on AD2). I've been troubleshooting for days now without any results.
I've already tried setting the internal pulldown resistors but it didn't work. I also tried with ad external 10k pulldown resistor, but then the value won't go to zero. It will instead settle on a value proportional to the resistor value for some reason.
I've also tried to change the attenuation value of the pin with no results.
This doesn't seem to be code dependent since the problem is presente even with the analog read esample on the arduino ide, but if you want I can post some snippets of the code.
Any help is appreacieted.
Little Update:
I discovered that the problem of the adc2 staing high is present only when the esp32 is powered via the usb connector. I tried using the other physical uart using another esp32 as a bridge to the computer and the reading where almost normal. Almost because now the reading of one adc channel "leaks" to another (gpio 14 leaks to 27, 27 to 26, 26 to 25 and 25 to 14). Checking with the multimeter I can't see any voltages so the problem seems to be software after all. I'll post all the code.
// BLT serial of HC-05 module
#define ONBOARD_LED 2
HardwareSerial &btSerial = Serial2; //Rx: 16, Tx: 17. Swap on the bt module
//Reset Button Pin and variables
#define butPin 12 //ADC2_5 //12
// Encoder pins and variables
#define sensPin1 15 //ADC2_3 //15
#define sensPin2 5
#define N 72
#define STEP 360 / N
// EMG pins and variables
#define emg0 14 //ADC2_6
#define emg1 27 //ADC2_7
#define emg2 26 //ADC2_9
#define emg3 25 //ADC2_8
#define emg4 33 //ADC1_5
#define emg5 32 //ADC1_4
#define emg6 35 //ADC1_7
#define emg7 34 //ADC1_6
#define emg8 39 //ADC1_3
#define emg9 36 //ADC1_0
uint_fast16_t emgRead0 = 0;
uint_fast16_t emgRead1 = 0;
uint_fast16_t emgRead2 = 0;
uint_fast16_t emgRead3 = 0;
uint_fast16_t emgRead4 = 0;
uint_fast16_t emgRead5 = 0;
uint_fast16_t emgRead6 = 0;
uint_fast16_t emgRead7 = 0;
uint_fast16_t emgRead8 = 0;
uint_fast16_t emgRead9 = 0;
int count = 0; //Encoder Count
char buffer[60]; //Buffer to send the message
// mes[0:8] --> EMG
// mes[9] --> Angle
//Test variable
bool test = false;
bool btEn = true;
bool srEn = false;
void setup() {
Serial.begin(115200);
btSerial.begin(38400); //, SWSERIAL_8N1, blt_rx, blt_tx, false);
analogSetAttenuation(ADC_11db);
pinMode(ONBOARD_LED, OUTPUT);
pinMode(sensPin1, INPUT_PULLUP);
pinMode(sensPin2, INPUT_PULLUP);
pinMode(butPin, INPUT_PULLUP);
pinMode(emg0, INPUT_PULLDOWN);
pinMode(emg1, INPUT_PULLDOWN);
pinMode(emg2, INPUT_PULLDOWN);
pinMode(emg3, INPUT_PULLDOWN);
pinMode(emg4, INPUT_PULLDOWN);
pinMode(emg5, INPUT_PULLDOWN);
pinMode(emg6, INPUT_PULLDOWN);
pinMode(emg7, INPUT_PULLDOWN);
pinMode(emg8, INPUT_PULLDOWN);
pinMode(emg9, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(sensPin1), intFunc, CHANGE);
delay(2000);
if (!digitalRead(butPin)) {
srEn = true;
}
digitalWrite(emg0, LOW);
digitalWrite(emg1, LOW);
digitalWrite(emg2, LOW);
digitalWrite(emg3, LOW);
}
void loop() {
//Reset count if button pressed
if (!digitalRead(butPin)) {
count = 0;
}
//EMG reading
int time = micros();
if ((srEn || btEn)) {
emgRead0 = analogRead(emg0);
emgRead1 = analogRead(emg1);
emgRead2 = analogRead(emg2);
emgRead3 = analogRead(emg3);
emgRead4 = analogRead(emg4);
emgRead5 = analogRead(emg5);
emgRead6 = analogRead(emg6);
emgRead7 = analogRead(emg7);
emgRead8 = analogRead(emg8);
emgRead9 = analogRead(emg9);
}
//mes[0], mes[1], mes[2], mes[3], mes[4], mes[5], mes[6], mes[7], mes[8], mes[9], mes[10]
if (!test) {
// Sending measurments as a single array
sprintf(buffer, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", emgRead0, emgRead1, emgRead2, emgRead3, emgRead4, emgRead5, emgRead6, emgRead7, emgRead8, emgRead9, count * STEP);
if (srEn) {
Serial.print(buffer);
}
if (btEn) {
btSerial.print(buffer);
}
} else {
// Testing part of the code
sprintf(buffer, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", emgRead0, emgRead1, emgRead2, emgRead3, emgRead4, emgRead5, emgRead6, emgRead7, emgRead8, emgRead9, count * STEP);
time = micros() - time;
sprintf(buffer, " Count: %d\t Gradi: %d\t Sample Time: %d\n", count, count * STEP, time);
if (srEn) {
Serial.print(buffer);
}
if (btEn) {
btSerial.print(buffer);
}
delay(200);
}
if (Serial.available() > 0) {
String msg = Serial.readStringUntil('\n');
if (msg == "BTON") {
btEn = true;
} else if (msg == "BTOFF") {
btEn = false;
} else if (msg == "SERON") {
srEn = true;
} else if (msg == "SEROFF") {
srEn = false;
} else if (msg == "T") {
test = !test;
} else {
Serial.print(msg + '\n');
}
digitalWrite(ONBOARD_LED, btEn || srEn);
}
if (btSerial.available() > 0) {
String msg = btSerial.readStringUntil('\n');
if (msg == "BTON") {
btEn = true;
} else if (msg == "BTOFF") {
btEn = false;
} else if (msg == "SERON") {
srEn = true;
} else if (msg == "SEROFF") {
srEn = false;
} else if (msg == "T") {
test = !test;
} else {
btSerial.print(msg + '\n');
}
digitalWrite(ONBOARD_LED, btEn || srEn);
}
}
void intFunc() {
if (digitalRead(sensPin1) == digitalRead(sensPin2)) {
count--;
} else {
count++;
}
}
Ill leave the serial output to hopefully illustrate better the problem with the first four analog channels:
roughly 5v to the 2nd channel
0,3723,289,0,0,0,0,0,0,0,0
0,3713,285,0,0,0,0,0,0,0,0
0,3705,285,0,0,0,0,0,0,0,0
roughly 5v to the 4th channel
208,0,0,3777,0,0,0,0,0,0,0
210,0,0,3786,0,0,0,0,0,0,0
209,0,0,3791,0,0,0,0,0,0,0