0 - Introduction
Most Esp32s have more than one UART, which lets us communicate between microcontrollers, with multiple computers and even with some peripherals like GPS modules.
To follow this article you will either need atleast two Esp32s (buy here) or one Esp32 and atleast one Uart to USB converter (buy here). You can also use any other microcontroller that has UART, as long as you can replicate the code below in it.
1 - Code
On the setup function we need to begin every UART the Esp32 has. UART0 is started with the normal ‘Serial.begin’ you use with Arduinos and then UART1 and 2 are started by giving the begin function the Serial mode, RX and TX pins.
By default, Serial2 uses pins 16 to receive and 17 to transmit.
#include <Arduino.h>
void setup()
{
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, GPIO_NUM_2, GPIO_NUM_4);
Serial2.begin(115200, SERIAL_8N1, RX2, TX2);
}
On loop, for every UART, we will check if there is anything available to read, and if there is we read it and send it to the other UARTS:
void loop()
{
if (Serial.available())
{
char c = Serial.read();
Serial1.print(c);
Serial2.print(c);
}
if (Serial1.available())
{
char c = Serial1.read();
Serial.print(c);
Serial2.print(c);
}
if (Serial2.available())
{
char c = Serial2.read();
Serial.print(c);
Serial1.print(c);
}
}
And that’s it. As you could see, using any UART other than UART0 is pretty much the same.
Thanks for reading and stay tuned for more tech insights and tutorials. Until next time, and keep exploring the world of tech!