Serial between arduinos with rx/tx pins

Edit: This is fixed by calling "Serial1." functions if using rx/tx hardware serial.

I read this blog post and figured I'd give a go at getting a pair of Leonardo's to talk to each other. I wrote up the below transmitter and receiver sketches. The sender sends a byte every second, while the receiver lights up its LED and clears the buffer on receiving some data over serial.

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

void loop() {
  Serial.write(64);
  delay(1000);
}
char c;
const int ledPin = 13;

void setup() {
  Serial.begin(9600);
}
void loop() {
  if (Serial.available()) {
    delay(100);
    digitalWrite(ledPin, HIGH);
    while(Serial.available()) {
      c = Serial.read();
    }
    digitalWrite(ledPin, LOW);
  }
}

I hooked up the microcontrollers to the same 3.7V battery so they have a common ground and crossed over their rx and tx pins and I'm getting nothing. I've hooked each up individually to the Arduino IDE serial monitor and they both behave appropriately on their own, but when I connect them together via their rx/tx pins I get nothing.

I've spent a few hours looking for what I've done wrong and haven't come up with a fix. Any help appreciated.

Hi lilith

I hooked up the microcontrollers to the same 3.7V battery

I've hooked each up individually to the Arduino IDE serial monitor and they both behave appropriately on their own

The Leonardo needs a 5V power supply. When you connect each of them to the serial monitor (presumably via USB?) they get 5V power from the USB.

If you have two USB sockets available on your PC, try powering them both via USB as a test.

Regards

Ray

Also, try with placing the delay(100) after the setting of the LED on.
Otherwise it is LED on then straight away LED off before you can see anything.


Paul

  if (Serial.available()) {
    delay(100);
    digitalWrite(ledPin, HIGH);
    while(Serial.available()) {
      c = Serial.read();
    }
    digitalWrite(ledPin, LOW);
  }

You also should move your delay statement, assuming it is meant to keep the LED on for 100ms so it is visible. With only one character being sent per second, the while statement will end after reading one character, so there will only be a very short time between the LED going on and off.

You could also remove the while loop. You check that at least one character has been received in the if statement. You then do stuff with the LED and read and discard the character in the Serial.read. The whole if statement will be repeated the next time loop() is executed, so (in this example) the while loop is redundant.

getting a pair of Leonardo's to talk

I think those chips require a special serial start sequence or delay that I don't see in the code.

I resolved this! I just needed to use Serial1. functions for rx/tx pin serial as they aren't wired to USB serial on the leonardo.