Chapter 8 Serial Communication

Serial Communication is a means of communication between different devices/devices. This section describes ESP32’s Serial Communication.

Project 8.1 Serial Print

This project uses ESP32’s serial communicator to send data to the computer and print it on the serial monitor.

Component List

ESP32-WROVER x1

Chapter01_00

GPIO Extension Board x1

Chapter01_01

Micro USB Wire x1

Chapter08_00

Serial port on ESP32

Freenove ESP32 has integrated USB to serial transfer, so it could communicate with computer connecting to USB cable.

../../../_images/Chapter08_02.png

Arduino Software also uploads code to Freenove ESP32 through the serial connection.

Your computer identifies serial devices connecting to it as COMx. We can use the Serial Monitor window of Arduino Software to communicate with Freenove ESP32, connect Freenove ESP32 to computer through the USB cable, choose the correct device, and then click the Serial Monitor icon to open the Serial Monitor window.

../../../_images/Chapter08_03.png

Interface of serial monitor window is as follows. If you can’t open it, make sure Freenove ESP32 has been connected to the computer, and choose the right serial port in the menu bar “Tools”.

../../../_images/Chapter08_04.png

Circuit

Connect Freenove ESP32 to the computer with USB cable

../../../_images/Chapter08_05.png

Sketch

Sketch_08.1_SerialPrinter

../../../_images/Chapter08_06.png

Download the code to ESP32-WROVER, open the serial port monitor, set the baud rate to 115200, and press the reset button. As shown in the following figure:

../../../_images/Chapter08_07.png

As shown in the image above, “ESP32 initialization completed! “ The previous is the printing message when the system is started, it uses the baud rate of 120,000, which is incorrect, so the garbled code is displayed. The user program is then printed at a baud rate of 115200.

How do I disable messages printed at system startup?

Use a jumper wire and connect one of its ends to GPIO5 of development board and the other to GND, so that we can disable the system to print out startup messages.

For more information, click here.

The following is the program code:

 1/**********************************************************************
 2  Filename    : SerialPrinter
 3  Description : Use UART send some data to PC, and show them on serial monitor.
 4  Auther      : www.freenove.com
 5  Modification: 2024/06/18
 6**********************************************************************/
 7
 8void setup() {
 9  Serial.begin(115200);
10  Serial.println("ESP32 initialization completed!");
11}
12
13void loop() {
14  Serial.printf("Running time : %.1f s\n", millis() / 1000.0f);
15  delay(1000);
16}

Reference

void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t txPin=-1, bool invert=false, unsigned long timeout_ms = 20000UL);

Initializes the serial port. Parameter baud is baud rate, other parameters generally use the default value.

size_t println( arg );

Print to the serial port and wrap. The parameter arg can be a number, a character, a string, an array of characters, etc.

size_t printf(const char * format, ...)  __attribute__ ((format (printf, 2, 3)));

Print formatted content to the serial port in the same way as print in standard C.

unsigned long millis();

Returns the number of milliseconds since the current system was booted.

Project 8.2 Serial Read and Write

From last section, we use serial port on Freenove ESP32 to send data to a computer, now we will use that to receive data from computer.

Component and circuit are the same as in the previous project.

Sketch

Sketch_08.2_SerialRW

../../../_images/Chapter08_08.png

Download the code to ESP32-WROVER, open the serial monitor, and set the bottom to Newline, 115200. As shown in the following figure:

../../../_images/Chapter08_09.png

Type characters such as ‘ABCDEFG’ at the top, then press Enter to send the data to the ESP32, and the serial port monitor will print out the data received and forwarded back by the ESP32.

../../../_images/Chapter08_10.png

The following is the program code:

 1/**********************************************************************
 2  Filename    : SerialRW
 3  Description : Use UART read and write data between ESP32 and PC.
 4  Auther      : www.freenove.com
 5  Modification: 2026/01/12
 6**********************************************************************/
 7String inputString = "";      // a String to hold incoming data
 8bool stringComplete = false;  // whether the string is complete
 9
10void setup() {
11  Serial.begin(115200);
12  Serial.println("\nESP32 initialization completed!");
13  Serial.println("Please input some characters,");
14  Serial.println("select \"Newline\" below and Ctrl + Enter to send message to ESP32.");
15  Serial.println("Ready to receive data...\n");
16}
17
18void loop() {
19  if (Serial.available()) {
20    inputString = Serial.readStringUntil('\n');
21
22    // trim() removes leading and trailing whitespace characters, including \r, \n, tabs, etc.
23    inputString.trim();
24
25    if (inputString.length() > 0) {
26      stringComplete = true;
27    }
28  }
29
30  if (stringComplete) {
31    Serial.printf("inputString: %s\r\n", inputString.c_str());
32    inputString = "";
33    stringComplete = false;
34  }
35}

In loop(), determine whether the serial port has data, if so, read and save the data, and if the newline character is read, print out all the data that has been read.

Reference

String();

Constructs an instance of the String class.

For more information, please visit

https://www.arduino.cc/reference/en/language/variables/data-types/stringobject/

int available(void);

Get the number of bytes (characters) available for reading from the serial port. This is data that’s already arrived and stored in the serial receive buffer.

Serial.read();

Reads incoming serial data.