Can't analogRead pin 19 or 22. Ugh.

extan1
Posts: 4
Joined: Fri Jan 21, 2022 6:50 pm

Can't analogRead pin 19 or 22. Ugh.

Postby extan1 » Fri Jan 21, 2022 7:11 pm

Greetings!

This feels like a trivial question, but I've struggled with it for days and am completely out of ideas.

I've got some ESP32 DevkitCs from Amazon (https://smile.amazon.com/gp/product/B0718T232Z) and am testing with the Arduino IDE (set to Board: "ESP32 Dev Module"). When I analogRead GPIO pins 19 or 22 get back "0".

Here's a test run using a 1k/10k voltage divider between 3.3V and ground as input (reads 0.3V on DMM). I've connected the same input to GPIO 34 as a reference:

Code: Select all

Pin 19: 0
Pin 22: 0
Pin 34: 221
Pin 19: 0
Pin 22: 0
Pin 34: 224
Pin 19: 0
Pin 22: 0
Pin 34: 222
Pin 19: 0
Pin 22: 0
Here's the code:

Code: Select all

void setup() {
  Serial.begin(115200);
}

void loop() {
  Serial.print("Pin 19: "); Serial.println(analogRead(19));
  Serial.print("Pin 22: "); Serial.println(analogRead(22));
  Serial.print("Pin 34: "); Serial.println(analogRead(34));
  delay(1000);
}
And a pic of the wiring:

Image

I had two boards exhibiting the same behavior, but came to the conclusion that I must have fried them somehow during prototyping. So I just bought a third board and plugged it in straight out of the package. Alas, it shows the same behavior ;(

My project involves a number of sensors, so really hate to forsake 2 inputs.

It seems almost impossible that I could have three separate boards all with the same problem. But I have exactly zero other ideas.

I'm hoping someone may have had a similar experience, or have some thoughts about what I might try next.

Thanks in advance for any ideas!
Attachments
PXL_20220121_175537323-sm.jpg
PXL_20220121_175537323-sm.jpg (388.28 KiB) Viewed 6210 times

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: Can't analogRead pin 19 or 22. Ugh.

Postby ESP_Sprite » Sat Jan 22, 2022 5:06 am

GPIO19/22 are not analog-capable pins. Refer to here and use pins marked with 'ADC1_x'. You can also use 'ADC2_x' pins, but only when you do not use WiFi.

extan1
Posts: 4
Joined: Fri Jan 21, 2022 6:50 pm

Re: Can't analogRead pin 19 or 22. Ugh.

Postby extan1 » Sat Jan 22, 2022 1:30 pm

Thanks very much for your reply.

I think I may have misunderstood the pin configuration explanation. I tested what I believe to be all available GPIO pins except strapping, flash, and uart (in my previous wiring some were working as digital inputs, so had not tested as analog inputs):

Code: Select all

Pin 0: (skip)
Pin 1: (skip)
Pin 2: (skip)
Pin 3: (skip)
Pin 4: 314
Pin 5: 0
Pin 6: (skip)
Pin 7: (skip)
Pin 8: (skip)
Pin 9: (skip)
Pin 10: (skip)
Pin 11: (skip)
Pin 12: 314
Pin 13: 314
Pin 14: 315
Pin 15: 315
Pin 16: 0
Pin 17: 0
Pin 18: 0
Pin 19: 0
Pin 20: (skip)
Pin 21: 0
Pin 22: 0
Pin 23: 0
Pin 24: (skip)
Pin 25: 314
Pin 26: 315
Pin 27: 313
Pin 28: (skip)
Pin 29: (skip)
Pin 30: (skip)
Pin 31: (skip)
Pin 32: 303
Pin 33: 304
Pin 34: 304
Pin 35: 302
Pin 36: 304
Pin 37: (skip)
Pin 38: (skip)
Pin 39: 303
I then tested the pins that failed (analogRead == 0) as digitalRead, and they work properly:

With input set LOW:

Code: Select all

Pin 5: 0
Pin 16: 0
Pin 17: 0
Pin 18: 0
Pin 19: 0
Pin 21: 0
Pin 22: 0
Pin 23: 0
With input set HIGH:

Code: Select all

Pin 5: 1
Pin 16: 1
Pin 17: 1
Pin 18: 1
Pin 19: 1
Pin 21: 1
Pin 22: 1
Pin 23: 1
These pins match the GPIO pins listed in the diagram that are not associated with either ADC_1 or ADC_2, so it seems that I misinterpreted these as pins capable of analog input, but they are not....in which case I'm very embarrassed.

Is that correct?

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: Can't analogRead pin 19 or 22. Ugh.

Postby ESP_Sprite » Sun Jan 23, 2022 2:07 am

When you say 'pin 19 or 22', and in the list of pins in your tables: Are you talking about the physical pin of your development board or are you talking about GPIO 19 and 22?

extan1
Posts: 4
Joined: Fri Jan 21, 2022 6:50 pm

Re: Can't analogRead pin 19 or 22. Ugh.

Postby extan1 » Mon Jan 24, 2022 1:05 am

Sorry for being unclear. When I say "pin" I'm referring to GPIO pins

ESP_Sprite
Posts: 9770
Joined: Thu Nov 26, 2015 4:08 am

Re: Can't analogRead pin 19 or 22. Ugh.

Postby ESP_Sprite » Mon Jan 24, 2022 7:51 am

Gotcha. I *think* you have the right idea now. In general, the ESP32 has a bunch of GPIOs. In theory, all of these can be used as digital inputs. Most of these (GPIO0-33) can be used as digital outputs as well. Some of these can also be used as analog inputs (32-39) and some of them can also be used as analog inputs but only if WiFi is not active (0, 2, 4, 12-15, 25-27).
Aside from how they're used, the 2nd issue to take into account is if these pins are already used for something else. Specifically, GPIO6-11 are connected to the flash and as such you can't use them for something else, and GPIO1 and 3 are used for serial, so you probably don't want to use them for anything else.

extan1
Posts: 4
Joined: Fri Jan 21, 2022 6:50 pm

Re: Can't analogRead pin 19 or 22. Ugh.

Postby extan1 » Mon Jan 24, 2022 11:25 pm

Yes. I really appreciate your help. Thank you very much.

I think the key point that I was missing -- and seems apparent now, but was just not seeing before -- is that if a GPIO pin is not marked as associated with ADC1 or ADC2, eg the pins I was having trouble with:

Pin 5
Pin 16
Pin 17
Pin 18
Pin 19
Pin 21
Pin 22
Pin 23

then it can NOT be used as as an analog input.
Please correct me if I'm wrong, but otherwise will consider this question resolved.
Thank you again!

Who is online

Users browsing this forum: No registered users and 46 guests