Skip to content

[Nano ESP32] Serial Ports update #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,14 @@ void setup() {

## USB Serial & UART

The Nano ESP32 board features 2 separate hardware serial ports.
The Nano ESP32 board features 3 hardware serial ports, as well as a port exposed via USB.

One port is exposed via USB-C®, and
One is exposed via RX/TX pins.
- `Serial` refers to the USB port.
- `Serial0` refers to the first hardware serial port (UART), accessible via the board's RX/TX pins (D0, D1).
- `Serial1` is the second UART port, which can be assigned to any free GPIOs.
- `Serial2` is the third UART port, which can also be assigned to any free GPIOs.

### Native USB
### Serial (Native USB)

Sending serial data to your computer is done using the standard `Serial` object.

Expand All @@ -428,9 +430,11 @@ Serial.print("hello world");

To send and receive data through UART, we will first need to set the baud rate inside `void setup()`.

### UART
### Serial0 (UART)

The pins used for UART on the Nano ESP32 are the following:
***Please note: `Serial0` is shared with the bootloader/kernel, which prints a few messages at boot/reset, and in the event of a crash, the crash dumps is printed via FreeRTOS on this serial port. For these reasons, you may want to use the `Serial1` or `Serial2` ports to avoid any interference ([read more](#serial1--serial2-uart)).***

The default pins for UART communication on the Nano ESP32 are the following:

| Pin | Function | Description |
| --- | -------- | -------------------- |
Expand Down Expand Up @@ -461,6 +465,30 @@ And to write something, we can use the following command:
Serial0.write("Hello world!");
```

### Serial1 & Serial2 (UART)

The Nano ESP32 features 2 additional hardware serial ports that have no pre-defined pins, and can be connected to any free GPIO. Therefore, to use them, their TX and RX pins need to be manually assigned.

To use `Serial1` and `Serial2`, you need to initialize them in your program's `setup()` function:

```arduino
//initialization
Serial1.begin(9600, SERIAL_8N1, RX1PIN, TX1PIN);
Serial2.begin(9600, SERIAL_8N1, RX2PIN, TX2PIN);

//usage
Serial1.write("Hello world!");
Serial2.write("Hello world!");
```

- Replace `RXPIN` and `TXPIN` with the GPIOs you want to assign (e.g. `D4`, `D5`).
- You can then use commands such as `Serial1.write()` and `Serial1.read()`.

The `SERIAL_8N1` parameter is the configuration for serial communication.
- `8` = data word length (8-bit). Can be changed to 5,6,7-bits.
- `N` = parity, in this case "none". Can be changed to "even" (E) or "odd" (O).
- `1` = stop bit, other option available is 2.

## I2S

The Inter-IC Sound (I2S or IIS) protocol is used for connecting digital audio devices with a variety of configurations (Philips mode, PDM, ADC/DAC).
Expand Down