As all the hardware UART Ports (UART0 - UART3) of MEGA have been utilized, you can comfortably create a software UART Port (SUART) on MEGA and connect it with the UART1 Port of ESP32 C3 for data excage. Leave the UART0 of both MEGA and ESP32 C3 connected with the PC/SM.
1. Make the following hardware setup (Fig-1):
Figure-1:
2. Upload the following sketch into ESP32 C3:
char myData[10];
void setup()
{
Serial.begin(9600);
Serial1.begin(9600, SERIAL_8N1, 20, 21); //RX1 = 20, TX1 = 21
delay(1000);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
byte m = Serial.readBytesUntil('\n', myData, sizeof myData - 1);
myData[m] = '\0'; //insert null character
Serial.println(myData);
//-----------------------
Serial1.println(myData);
}
n = Serial1.available();
if (n != 0)
{
byte m = Serial1.readBytesUntil('\n', myData, sizeof myData - 1);
myData[m] = '\0'; //insert null character
Serial.println(myData);
//-----------------------
}
}
3. Upload the following sketch into MEGA.
#include<SoftwareSerial.h>
SoftwareSerial SUART(10, 11); //SRX = 10, STX = 11
char myData[10];
void setup()
{
Serial.begin(9600);
SUART.begin(9600);
}
void loop()
{
byte n = Serial.available();
if (n != 0)
{
byte m = Serial.readBytesUntil('\n', myData, sizeof myData - 1);
myData[m] = '\0'; //insert null character
Serial.println(myData);
//-----------------------
SUART.println(myData);
}
n = SUART.available();
if (n != 0)
{
byte m = SUART.readBytesUntil('\n', myData, sizeof myData - 1);
myData[m] = '\0'; //insert null character
Serial.println(myData);
//-----------------------
}
}
4. Open Serial Monitor of ESP32 C3 at Bd = 9600 and Newline option.
5. Open Serial Monitor of MEGA at Bd = 9600 and Newline option.
6. Press Reset Buttons of oth Arduinos.
7. Enter ESP32 C3 from the InputBox of the Serial Monitor of ESP32 C3. Check that the message as appered on the OutputBox of the Serial Monitor of MEGA.
8. Enter MEGA from the InputBox of te Serial Monitor of MEGA. Check that the message has appered on the OutputBox of the Serial Monitor of ESP32 C3.
9. OUTPUT:
ESP32 C3
MEGA