I searched a lot around concerning how UARTS on NANO ESP32 are working, but still have a lot of doubts. Tech specifications report that there are three UARTS on board: SERIAL0, SERIAL1 and SERIAL2.
a. the term "pin" make me crazy, what do they mean? According what? The schematic of the board the "pins" are two headers connectors numbered from 1 to 15. KIKAD as as first "pin" the D1/TX0. "Pin" 0 and "pin" 1 showed above are D1 and D0 but, according to the schematic board are GPIO44 and GPIO43.Different tables from ardino web site state different things.See below
b. SERIAL0 is used by the core to dirive the USB port. If I want to use it for monitoring I just have to add the following lines to my sketch:
Serial.begin(9600);
Serial.println("HELLO");
but shall refer to that UART in this way?
Serial0.begin(9600);
Serial0.println("HELLO");
c. How I can use the other two UARTS? For instance if I want (actually I have to use them, no others "pins" left) to use the D0/RX0 and D1/TX0 "pin" what I have to do? Something like this?
const byte RXD0 = 0; //D0/RX0
const byte TXD1 = 1; // D1/TX0
Serial1.begin(9600, SERIAL_8N1, RXD0, TXD1);
It doesn'work!
d. What about then Serial2? How I can use it?
By default, Serial2 is configured to use GPIO19 and GPIO20. These microcontroller pins are not broken out on the Nano ESP32, so it is necessary to configure the pins in the sketch code. For example, if you wanted to use Arduino pin 9 as RX and pin 10 as TX, you would use it like this:
void setup() {
Serial2.begin(9600);
// Configure Serial2 to use pins 9 and 10 as RX and TX
Serial2.setPins(9, 10);
}
void loop() {
Serial2.println("hello");
delay(1000);
}
Fig-1 is the pin out diagram of generic ESP32. Locate the DPin numbers for UART0 which is connected with PC, UART1 is not useable and is not shown, UART2 is avilable to user.
(1) Arduino NANO ESP32 is of the same form factor of classic NANO. (2) In Fig-1, D8 correspond to GPIO-17 which is TX2 pin of UART2 (3) In Fig-2, GPIO-16 is the RX2 pin of UART2, and it not available with Fig-1. (4) etc.
Guys, so much confusion here. Let me try to clear up the air.
Facts on Arduino Nano ESP32
The Arduino Nano ESP32 contains an ESP32-S3 CPU, not an ESP32, so the replies above mentioning an ESP32 are off-topic.
Yes, it is confusing. Sorry
Facts on Arduino Nano ESP32 serial ports
Serial0, Serial1 and Serial2 are physical UARTs provided by the ESP32-S3 CPU. They can be used as all other UARTs in Arduino.
For compatibility with the original Arduino Nano board, the names of the Serial0 I/Os are D0 (aka RX) and D1 (aka TX), and they are in a strange sequence on the side of the board: 1, then 0, then 2, 3, and so on.
Serial0 is used by the bootloader and is the "debug UART", therefore there are some "unavoidable" messages being output there at boot and when a critical issue is detected. If you use Serial0, do remember that.
The I/Os associated with Serial1 and Serial2 aren't defined. You can use any (otherwise unused) GPIO signal.
Serial on the other hand is the "Arduino console", and on the Nano ESP32 it is a virtual UART implemented via USB CDC.
The board's USB port is directly connected to the ESP32-S3, which provides this virtual UART emulation. There are no external USB/serial converter chips.
Via the Tools -> USB Mode menu of the IDE, you can choose if you want a software CDC implementation (via TinyUSB, provides DFU and ease of use) or a hardware CDC (limited, but allows debugging). Serial remains on the virtual USB port in either case; this is just choosing the "USB driver" on the CPU.
Facts on Arduino Nano ESP32 pin numbers
We want to keep compatibility with all the other boards in the Nano family, so that it is possible to use a sketch made for a different board mentioned there on the Nano ESP32. This has created a conflict, since the ESP32-S3 CPU numbers GPIOs differently.
We have a guide on this subject. What is meant by "pin" depends on how you configure the Tools -> Pin Numbering menu in the IDE:
The default choice, By Arduino number, makes the pin numbers in your sketch match the original Nano layout (the orange ones in the first picture). With this option, to access Serial0's TX you need to use 1.
This compatibility is however limited to the Arduino API and the simplest libraries. Libraries that use advanced hardware features of the CPU will get confused and you will have all sorts of errors or weird behaviors.
If you choose By GPIO number (legacy), the pin numbers in your sketch are interpreted as the hardware GPIO numbers (the green ones in the same picture). With this option, to access Serial0's TX you need to use 43.
This is the safe choice for maximum compatibility with libraries. However, you need to use the GPIO (green) numbers in the sketch.
That said, there's an easy way to avoid this confusion and be able to use the same sketch on both pin mappings:
Please, please, please use pin names in your sketch and not numbers. If you write D12 instead of 12, it is obvious you are referring to the pin in the top right of the picture, and the core will transparently map that to 12 (by default) or 47 (in GPIO mode). No headaches!
Hope this is helpful to clean up some misconceptions! Let me know if there's something I've missed.