I am working on a project that involves serial communication between an Arduino Mega (receiver) and an ESP32-CAM (sender). The ESP32-CAM is responsible for scanning a QR code and sending the data (a text) to the Arduino Mega. Depending on the data received, the Arduino Mega will give commands to two servo motors. I have a logic level shifter to avoid any problems of voltage between an Arduino Mega and the ESP32-CAM.
However, I am facing two main issues:
UART USB Connection: Currently, I am using UART USB to upload code to the ESP32-CAM, which means that the RX and TX pins of the ESP32-CAM are connected to the UART USB. As a result, I cannot use these pins for serial communication with the Arduino Mega. Is it possible to use other pins for serial communication instead of (e.g., GPIO 1 and GPIO 3)?
Connecting Arduino Mega and ESP32-CAM for Serial Communication: After uploading the code, I need to connect the Arduino Mega and ESP32-CAM for serial communication. How should I do this? How should I connect the logic level shifter in this setup?
Any help or guidance on these issues would be greatly appreciated. Additionally, if anyone has sample code for this setup, it would be very helpful.
first, don't power the servos from the MEGA's 5V output. That might draw too much current for your MEGA and lead to a crash. Servo should be powered separately and GND shared with the MEGA.
the typical wiring of a voltage adapter is as such:
you have on one side the 3.3V I/Os and on the other side the matching 5V I/Os. The correspondance goes both ways. So connect Tx and Rx of the ESP-CAM to the 3.3V side and Rx1 and Tx1 of the MEGA on the other side (so that Rx is connected to Tx and Tx to Rx). connect GNDs and suitable voltage on both sides.
Look if Serial2 is available for the ESP32-CAM. That will give you a second UART to connect to your MEGA (use Serial1 for example so that the MEGA can also have it's Serial line free for upload and debug)
I used ESP-CAM pins 14 (RX1) and 15 (TX1) to connect it to a Mega Serial1 pins 18 TX1 and 19 RX1
note the potential divider on the Mega Tx1 pin 18 to ESP32 RX1 pin 14 serial line
program to test ESP-CAM Serial1
// ESP32-CAM Serial1 test
// for loopback test connect pins 14 and 15
#define RXD1 14
#define TXD1 15
void setup() {
// initialize both serial ports:
Serial.begin(115200);
Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
Serial.println();
Serial.println("serial1 test Rx pin 14 Tx pin 15");
}
void loop() {
// read from port 1, send to port 0:
if (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0, send to port 1:
if (Serial.available()) {
int inByte = Serial.read();
//Serial.write(inByte);
Serial1.write(inByte);
}
}
EDIT: any reason why you cannot run the servos from the ESP-CAM and eliminate the Mega?
I use this option but i use the logic level shiffter, when it comes to send message from arduino mega 2560 to ESP32-CAM it works and i see the message in Arduino mega serial monitor.
but when i send message from ESP32-CAM to arduino mega it doesn't work i see the message that i send from esp32cam to arduino mega on serial monitor of esp 32 cam.
for my project i need the data from the esp 32 cam to arduino mega .
how have you connected the level shifter?
the serial program I used on the mega was
// Arduino Mega serial1 test
// mega Serial1 pin 18 is Tx
// Serial1 pin 19 is Rx
// for loopback test connect pin 18 to pin 19
// for RS232 shield connect
// Mega pin 18 TXD to TTL/RS232 Tx
// Mega pin 19 RXD to TTL/RS232 Rx
// for loopback test connect 9-pin D_type connector pins 2 Tx to 3 Rx (pin 5 is GND)
// connect GND pins together and VCC to 5V
void setup() {
Serial.begin(115200); // initialise serial monitor port
Serial1.begin(115200); // initialise Serial1
Serial.write("Arduino Mega Serial1 test - for loopback test connect pin 18 to pin 19\n");
Serial.write("RS232: Mega pin 18 TXD to TTL/RS232 Tx and pin 19 RXD to TTL/RS232 Rx\n");
Serial.write("RS232 - loopback connect 9-pin D-type pin 2 Tx to pin 3 Rx\n");
}
void loop() {
if (Serial1.available()) { // read from Serial1 output to Serial
Serial.write(Serial1.read());
}
if (Serial.available()) { // read from Serial outut to Serial1
char inByte = Serial.read();
//Serial.write(inByte); // local echo if required
Serial1.write(inByte);
}
}
you can test it by connecting pin 18 to 19 to form a loopback test - text entered on serial monitor is echoed back to the display
how have you connected the level shifter?
Arduino Mega 2560 Level Shiffter ESP32CAM
TX(Pin18) HV|LV TXD(gpio14)
RX(Pin19) HV|LV RXD(gpio15)
5v HV|LV i don't connect it
GND GND|GND GND I don't connect 5v of arduino mega 2560 (HV) to 3.3v(LV) of ESP32-CAM because i use ttl to programm esp 32 cam that's mean urat and uot and gnd and 5v pins of esp32cam aren't free to use themm
if u can help me show me how to connect the arduino and esp32cam ( put in your mind that i use ttl for program code and i can't remove it after i upload the code )