0% found this document useful (0 votes)
419 views

Demo 2 - How To Use Multiple Serial Ports On Arduino ESP32 - IoT Sharing PDF

The document discusses using multiple serial ports on the Arduino ESP32 microcontroller. It explains that the ESP32 has 3 serial ports that can be accessed using the HardwareSerial class. It provides an example code that prints the string "hello ESP32" every second to the serial monitor by creating instances of the HardwareSerial class for each serial port, like HardwareSerial Serial1(1).
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
419 views

Demo 2 - How To Use Multiple Serial Ports On Arduino ESP32 - IoT Sharing PDF

The document discusses using multiple serial ports on the Arduino ESP32 microcontroller. It explains that the ESP32 has 3 serial ports that can be accessed using the HardwareSerial class. It provides an example code that prints the string "hello ESP32" every second to the serial monitor by creating instances of the HardwareSerial class for each serial port, like HardwareSerial Serial1(1).
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

27/1/2019 Demo 2: How to use multiple Serial ports on Arduino ESP32 ~ IoT Sharing

Demo 2: How to use multiple Serial ports on


Arduino ESP32
7:19 AM

IOTSHARING DOTCOM

ESP32
NO COMMENTS

1. Introduction
Arduino ESP32 use Serial port to flash software and print information on Terminal. ESP32 supports 3
Serial ports so you need not to use SoftwareSerial as in general Arduino. In this tutorial we only care
about using How to use multiple Serial port on Arduino ESP32 to print the debug information to
Terminal.
2. Hardware
You do not need any extra hardware.
3. Software
We use "HardwareSerial" class for Serial communication. It has some important interfaces:
- HardwareSerial(int uart_nr): this is the constructor of HardwareSerial where uart_nr is 0, 1 or 2 so we
have maximum 3 Serial ports.

- void begin(unsigned long baud, uint32_t config=SERIAL_8N1, int8_t rxPin=-1, int8_t


txPin=-1):initialize Serial port with baudrate, Serial mode (default is SERIAL_8N1), rxPin and txPin (if you
leave these parameters empty library will use default pins).

1 void HardwareSerial::begin(unsigned long baud


2 {
3 if(_uart_nr == 0 && rxPin < 0 && txPin
4 rxPin = 3;
5 txPin = 1;
6
7 }
8 if(_uart_nr == 1 && rxPin < 0 && txPin
9 rxPin = 9;
10 txPin = 10;
11 }
12 if(_uart_nr == 2 && rxPin < 0 && txPin
13 rxPin = 16;
14 txPin = 17;
15
16 }
_uart = uartBegin(_uart_nr, baud,
}

- available(): Get the number of bytes (characters) available for reading from the serial port.
- print(): Prints data to the serial port as human-readable ASCII text.
- println(): Prints data to the serial port as human-readable ASCII text followed by a carriage return
character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').
http://www.iotsharing.com/2017/05/how-to-use-serial-arduino-esp32-print-debug.html 1/2
27/1/2019 Demo 2: How to use multiple Serial ports on Arduino ESP32 ~ IoT Sharing

- read(): Reads incoming serial data on Rx pin.


- readStringUntil(): reads characters from the serial buffer into a string until facing terminator character.
- Because Arduino library created a default instance HardwareSerial Serial(0), so you can use created
Serial object directly (in example below) without create an instance by yourself.
- In order to use more Serial port, you just create another instance of HardwareSerial such
as: HardwareSerial Serial1(1) or HardwareSerial Serial2(2) and then use them as usual.
We will make a simple demo that print string “hello ESP32” every 1 second on Terminal. This demo is
easy. To monitor “printed” data just go to Tools > Serial Monitor
4. Result

Figure: ESP32 using Serial to print debug information

Related Posts:

http://www.iotsharing.com/2017/05/how-to-use-serial-arduino-esp32-print-debug.html 2/2

You might also like