Additional Information

Site Information

 Loading... Please wait...

ESP32 – Programming Three Serial Ports (UARTs) Using the Arduino IDE

Posted by Wilfried Voss on

ESP32 Series of Modules

Due to the global shortage of electronic components, we at Copperhill Technologies had to go through a transition phase from LPC NXP processors (which were still not available at the time of this writing) to the ESP32. Not only does the ESP32 provide improved functionality (including integrated Bluetooth and WiFi for IoT applications) and increased memory resources, but it is also available in large volumes.

However, as it is the nature of a software developer's life, we had to deal with a learning curve. Using the Arduino Integrated Development Environment (IDE) plus the availability of abundant source code examples made the development a breeze, but there were also a few quirks that needed attention. One of them is programming the additional serial ports (three UARTs) on the ESP32 processor.

When working with the Arduino IDE, you access the serial ports through the Serial class (Serial, Serial1, Serial2). However, Serial1 and Serial2 will not work with the ESP32, and there is a good reason for that.

The ESP32 processor allows mapping the three serial ports (UARTs) to any pin between GPIO0 and GPIO31. Most ESP32 development boards (including our ESP32 CAN Bus Module) offer additional ports labeled TX2/RX2 or similar, but you do not have to precisely use those pins. Every other GPIO pin can act as Serial RX/TX.

The assignment of these pins is managed through the HardwareSerial class. Its constructor accepts one parameter, i.e., the UART number. The begin method takes four parameters: Serial baud rate, UART mode, RX pin assignment, and TX pin assignment.

#include <HardwareSerial.h>
HardwareSerial ESP32Serial1(1);
ESPSerial1.begin(lBaudrate, SERIAL_8N1, 16, 17);

From here on, all other Serial functions (such as read, write) are compatible with standard Arduino hardware. The following .ino file demonstrates the use:

Click here to download the Arduino .ino file.


ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module

ESP32 WiFi, Bluetooth Classic, BLE, CAN Bus Module

This board comes with an onboard ESP32 WROOM-32 WiFi, Bluetooth Classic, BLE Module, and a CAN Bus port with a transceiver. Also, onboard is an RGB LED, IO pins on a 0.1" pad.

Programming is accomplished through the popular Arduino IDE connected to the USB-to-Serial converter with USB-C connector, automatic bootloader and reset.

The ESP32 is a series of low-cost, low-power system-on-chip microcontrollers with integrated Wi-Fi and dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations and includes built-in antenna switches, RF balun, power amplifier, low-noise receive amplifier, filters, and power management modules.

More Information...