Serial Communication Between a Leonardo and an Uno

Hello!

I am having trouble receiving information sent by my arduino Uno to my Leonardo via RX/TX connections. When using two arduino Unos however, the message passes just fine? Could this be due to the whole Serial1 thing on the arduino Leonardo? P.S. I have also noticed how when using both Unos, powering the receiving arduino provides current for both arduinos, whereas with the Leonardo, it does not provide current for the Uno sending it information.

Here are the codes I have uploaded to both arduinos:

(Receiving / Leonardo)

char mystr[10];

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial1.readBytes(mystr,5);
  Serial.println(mystr); 
  delay(1000);
}

(Sending / Uno)

char mystr[5] = "Hello";

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.write(mystr,5);
  delay(1000);
}

Thank you in advance!

I have also noticed how when using both Unos, powering the receiving arduino provides current for both arduinos,

Only by leakage. You shouldn’t try to power it like that. You can damage one or both.

You say you’re having trouble, but you didn’t say what the trouble was.

There are two serial ports on the Leonardo, Serial and Serial1. The Serial instance is via the onboard USB-to-serial conversion process. Serial1 is via the RX and TX pins.

If you don't have anything connected to the RX and TX pins, using Serial1 is not going to accomplish anything.

Here's the tested Serial code for Arduino Leonardo:

void setup()
{
  Serial.begin(115200); //This pipes to the serial monitor
  while(!Serial);
  Serial1.begin(115200); //This is the UART, pipes to sensors attached to board
}

void loop()
{
  if( Serial.available() )
  {
    int inByte = Serial.read();
    Serial1.write( inByte );
  }
  if( Serial1.available() )
  {
    int inByte = Serial1.read();
    Serial.write( inByte );
  }
}

On your receiver, you might consider waiting until something is available to read before trying to read it.

Thanks all,
Second answer helped out a lot and made someone very happy!
This thread can be closed

Delta_G:
On your receiver, you might consider waiting until something is available to read before trying to read it.

Try the following codes (a modified version of your's):

char mystr[10];

void setup() 
{
  Serial.begin(9600);
  Serial1.begin(9600);   //it was missing
}

void loop() 
{
  
  byte n = Serial1.available();
  if (n != 0)
  {
     byte m = Serial1.readBytes(mystr,5);  //blocking code
     mystr[m] = '\0';      //you need to place null character at the end of char array
     Serial.println(mystr);
     //delay(1000);         //timing is being controlled by sender
  }
}

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R