Serial comm between arduinos not working

Hi, I'm using an Arduino Mega and an Arduino Nano 33 IoT. I'm trying to get them to communicate using Serial1 on Mega and Serial on Nano but with no luck. I managed to get communication in the same way with a nodemcu/esp8266 but applying exactly the same method doesn't seem to work.

This is my simplified code on Nano:

#include <ArduinoJson.h>
#include <ezTime.h>

void setup() {

  Serial.begin(115200);
  while (!Serial and millis() < 5000);

}

void loop() {

  int traincount=0;
  char buffer[400];
  // Print Time
  sprintf(buffer, "%i\t%i\t%i\t%i\t%i\t%i", year(myTZ.now()), month(myTZ.now()), day(myTZ.now()), hour(myTZ.now()), minute(myTZ.now()), second(myTZ.now()));
  Serial.print(buffer);
    JsonArray depart = doc["departures"];

  // Print the values in Json body from http request
  for (JsonObject train : depart) {
    if ( Direction_correct( train["display_informations"]["headsign"].as<String>() ) ){

      String time = train["stop_date_time"]["arrival_date_time"];
      sprintf(buffer, "\t%s\t%f\t%f", train["display_informations"]["direction"].as<const char*>(), CompareTime(time, 1), Compare2Times(time, train["stop_date_time"]["base_arrival_date_time"], 1)/5);
      Serial.print(buffer);
      traincount++;
      if (traincount >= 3) {break;}
    }

  }

  Serial.print("\n");

  delay(30000);

}

This prints the correct data to my computers monitor, skipping lines and with correct tabs ("\t")

This is my code on the Mega which works with the nodemcu

void setup() {

  Serial.begin(115200);
  Serial1.begin(115200);
  while(!Serial or !Serial1);

}

void loop() {

  receivefromwifimodule();

}

void receivefromwifimodule() {

  while (Serial1.available() > 0) {

    static String message;
    char inByte = Serial1.read();
    Serial.print(inByte);

    if ( inByte != "\n") { 
      message += inByte;
    }
    //Full message received...
    else
    {
      Serial.println();
      Serial.println(message);
      message = "";
    }
  }

}

Which pins are you using for serial comms on the Nano 33 IOT and have you taken into account that the Nano is a 3.3V device whereas the Mega is a 5V device ?

Note that pins 0 and 1 on the Nano are for the Serial1 interface rather than Serial

2 Likes

Mega is 5V and Nano 33 IoT is 3.3V - do you have proper voltage translation?

Have you tried each Arduino separately, and verified that it can communicate - both send and receive - with a serial terminal on your computer?

Have you tried using one of the spare ports on the Mega to echo what it's receiving?

oh I didn't know those pins were for Serial1 it now works !
Thank you so much :slightly_smiling_face:

I am glad that it works

Have you taken the voltage difference between the 2 boards into account ?

1 Like

You did not answer the questions about the needed level shifter, why?

1 Like

Since I’m only sending data from the 3.3V to the 5V board I’m assuming I don’t require one

So the answer to

is yes, and I assume that there is a common GND connection between the two boards

That'll probably work - until it doesn't!

And, when it doesn't, you'll have no way to tell - because you have no confirmation or feedback .

I’ll implement one then :+1:

That is not what I would have expected after

1 Like

One what ?

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