[HELP] Serial Communication between an ESP32-CAM and Arduino Mega2560

Background: I am attempting to create a "follow me robot" that uses a combination of sensors and visual feedback from the ESP32-CAM for path planning and obstacle avoidance. As you probably would think, for most types of image / video processing, the Arduino Mega2560 and even the ESP32 is simply not strong enough. Hence, why I am using the ESP32-CAM to not only capture images, but also to live stream that image so a stronger computer (in my case a smartphone via an android app) will process the videostream and instruct the "follow me robot" on where to go. But, since my follow me robot would need a lot of input pins just using the ESP32-CAM is not sufficient. I need to find some way to allow serial communication using the RX and TX pins between my ESP32-CAM and my Arduino Mega. I need to be able to send input data from my various sensors to the ESP32-CAM so that the ESP32-CAM can then forward that data via wifi to a smartphone android app.

Anyways, for reference, in order to upload code to the ESP32-CAM, I currently have my Arduino board and ESP cam looked up like this with the settings on my Arduino IDE using the board "AI Thinker ESP32-CAM":

In order to upload code to the Arduino Mega, all I'm doing is disconnecting the GND from the RESET pins, and setting my IDE to the "Arduino Mega" board.

And of course my Arduino mega is connected to my computer and that's how I am uploading code to both my mega and esp cam.

Problem: As I alluded to in the background portion of this, my problem is I am not sure how exactly to go about communicating between the Arduino Mega and ESP Cam. I'm almost certain it's possible via the RX/TX pins, but I haven't the slightest clue on how to approach this programmatically. Here are some things I have tried:

  • As I described in my background, I can upload code individually to both my mega and my esp, and they both work. Moreover, in the serial monitor, I noticed that when both boards have their serial outputs at different baud rates, I can get debug info from both boards by just changing the baud rate I'm listening to. So based off that, I did try setting both boards to the same baud rate to see if I could just use the Serial.available() function to get serial info from each other between the boards.

  • I tried to use the SoftwareSerial library as shown below. In this example, what I was attempting to do:

Code on my MEGA

#include <SoftwareSerial.h>

SoftwareSerial esp(0,1); //RTX, TX

void setup() {
  Serial.begin(9600);
  while (!Serial.available()) {
  }
  esp.begin(19200);
}

void loop() {
  Serial.println(esp.read());
}

Then I modified the CameraWebServer example code from the ESP32-CAM and added these [not I'm not including code from the CameraWebServer because it's too long and distracting:

#include <SoftwareSerial.h> //Note: This is the esp softwareserial library. Not the regular one. Just an implementation of the regular one for esps.

SoftwareSerial ard(1, 3); //1 for GPIO1 and 3 GPIO3 - not sure if this is right

void setup() {
  Serial.begin(9600);
  while (!Serial.available()) {
  }
  ard.begin(19200);
}

void loop() {
  if (ard.available()) {
     ard.write("Test");
  }
}

And well, not only did my webserver stop working for the ESP32-CAM, but I did not get a "Test" printed on my console either. I feel like I am missing some fundamental or approaching this completely the wrong way.... :frowning:

P.S. this is my first post, if I missed anything, or if you need more information from me, do let me know!

SoftwareSerial esp(0,1); //RTX, TX

Looks like your first 2 mistakes :
1 - why are you using SoftwareSerial on the Mega when it has 4 hardware UARTs ?
2 - one of the hardware UARTs uses pins 0 and 1

How else do I do it using the hardware UARTs? In my research for this topic, I saw a few places mention a "HardwareSerial" library. I found 2 libraries with a similar name on Arduino ide:

But these aren't the same I saw in the other examples.

How else do I do it using the hardware UARTs?

Just use the correct pins for the connections and then Serial1, Serial2 or Serial3 in the serial functions just as you would Serial

See Serial - Arduino Reference

The ESP32-CAM only has a single UART. And that UART is used my me for programming. So I used the espsoftwareserial library and did this:

Code on Arduino board (note: I connected the rx/tx wires AFTER uploading my code and just hit reset):

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
}

void loop() { // run over and over
  Serial.write("hiya\n");
  delay(1000);
}

Code on ESP32 CAM:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(15, 14); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.println(mySerial.readString());
  }
  delay(1000);
}

Now the issue is, that the "hiya" only prints out right after I hit the reset button on my arduino board. And only then. It doesn't print hiya every 1 second like I would expect it to. Am I doing something wrong in my code?

Now the issue is, that the "hiya" only prints out right after I hit the reset button on my arduino board. And only then. It doesn't print hiya every 1 second like I would expect it to

Add code to turn an LED on/off every time the Arduino sends the message. Does the Arduino continue to send the message or does it stop ?

By the way, I still think that you should use another UART on the Mega

The ESP32-CAM only has a single UART. And that UART is used my me for programming.

On the ESP32 you can also define another hardware serial interface using pins of your choice like this

	Serial2.begin(115200,SERIAL_8N1,14,15);

UKHeliBob:
On the ESP32 you can also define another hardware serial interface using pins of your choice like this

	Serial2.begin(115200,SERIAL_8N1,14,15);

I have told the OP this in another thread but their comment was;

Yes, the ESP32 supports 3 UART ports but I am talking about the ESP32-CAM. It's a different product. The ESP32-CAM has fewer ports because the cam takes up several of those ports internally. So I am correct. The ESP32-CAM only supports a single UART port ;).

So the (working) programs I have that use 2 UARTS on the ESP32CAM are apaprently not possible.

So the (working) programs I have that use 2 UARTS on the ESP32CAM are apaprently not possible.

I must be imagining that it works on the ESP32-CAM too, even though it does

srnet:
I have told the OP this in another thread but their comment was;

So the (working) programs I have that use 2 UARTS on the ESP32CAM are apaprently not possible.

OK, you are right. I apologize. I was very dismissive. I thought you misread and were commenting on the ESP32.
I was just going by this diagram


Once again, I apologize for being so dismissive. I will try this.

1 Like

aseef:
I thought you misread and were commenting on the ESP32.

I was actually comentating on both.

The ESP32CAM uses a standard ESP32-S with regulator components and a socket (note the use of the word socket) for the camera and SD card, as the schematic for the ESP32CAM makes clear (always a good idea to study schematics).

Now the ESP32 used in the ESP32CAM has no way of knowing that there is a 'socket' connected to its pins, so you can use those pins if the camera and SD card are not plugged in.

I am sorry, but I can't figure out the answer explained in this thread. Can anyone explain to me how can I address the same exact problem discussed in this thread?

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