Serial Communication Between Arduinos Requires USB?

Hi

I am trying to send a byte from one arduino (Mega) to another (Uno) using the Serial Ports on each. My code (below) works when each arduino is plugged into the computer via USB, but not when one or both are powered by external power supplies. I'm guessing that the Serial ports communicate directly with USB and that why that doesn't work. Is there a work around to this problem? If I'm just sending a byte of information can I connect analog pins on each together and just write and read the value that way? What is the easiest way to do this?

Mega

int desiredRPM=3000;
byte Norm_desiredRPM;
void setup() {
  Serial3.begin(9600);

}

void loop() {
  Norm_desiredRPM=desiredRPM*.02125;//Sets value to 255 for 12000rpm and 0 for 0 rpm.
  Serial3.write(Norm_desiredRPM);
  Serial.println(desiredRPM);
  Serial.println(Norm_desiredRPM);
delay(1000);
}

Uno

byte Norm_desiredRPM;
int desiredRPM;
void setup() {
  Serial.begin(9600);
}

void loop() {
  if(Serial.available()>0)
  {
    Norm_desiredRPM = Serial.read();
    desiredRPM=Norm_desiredRPM/.02125;
    Serial.print("value received: ");
    Serial.println(Norm_desiredRPM);
    Serial.print("Processed: ");
    Serial.println(desiredRPM);
    delay(1000);
  }
}

On the UNO you only have 1 Serial port, pins 0 and 1 are connected to the USB... So how did you do the wiring?

Regarding this line of code    Norm_desiredRPM = Serial.read(); I suggest you read again the documentation for the [url=https://www.arduino.cc/en/Serial/read]Serial.read[/url]() function to see what it returns and study Serial Input Basicsfor best practices

You should probably use SoftwareSerial on your Uno to create another serial port on two other pins. That way you leave Hardware Serial free for uploading programs and sending debug messages to the Serial Monitor.

Serial communication should work perfectly well without either Arduino connected to a PC. But you do need to connect the GNDs of the two Arduinos.

...R

hi, why don't you use the wire.h library to send from one arduino to another.

It must be possible because I communicate between thousands of Unos using two SoftwareSerial ports on each and external power. I use the hardware serial port for debugging.

Hi all. So my connections are UNO Rx Pin 0 to the Mega Tx Pin 14 and the UNO Tx Pin 1 to the Mega Tx Pin 15. Upon uploading the code to each and observing the serial monitor for the Uno, I get no values unless the Mega is plugged into the computer with a USB.

Sounds like I need to use Software Serial to avoid the 0 and 1 pins on the UNO. Can I use Software Serial on any other two pins for the Uno?

Did you connect the grounds? You need to; if they're both connected to the same computer by USB, the grounds would be connected that way, explaining why it works when both are connected to USB.

Hmm.. Yeah I didn't do that. Let me try.

Thanks all!

You mega code appears incomplete. There is no Serial.begin(9600); in Setup, so it isn't clear how this worked before.

Are the serial comms wires kosher ? Rx>Tx and Tx>Rx?

I see no point in using software serial on the Uno for this purpose, it's just a red herring.

I see the expected

  Serial3.begin(9600);

in the Mega code.

Nick_Pyner: I see no point in using software serial on the Uno for this purpose,

I see no point in not using software serial, because the tx and rx pins are connected to the USB serial chip.

I said for this purpose. And having the serial pins connected to the USB chip is not a reason. Besides, if the OP actually needed to use software serial, it is something he would have found out already. As things stand, OP can do all his uploading and dubugging to and of Uno via hardware serial in the normal and sensible manner, even thereby absolving the Mega from being part of that process - which I imagine is exactly what he has done.

And I see the unexpected

  Serial.println(desiredRPM);
  Serial.println(Norm_desiredRPM);

in the Mega code, hence my comment about the code looking incomplete.

Nick_Pyner: And having the serial pins connected to the USB chip is not a reason. ... OP can do all his uploading and dubugging to and of Uno via hardware serial in the normal and sensible manner...

If the Mega talks while uploading to the Uno the upload will probably fail.

So disconnect it. How hard is that?

Whandall: If the Mega talks while uploading to the Uno the upload will probably fail.

So disconnect it. How hard is that?

For me?

I don't have to do it, so it's very easy.