ESP32, Arduino and 3 hardware serial ports

When working with ESP32 WiFi/Bluetooth MCU under Arduino SDK for ESP32, you will notice that Serial work just fine. But Serial1 and Serial2 do not. ESP32 has 3 hardware serial ports that can be mapped to almost any pin. But, Serial1 and Serial2 will not work. In case of ESP32 this just has to be done in a slightly different way:

The trick is to use HardwareSerial library to access UART 1 and 2 instead of Serial1 and Serial2

ESP32 board with 3 hardware serial ports

  • Class HardwareSerial accepts one parameter in constructor, it is a number of UART. Values from 0 (UART 1) to 2 (UART 3)
  • HardwareSerial(0) is the same as Serial so be aware
  • begin method accepts 4 parameters
    • baud speed
    • UART mode
    • RX pin
    • TX pin

The real beauty of this solution is that almost any pin can be used as TX or RX pin for any serial port. Most ESP32 dev boards have labels like TX2 or RX2, but you really do not have to exactly those pins. Every other GPIO pin can act as Serial RX, but only the ones between GPIO0 and GPIO31 can be used as TX. Still, that gives plenty of pins to choose from…

More about ESP32 can be read in Getting Started With ESP32 and Arduino post.

Here you can find the selection of the most interesting ESP32 Arduino compatible boards.

15 thoughts to “ESP32, Arduino and 3 hardware serial ports”

  1. I have been trying for a couple of weeks to get the second and third serial ports working. I have tried a lot of different methods but none worked. Your solution worked right off.
    I am using an ESR32 Dev board by Doit with a Nextion screen running off Ubuntu Linux.

    Thanks very much for the solution.

    Ron Hunter

  2. Thank you so much for the explanation of the 3 serial ports. I too am using the Nextion screen.
    good stuff!

    Jim Cullins

  3. I get lots and lots of errors. I am reading a GPS unit at 9600. It is useless as it comes across. Only about half of the lines are error free. Using TTGO ESP32, level shifter from SKM32. Any ideas of things to try? The last time I used the SKM32, it was perfect. I will try other hardware tomorrow.

  4. TTGO ESP32 Lora T-Beam. HardwareSerial MySerial2(2) on pin13tx. to serial ttl-usb adapter. I can get data from debug serial0 tx pin. driving me nuts. Anyone had any success on this yet?

  5. Hi,
    this is for sure very helpful (even if a bit late 🙂 ) information. Thanks!!!
    What do you mean by:
    ————————————————–
    Every other GPIO pin can act as Serial RX, but only the ones between GPIO0 and GPIO31 can be used as TX. Still, that gives plenty of pins to choose from…
    ————————————————–
    My English is not too academic, but I am missing something here to make sense?
    Can you elaborate “every other”, “only” and “plenty” in this context?
    Can you really use GPIO6 – GPIO11 ?

    1. Hi
      I have spend many hours trying to find out what is wrong.
      After this explanation was issue solved in one minute. 🙂

      Thank you very much.

  6. Hi,
    good work, I checked it, works nicely but if I used only RX /TX pins and when I was set one port xx .begin (9600 , SERIAL_8N1, x, x ) and the other to 8E1 or 8O1, parity was not considered. The transmission will be unchanged. Have You any idea how correct it ?

  7. HardwareSerial(0) is the same as Serial so be aware
    my code :
    HardwareSerial Esp32SlaveSerial(0);
    Esp32SlaveSerial.begin(115200,SERIAL_8N1,22,21); //22rx 21tx
    Is it wrong?
    serial0(uart0) Can’t be mapped to other pins (default only 1 3) ?

    Thank you!

  8. Supplemental
    debugging results:Esp32SlaveSerial.begin(115200,SERIAL_8N1,22,21);
    Esp32SlaveSerial.print()
    Output at serial port 0 (1, 3)

    How should I use 21 22 as serial port 0?

  9. How do I go about using/reassigning the CTS and RTS lines on serial2? I’m interfacing with a rather odd beast (4800 7-E-1 with hardware flow control) and actually need those lines. I could use an I2C UART but that’s a colossal PITA and I’d really prefer to not have to do that.

  10. Fantastic insights there! Appreciate you taking the time to share it. I love articles which help me push the boundaries of what I know, and what’s possible.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.