Blog
Recent Posts
ESP32, ESP32-S2 - Serial Port, Native USB Access using Arduino IDE
Posted by
on
The test setup, as shown in the image, represents the hardware of a new project that requires reading CAN (Controller Area Network) data frames, combining them with real-time information plus GPS position, and storing the result onto an SD card. This post will focus on the ESP32 communicating with the GPS module delivering NMEA 0183 serial data per UART at 9600 baud.
Since we use the Arduino IDE to program the ESP32, we are using the Serial class, representing the standard approach. Thus, any experienced programmer wouldn't expect any significant problems but think again when it comes to time-sensitive applications. After all, a slow serial rate of 9600 baud may impact the performance. First of all, it is mandatory to reserve a sufficient buffer to store the serial data when the program is busy with other tasks. Secondly, there are some flaws in the ESP32's adaptation of the HardwareSerial class, which came unexpectedly and caused delays during the development process.
Assigning the Receive Buffer Size
In our project, we assign the serial TX and RX pins and use the HardwareSerial class, as demonstrated in the below shown code. For more information on the use of HardwareSerial, see https://quadmeup.com/arduino-esp32-and-3-hardware...
In the setup() function, we install a serial buffer of 1024 bytes, which is sufficient to store the full NMEA 0183 serial data string. As noted in the code, the standard Arduino CPU utilizes a 64-byte buffer, while the ESP32 comes with 256 bytes. That size can be increased by using Serial.setRxBufferSize. All this will take care of the serial buffer design.
The Problem with Serial.readBytes()
The code in the loop() section shows our initial approach to catching the data using the Serial.readBytes() function. However, we encountered a problem with this method, i.e., the function always returns MAX_STRLEN, regardless of the actual number of bytes received. This is most probably a bug in the ESP32's HardwareSerial library for the Arduino IDE. It is therefore highly recommended not to use this function.
There are a few alternatives to accomplish the functionality. The following code section demonstrates our version:
ESP32-S2 - Native USB
The reason for using the S2 version of the ESP32 was the need for a high-speed serial connection, namely the ESP32-S2's native USB, which is faster than any other serial connection on the processor.
We did some time-consuming research on accessing the native USB port. Unfortunately, we found a tremendous amount of misleading information, probably published immediately after the release of the S2 version. In the meantime, however, the situation has improved, and things are much simpler than explained in the original resources.
First of all, you need to update the Arduino IDE's Board Manager as described here: https://quadmeup.com/arduino-esp32-and-3-hardware...
The code turned out to be pretty simple, as demonstrated below, but please be aware that this is only a demo sample which should not be used "as is."
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.
ESP32 with Dual Isolated CAN Port Controls Thomson Electrac Linear Actuator
The above image shows part of a hardware setup for a customer project to control and synchronize two Thomson Electrac linear actuators. See also my posts:Thomson Electrak Linear Actuator with SAE J1939 Interface for Railway and Industrial Applications Thomson... Electrak HD Linear Actuator - Multiple Actuators in the Same Network Will Empty the Batteries... Thomson Electrac HD [...]
espBerry Project: ESP32 with CH9102F USB-UART Chip for Serial Speed up to 3Mbit/s
I have a project that involves the ESP32-DevKitC development board where the application requires the fast transport of data per USB. The onboard USB-UART bridge chip provides transfer rates of up to 3 Mbps. In addition, the ESP32 has three UART interfaces, i.e., UART0, UART1, and UART2, which provide asynchronous communication, communicating at a speed of [...]
SAE J1939 Protocol Stack Sketch for ESP32 Using the Arduino IDE
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. Furthermore, the processor provides the means to easily [...]
ESP32 DC Motor PWM Speed Control (Pulse Width Modulation) - The Hidden Facts
Lately, I have worked on two projects requiring DC motor control per PWM (Pulse Width Modulation). What both projects had in common was a profound lack of documentation regarding PWM control, specifically the side effects that have a significant impact on the programming. This post aims to point to these quirks that may cause considerable headaches [...]
ESP32 Development Board with NMEA 2000 & NMEA 0183 HAT
The ESP32 processor, integrating peripherals such as UART, CAN Bus, WIFI, and Bluetooth, allows a wide range of applications, and the low price adds to its popularity. There are various ESP32 development boards available in the market, such as the ESP32-WROOM-32, as shown to the left. However, compared to popular systems like the Raspberry Pi or [...]
Extending ESP32 Functionality by Using Raspberry Pi HATs
The ESP32 processor, integrating peripherals such as UART, CAN Bus, WIFI, and Bluetooth, allows a wide range of applications, and the low price adds to its popularity. There are various ESP32 development boards available in the market, such as the ESP32-WROOM-32, as shown to the left. However, compared to popular systems like the Raspberry Pi or [...]
ESP32 Triple CAN Bus Application Through Adding Two MCP2515 Ports
The first question that may arise when talking about accessing the MCP2515 CAN Bus controller per ESP32 may be, "Why would you need an MCP2515 controller when the ESP32 comes with an internal CAN port?" Yes, I found this question in one of the online forums while researching this particular topic. The answer is easy: [...]
ESP32 Project: USB to Bluetooth Gateway
The ESP32 processor allows the integration of Wi-Fi, Bluetooth, and Bluetooth LE (BLE) for a wide range of IoT (Internet of Things) applications. Using Wi-Fi ensures connectivity within a large radius. At the same time, Bluetooth allows the user to easily detect (with low-energy beacons) a module and connect it to an Android/iOS smartphone or [...]
ESP32 Based CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway
The following is a follow-up on our post Controller Area Network (CAN), SAE J1939, NMEA 2000 Wireless Module for IoT and ELD Applications. The CAN Bus, SAE J1939 and NMEA 2000 to Bluetooth Gateway is the first product of a new series of wireless gateways for IoT (Internet of Things), ELD (Electronic Logging Devices), and other applications. At the [...]