Serial communication between esp32 and arduino mega sends null and ⸮

I tried to send serial from my esp32 to my mega. I connected from the esp32 rxp2 and tx2 to my mega rx2 and tx2. I know the serial works, but it also sends a lot of nulls and ⸮.
esp32 code:

#include "thingProperties.h"
#define RXp2 16
#define TXp2 17


void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here 
  // for (int i =0; i<301; i++)
  // {
  //   Serial2.print(i);
  //   Serial2.print("#");
  //   //Serial.println(Serial2.readString());
  //   Serial.print("sent packet ");Serial.println(i);
  //   delay(500);
  // }
  Serial2.write("test#");
  Serial.println(temp);
  delay(1000);
  
}

arduino code:

String message;
char character;
#define RXp2 16
#define TXp2 17
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial2.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial2.available() > 0){
    character = Serial2.read();
    if(character == '#'){
      Serial.println(message);
      
      message = "";
      Serial.println();
    }else{
      message.concat(character);
    }
  }
}

What it prints out:

⸮⸮⸮⸮test

⸮p⸮test

⸮⸮⸮⸮⸮⸮⸮k[⸮۶⸮߯⸮⸮⸮⸮⸮<⸮test

⸮⸮test

⸮⸮⸮test

⸮⸮⸮test

Note: my esp32 code is on the arduino iot cloud.
The code works that it listen until # is sends and then print out the message

your MEGA Tx sends 5V
your ESP Rx only accepts 3.3V

do you have a voltage adapter in between?

you also need to join the GNDs of your boards

Thank you, grounding the boards together solved it, I don't send from the mega to the esp so I don't really need both wires so no need to 5v to 3v3

1 Like

What type of level shifter are being used?

Again I only send from the esp to the Arduino, I solved the issue by connecting there grounds, I also removed the wire from the rx in the esp to the tx of the Arduino. So now it's only one way communication, esp--->Arduino

that should do it then

have fun!

Glad that you got it sorted. Installation and troubleshooting is not for problem with your project, hence your topic has been moved to a more suitable location on the forum.

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