Serial comunication between arduino mega and esp32 cam

I was try to send some data in form of string form esp32 cam to Arduino mega 2560.
This is the code for esp32 cam:

#include <HardwareSerial.h>

HardwareSerial SerialPort(1);

void setup() {
  // Inisialisasi Serial untuk komunikasi dengan PC
  Serial.begin(115200);
  // Inisialisasi Serial1 untuk komunikasi dengan Arduino Mega
  SerialPort.begin(9600, SERIAL_8N1, 16, 17); // RX=16, TX=17
}

void loop() {
  // String yang akan dikirim
  String message = "Hello from ESP32-CAM";

  // Kirim string ke Arduino Mega
  SerialPort.println(message);

  // Tampilkan di Serial Monitor
  Serial.println("Message sent: " + message);

  // Tunggu 2 detik sebelum mengirim lagi
  delay(2000);
}

and this is the code for the mega:

void setup() {
  // Inisialisasi Serial untuk komunikasi dengan PC
  Serial.begin(115200);
  // Inisialisasi Serial1 untuk komunikasi dengan ESP32-CAM
  Serial2.begin(9600);
  delay(1000);
}

void loop() {
  // Periksa apakah ada data yang tersedia di Serial1
  if (Serial2.available()) {
    // Baca data yang diterima dari ESP32-CAM
    String message = Serial2.readStringUntil('\n');
    // Tampilkan di Serial Monitor
    Serial.println("Message received: " + message);
  } else{
    Serial.println("serial2 is not available");
  }

delay(100);
}

And then I uploaded the code for the esp32 cam first and the for the mega. When I run the code for the mega I got this result:

Message received: Jk�

serial2 is not available

serial2 is not available

serial2 is not available

serial2 is not available

serial2 is not available

serial2 is not available

serial2 is not available

serial2 is not available

serial2 is not available

I got that result over n over again without the string form the variable message in the mega.
What did I do wrong? and how to fix it?

*Edited

Have you taken into account that the Mega runs at 5V and the ESP32 runs at 3.3V ?

Have the two boards got a common GND ?

Yes i know that esp32 cam can run at 3.3V. But i read online that esp32 cam run better at 5V and many people ran issues when 3.3V used. The esp32 cam GND is connected to the GND arduino mega.

You power through USB at 5V but the pins (eg Rx) are designed for 3.3V

yes i use a voltage divider for the Rx pin esp32 cam. The 5V i used only for the power to power the esp32 cam that connected to the 5V pin in esp32 cam.

you use Serial2 and Serial1 .... stick to the one where the ESP is connected... :slight_smile:

Thank you for showing me the mistake and I already updated it and it still got this result:

Message received: Jk�
serial2 is not available
serial2 is not available
serial2 is not available
serial2 is not available
serial2 is not available
serial2 is not available
serial2 is not available
serial2 is not available
serial2 is not available

at some point it the serial 2 is available but the message are those character and the question mark icon and other time the serial2 is not available.

explain exactly the circuit (did you connect the GNDs?)

ESP32 cam pin Arduino Mega Pin
5V ------------------> 5V
GND ------------------> GND
UoR ------------------> Tx2 (16)
UoT ------------------> Rx2 (17)

What about the volatge adapter ??

Im still using the usb cabel for uploading the arduino mega code that connected from my PC

No voltage adapter between the mega and the esp then ?

yes, no voltage adapter

But do you lower the voltage from arduino Tx to ESP Rx ???

Yes i use a voltage divider to lower the voltage

So how is this wired ?

ESP32 cam pin Arduino Mega Pin
5V ------------------> 5V
GND ------------------> GND
UoR ------------------> Tx2 (16)
UoT ------------------> Rx2 (17)

I give up...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.