qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
kstillson
 
Posts: 20
Joined: Tue Feb 04, 2014 1:48 am

qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post by kstillson »

summary: a bunch of the qtpy gpio pins (a2, a3, sda) seem to not work at all for input in an Arduino sketch, while others (d0, a0) work only in pullup mode (not pulldown mode). But things work perfectly with an equivalent sketch in Circuit Python. Is there some sort of special initialization that circpy is doing for me that I need to do manually in my Arduino sketch?

I'm using just about the simplest possible sketch to scan the pins:

Code: Select all

#define D0 0     // qt-py-esp32-s2: D0 / gpio0 is the built in button near the usb-C port.
#define A4 SDA

void setup() {
  Serial.begin(115200);
  while (!Serial) { delay(20); }  // Wait for serial port to stabalize. 
  
  pinMode(D0, INPUT_PULLUP);    // hardwired against ground, so must use pullup mode.
  pinMode(A0, INPUT_PULLUP);    // Seems to only work in pullup mode.
  pinMode(A1, INPUT_PULLUP);    // Works fine
  pinMode(A2, INPUT_PULLUP);    // Doesn't work at all, in either pullup or pulldown mode.
  pinMode(A3, INPUT_PULLUP);    // Doesn't work at all, in either pullup or pulldown mode.
  pinMode(A4, INPUT_PULLUP);    // Doesn't work at all, in either pullup or pulldown mode.
}

void loop() {
  Serial.printf("d0=%d, a0=%d, a1=%d, a2=%d, a3=%d, a4=%d\n",
      digitalRead(D0), digitalRead(A0), digitalRead(A1), digitalRead(A2), digitalRead(A3), digitalRead(A4));
  delay(1000);
}
As noted in the comments, D0 and A1 work just fine. A0 works if you use pullup mode. a2, a3, and a4 don't seem to work regardless of which mode you use. Specifically- they each return the pulled-to value, regardless of whether the pad is connected to VCC or ground.

On the electrical side, I'm just using a plain qtpy-esp32-s2 with male headers carefully soldered, and a simple female-to-female jumper to connect either to 5v or gnd pin and whatever pad is being tested. I'm sure there's no physical problem with the board or my wires, because when I run a Circuit Python script that does exactly the same monitoring, everything works perfectly.

I'd love to use use Circuit Python and ditch Arduino completely, but my larger project needs deep sleep mode, which circ-py doesn't seem to have support for; at least not that I can find. It was when I couldn't get that larger project working that I came up with this extremely simplified version of the pin scanner, and found that even in this simple base-case, I can't get GPIO working for a number of the pins. :-(...

Any suggestions? Thanks!

- Ken

User avatar
kstillson
 
Posts: 20
Joined: Tue Feb 04, 2014 1:48 am

Re: qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post by kstillson »

In case it's ever useful to anyone- I did figure out the proximate cause, if not the root cause.

It appears that the Arduino IDE can occasionally map some of the BCM pin number constants to the incorrect values.

For example, I find that when I have my board set to "Adafruit QT Py ESP32-S2" (which is my board), that A2 is defined as 16, but the correct value should be 9. (As per this diagram: https://cdn-learn.adafruit.com/assets/a ... 1640130293).

When I look at what seems to be the relevant .h file (~/snap/arduino/70/.arduino15/packages/esp32/hardware/esp32/2.0.5/variants/adafruit_qtpy_esp32s2/pins_arduino.h), I see what looks like the correct assignment (on line 43):
static const uint8_t A2 = 9;

However, when I make use of A2 in my code, it's definitely coming up as 16, which is why the wrong pin is being initialized and scanned in the code I provided.

If I add the following, it fixes everything:

#define A0 18
#define A1 17
#define A2 9
#define A3 8
#define A4 7
#define A5 6

Why I have to do this, I do not understand. But once that's there, everything works exactly as expected.

Anyway, hope that comes in useful to someone, someday...
Best regards,

- Ken

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post by adafruit_support_carter »

However, when I make use of A2 in my code, it's definitely coming up as 16,
How do you know this?

Running the following example:

Code: Select all

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

void loop() {
  Serial.print("A2 = "); Serial.println(A2);
  delay(1000);
}
on a Qt PY ESP32 S2 results in the following output:

Code: Select all

A2 = 9
A2 = 9
A2 = 9
A2 = 9
A2 = 9
A2 = 9
A2 = 9

User avatar
kstillson
 
Posts: 20
Joined: Tue Feb 04, 2014 1:48 am

Re: qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post by kstillson »

Using your exact code, (I've got timestamps turned on):
13:23:55.591 -> A2 = 16
13:23:56.586 -> A2 = 16
13:23:57.581 -> A2 = 16
13:23:58.575 -> A2 = 16
13:23:59.603 -> A2 = 16

Although after a bit more grep'ing around, I think I found it.. Not surprisingly- human error ("This sort of thing has cropped up before, and it has always been due to human error." - HAL9000)

It looks like in the long pulldown list of boards, I selected "Adafruit Feather ESP32-S2" rather than "Adafruit QT Py ESP32-S2".. <sigh..> They're right next to each other on a list of dozens of boards...

Looks like there's a feature request to allow filtering of the boards list.. It's been open since 2017! <grumble grumble>.. https://github.com/arduino/Arduino/issues/6034 .. I guess I'll do what other folks on that thread have done, and just add my own script to manually filter .arduino15/packages/esp32/hardware/esp32/2.0.5/boards.txt so this doesn't keep happening.

Thanks for the nudge in the right direction!

- Ken

User avatar
adafruit_support_carter
 
Posts: 29150
Joined: Tue Nov 29, 2016 2:45 pm

Re: qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post by adafruit_support_carter »

Yep, that'd explain it. No worries - finding/picking the correct board in the Arduino IDE is a *huge* source of general issues. Long lists of boards. Some of the names are similar. etc. It can be confusing and picking an incorrect board comes up a lot.

User avatar
kstillson
 
Posts: 20
Joined: Tue Feb 04, 2014 1:48 am

Re: qt py esp32-s2: some gpio pins don't work in Arduino, but work fine in Circuit Python

Post by kstillson »

In-case anyone is interested, I've published my solution to filtering the boards list in my general collection of free tools:
https://github.com/kstillson/ktools/blo ... -boards.py

Might as well try to turn a silly mistake into some public good...

- Ken

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”