Site Information

 Loading... Please wait...

Blog

ESP32 UART0, UART1, UART2 Access Using the Arduino IDE

Posted by Wilfried Voss on

In the following, I am using the ESP32 WROOM Devkit for this tutorial in combination with the Arduino IDE. For more information on programming the ESP32 using the Arduino IDE, please refer to “Getting started with ESP32 development board and Arduino.

The Arduino IDE uses the Serial class (assigned to UART0) to access the USB port on the ESP32 WROOM Devkit. It is suitable for posting debugging information on the IDE’s Serial Monitor. However, the ESP32 offers more options for serial communication.

ESP32 UART Interfaces

The ESP32 has three UART interfaces: UART0, UART1, and UART2. They provide asynchronous, bidirectional communication at up to five Mbps. You can establish serial protocols using the appropriate breakout boards, such as RS232, RS422, RS485, USB, and more. One advantage of using the ESP32 is that you can assign almost any pin to the TX and RX signals for any serial port. Some development boards may come with labels such as RX2 and TX2, but you can still assign them to other pins.

However, when working with the ESP32 using the Arduino IDE, you will notice that the Serial class works fine, but Serial1 and Serial2 will not. Consequently, accessing Serial1 and Serial2 on the ESP32 requires using the HardwareSerial library.

Programming with the HardwareSerial Class

  • Baud Rate
  • UART Mode
  • Rx Pin
  • Tx Pin

As I mentioned before, you can assign almost any pin to the TX and RX signals for any serial port but there are some restrictions: Any GPIO pin can serve as Serial RX, but only the ones between GPIO0 and GPIO31 can be used as TX.

The HardwareSerial class supports all methods used for the standard Serial class, such as read(), write(), and more.

Addressing Potential Serial Buffer Overflow

I worked on numerous embedded projects that required bidirectional serial data exchange. I learned quickly that data reception can easily result in a buffer overflow unless you take protective actions through proper code design. The exact result of a buffer overflow is unpredictable, but it will most likely result in a program crash.

Before using the ESP32 as my standard solution, I worked with other embedded systems based on the NXP LPC17xx processors in combination with the LPCXpresso IDE. This setup required the development of code that operated closer to the hardware layer than using the Arduino IDE. Regarding the serial connection, I was able to use an Interrupt Service Routine (ISR) to catch every received data byte and store it in a buffer. I designed the buffer to prevent overflow effectively by assigning a sufficient buffer size and using a ring buffer algorithm.

Unfortunately, the Arduino programming environment does not provide an easy means of creating a similar protection against buffer overflow. Like the previously referred method, the Arduino/ESP23 processors receive serial data per interrupt routine. However, I consider the default buffer size insufficient for most serial-interface applications.

The standard Arduino processor provides 64 bytes, while the ESP32 has 256 bytes per default. For instance, a GPS sensor with NMEA 0183 protocol easily exceeds that size within milliseconds.

The standard buffer size will be sufficient when you can guarantee an adequate data polling frequency. However, you may encounter infrequent processor crashes when the buffer size is exceeded by only one byte. I observed this effect with the ESP32 but have not tested it with Arduino processors.

The thought that comes to mind is creating our own ISR and implementing a sufficient buffer algorithm. I don’t know if such an approach is possible, but it will require more knowledge and time than I am willing to invest. One resource warned about adding an ISR to the existing routine as it may create unpredictable results (i.e., program crashes).

You can, however, adjust the buffer size by using the setRxBufferSize method, as demonstrated in the sample code below. The ESP32 has more than sufficient memory resources to accommodate large buffer sizes. Nevertheless, keep an eye on the data polling frequency.

ESP32 UART Access Programming Sample

The sample code below is very simple, so I don’t provide a download link.

ESP32 Serial Ports - UART0, UART1, UART2 Access Using the Arduino IDE - Sample Code


espBerry - ESP32 Development Board with Dual Isolated CAN Bus HAT

espBerry - ESP32 Development Board with Dual Isolated CAN Bus HAT

The espBerry DevBoard combines the ESP32-DevKitC development board with any Raspberry Pi HAT by connecting to the onboard RPi-compatible 40-pin GPIO header. 

