Confusing pin numbering in the default sketch

I just received my Arduino Nano ESP32 and wanted to tinker with the default sketch. I added the colors yellow, pink, turquoise and white to the sketch and uploaded it. Only the orange onboard LED started flashing. The RGB LED did nothing. So I deleted the lines for the built-in LED from the sketch to see if it was uploaded correctly and it worked. No blinking. So what was wrong... a few minutes later I found the article about the different pin numbering options in the IDE. I switched to GPIO numbering, uploaded again and it was working. But wait the colors are in the wrong order... so green and blue must be switched...

It is a bit confusing that the default sketch doesn't use the default pin numbering (Arduino). Also it is confusing that the numbering for LEDG(green) and LEDB(blue) are switched in the default sketch.
Maybe this should be changed in the Arduino Nano ESP 32 Cheat Sheet website.

Here is my altered code with the switched LEDG and LEDB pin numbers and all 7 colors.

#define LEDR 46
#define LEDG 0
#define LEDB 45
#ifdef LED_BUILTIN
#undef LED_BUILTIN
#define LED_BUILTIN 48
#endif

void setup() {
  // put your setup code here, to run once:
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  //yellow
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, HIGH);

  delay(1000);
  //red
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);

  delay(1000);
  //pink
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, LOW);

  delay(1000);
  //blue
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, LOW);
  
  delay(1000);
  //turquoise
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);

  delay(1000);
  //green
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, HIGH);

  delay(1000);
  //white
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, LOW);

  delay(1000);
}
1 Like

I always refer to this image, the orange pin numbering is for Arduino and the yellow pin numbering is for Micropython.

I use Micropython with the Nano ESP32 so I am unsure if the pins retain the 'D' or not, but whatever one of the following should work.

#define LEDR D14
#define LEDG D15
#define LEDB D16
#ifdef LED_BUILTIN
#undef LED_BUILTIN
#define LED_BUILTIN D13
#endif
#define LEDR 14
#define LEDG 15
#define LEDB 16
#ifdef LED_BUILTIN
#undef LED_BUILTIN
#define LED_BUILTIN 13
#endif

2 Likes

Thank you. I only looked in the table of the "Nano ESP32 Selecting Pin Configuration"-guide which ends at D13. My post was meant as constructive criticism for the persons who manage the documentation as it is rather confusing for beginners. IMAO they should add D14, D15 and D16 to the predefined pin names list and change the default sketch accordingly so it will work with both configurations. Currently this sketch won't work because D14, D15 and D16 aren't defined.

#define LEDR D14
#define LEDG D15
#define LEDB D16
#ifdef LED_BUILTIN
#undef LED_BUILTIN
#define LED_BUILTIN 48
#endif

void setup() {
  // put your setup code here, to run once:
  pinMode(LEDR, OUTPUT);
  pinMode(LEDG, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(LEDR, LOW);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, HIGH);

  delay(1000);

  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, LOW);
  digitalWrite(LEDB, HIGH);

  delay(1000);

  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(LEDR, HIGH);
  digitalWrite(LEDG, HIGH);
  digitalWrite(LEDB, LOW);

  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
}

@T-Error yes there is possibly a need for a re-write of the docs, for me that pin out diagram has been very useful, it's not the first pin out you come across so I think that sometimes it gets over looked.

look at the very last post in this thread

There is an option to choose between two different numbering schemes which I was unaware of but like I said I use the Nano with Micropython so it was not really affecting what I was doing.

good luck with your project

@sumguy Yeah, I was referring to the guide which explains the different numbering schemes. There they recommend using the predefined labels " You can also control pins using labels such as
D1, D2, D3. These labels are predefined in the Board Package, and are adjusted based on what configuration you make.". But since D14, D15, D16 aren't predefined they won't work on any of both numbering scheme configurations.

Thanks for bringing this to our attention @T-Error. I have submitted a fix for the problems you reported here:

You can give the updated sketch from my proposed fix a try:

void setup() {
  // put your setup code here, to run once:
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  pinMode(LED_BLUE, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_BLUE, HIGH);

  delay(1000);

  digitalWrite(LED_BUILTIN, LOW);
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_GREEN, LOW);
  digitalWrite(LED_BLUE, HIGH);

  delay(1000);

  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(LED_RED, HIGH);
  digitalWrite(LED_GREEN, HIGH);
  digitalWrite(LED_BLUE, LOW);

  delay(1000);
  digitalWrite(LED_BUILTIN, LOW);
}

The "Arduino ESP32 Boards" core defines pin macros for all four LEDs:

  • LED_RED
  • LED_GREEN
  • LED_BLUE
  • LED_BUILTIN

The core is configured to adjust the values of these macros so that they are always correct regardless of what you have selected from the Tools > Pin Numbering menu in Arduino IDE. So best practices is to use these macros to refer to the LED pins instead of defining redundant macros as was done in the sketch in the documentation.

The first limited production run of the Nano ESP32 used a different model of RGB LED than the subsequent runs (as noted here). The pin numbers in the sketch were correct for the boards of that first run, but incorrect for the far more numerous number of boards from the subsequent production runs. The LED_GREEN and LED_BLUE macros are correct for the boards from the subsequent production runs.

3 Likes

@ptillisch And again the Arduino team proves that they are simply awesome. Thank you for the quick response! The new sketch works flawlessly on both configurations.

1 Like