The Dual Channel CAN Bus expansion HAT, designed for the Raspberry Pi, supports the full CAN2.0 Standard, and it features multi onboard protection circuits, high anti-interference capability, and reliable operation. As a result, it suits applications such as automotive devices or industrial automation.

The HAT is well documented, and there are multiple code samples using the C programming language under the Arduino IDE.

Read more...

Testing The SAE J1939 to RS232 & USB Gateway By Copperhill Technologies

In this post, I will demonstrate the test setup of our SAE J1939 to RS232 & USB Gateway. The JCOM.J1939.SER gateway is a high-performance, low-latency, serial vehicle network adapter for SAE J1939 applications. It allows any host device with an RS232 or USB port to monitor SAE J1939 data traffic and communicate with the SAE J1939 vehicle [...]

Read More »


Programmable Converter With Dual CAN FD, LIN, RS-232 Ports And microSD Slot

The CANFD LIN Gateway by MACH SYSTEMS is an easy-to-use, easy-to-program router, data logger, and simulator that features two CAN FD channels, a LIN channel, an RS-232 port, a microSD card slot, and multiple digital and analog inputs and outputs. The firmware can be developed using C/C++ in a free-of-charge IDE, and the user can load the application [...]

Read More »


Industrial IoT 4G LTE Router & Gateway With Ethernet, RS232, RS485 Serial Ports Plus Digital I/O

The ICR-3200 LTE gateway by Advantech represents an excellent solution to connect IP or serial devices to a cellular network, allowing industrial M2M and IoT applications that comprise of kiosks, industrial PCs, HMIs, traffic controllers, meters, UPS systems, and more.Supporting LTE Cat.4 upload speeds of up to 50 Mbps and download speeds of up to 150 Mbps, the [...]

Read More »


Industrial-Strength USB To RS232, RS485, TTL (UART) Isolated Converter

WaveShare introduced their industrial USB to RS232/485/TTL isolated converter with original FT232RL. It features embedded protection circuits such as power isolation, ADI magnetical isolation, and TVS diode, and more. Also, the USB TO RS232/485/TTL gateway comes with an aluminum alloy enclosure, making it robust and durable for industrial requirements.The USB TO RS232/485/TTL converter is very easy [...]

Read More »


Programmable CAN Bus To RS232 Gateway For Communication Between Controllers

The b-CANCubeNano by b-plus is a fitting gateway for serial data communication between CAN Bus and EIA-232 networks.The product was designed primarily for the use in vehicles and mobile machines. It features a CAN Bus interface according to ISO 11898-2 to communicate, for instance, with other machine controllers. It also maintains an EIA-232 serial interface to interact with other serial [...]

Read More »


Local Interconnect Network (LIN) To Controller Area Network (CAN Bus) Converter

The LIN (Local Interconnect Network) bus is an inexpensive serial communications protocol, which effectively supports remote application within a car's network. It is particularly intended for mechatronic nodes in distributed automotive applications, but is equally suited to industrial applications. It is intended to complement the existing CAN network leading to hierarchical networks within cars.Controller Area Network [...]

Read More »


SAE J1939 Network Bridge With RS232/Wireless Connection

As part of another customer project, we have started work on an SAE J1939 network bridge device, which we call the jCOM.J1939.BRIDGE. The bridging serial hardware layer is RS232 in combination with a bit-stuffing protocol, similar to the protocol described at JCOM.J1939 PROTOCOL & PROGRAMMING INTERFACE (PDF).  Our customer will use the RS232 serial port for extension of [...]

Read More »


SAE J1939 (CAN Bus) To RS232 Gateway With USB Option

We at Copperhill Technologies always thrive in adding more products to our line of OBD-II and SAE J1939 devices. Our current focus is on a SAE J1939 to RS232 gateway that can also easily used to connect to a USB port. The firmware will be identical to that of our SAE J1939 to Bluetooth Gateway With [...]

Read More »


RS232 to USB Converter With Arduino Due Or Mega 2560

Just about everyone who is involved with serial communication will have his/her RS232 to USB converter. Today's PCs don't even bother to support RS232. Consequently, a USB converter is mandatory to monitor RS232 data traffic. All this appears to render the following project obsolete. However, when it comes to a protocol converter, i.e. the conversion [...]

Read More »