Serial transfer between 2 Arduinos

There is Arduino Mega2560 and AT328P chip on a breadboard. I try to send and receive characters between them, but fail: the receiving program in Mega2560 waits forerver.

Mega2560 sends and receives succesfully, when rx0 is connected to tx0 of its own (digital pin 0 to pin1).
Breadboard chip sends and receives succesfully, when rx is connected to tx of its own (digital pin 0 to pin1).

There are the following connections between them:

  • ground to ground
  • 5V to 5V
  • rx0 of mega to tx of breadboard chip
  • tx0 of mega to rx of breadboard chip

Mega2560 gets its power from USB.

The echoing program in Mega2560 is like this:

// serial port echo
void setup(){
  Serial.begin(115200);
}

void loop(){
  char c1; 
  while(Serial.available() <= 0){}
  c1 = Serial.read();
  Serial.write(c1);
}

(actually there was some lead blinking code above, which I removed to make things simpler for readers)

The program in breadboard sends and receives the same character all the time:

// serial port feedback test. tx connected to rx
// if led remains on, nothing is received
// 1 sec blinking: wrong character received (and blink forever)
// fast blinking: ok
// tx and rx tied together or another mcu has serialecho pgm
long int baudrate = 115200; //2400;
const int ledpin1 = 8;

void setup(){
  Serial.begin(115200);
  pinMode(ledpin1, OUTPUT);
}

void loop(){
  char c1 = 'a'; 
  char c2;
  digitalWrite(ledpin1, HIGH); // on, if reading ok
  Serial.write(c1);
  delayMicroseconds(100);
  while(Serial.available() <= 0){}
  digitalWrite(ledpin1, LOW);
  c2 = Serial.read();
  if(c1 != c2){
    while(true){
     //blink(1, 500);
    }
  }
}

The breadboard AT328P is on the left of the image (to the right there is Bluetooth circuits which are not receiving/sending during the test).

Similar programs work as expected, when the breadboard chip is replaced with an Arduino Uno. Even bluetooth-- Mega2560 work well .
The chip AT328P runs other programs well, blinks leds and does analog conversions as expected.

I have no real oscilloscope or logic analyzer available now and start feeling frustrated when I cannot even imagine, what could be wrong. Ideas?

I believe you may be seeing conflict with the hardware serial port on the Mega.

The Mega already has serial port hardware connected to pins 0 and 1, so connecting the '328 tx to its rx, for example, causes a fight between the '328 and the usbserial port chip over the voltage on that line. Not pretty.

Fortunately the Mega has multiple hardware UARTS. I believe you can use Serial2 on a different set of pins. Search for Serial2.begin()…

-br

Thanks Billroy, that helped me a step forward.
Now Mega2560 receives the byte 'a' sent by the breadboard, and transmits that back, many times, from Serial1.

But there is something strange still with the circuit: the Mega2560 is echoing bytes (marked as a square) in a slower rate even when I am keeping the resetbutton of the breadboard down! I continue to test this.

I did not know that using rx, tx (pin 0 , pin1) for other serial transmission than USB connection to host might not be good. Can we not "branch" serial lines, if we use only one branch at a time? I thought that if I send nothing from the host computer (from the monitor window) to Mega2560 then it can receive from the breadboard without problems through pin 0. And when Mega2560 transmits, then both the USB host and the breadboard chip get the transmitted byte.

I wonder if using pins 0 and 1 really caused the problem especially when UNO in place of breadboard worked well (through its pins p0 and p1).

But anyhow, now it is easier to continue testing. Thanks!

I

It would be nice if ttl serial worked they way you imagine, but I'm afraid it doesn't. Connecting two serial tx drivers to the same serial rx input is like crossing the streams in Ghostbusters. Don't do it. Each driver is willing to source its full current trying to keep the line at its opinion of the correct level. Important safety tip.

Fortunately you are working on a mega so you can continue with hardware serial support. On a 328p you'd be using software serial...

As for your new problem, I don't fully understand the symptom. Can you amplify?

-br

billroy:
It would be nice if ttl serial worked they way you imagine, but I'm afraid it doesn't. Connecting two serial tx drivers to the same serial rx input is like crossing the streams in Ghostbusters. Don't do it. Each driver is willing to source its full current trying to keep the line at its opinion of the correct level. Important safety tip.

Fortunately you are working on a mega so you can continue with hardware serial support. On a 328p you'd be using software serial...

As for your new problem, I don't fully understand the symptom. Can you amplify?

-br

I believe you, very good that you told that so clearly. It might be like tying 2 output pins together. They might end up sourcing/sinking large currents in their fight.
The remaining problem might be such that I have damaged the rx, tx pins already and the system behaves strangely therefore. But that is not urgent to solve and might be difficult/unnecessary to explain. When I get new chip 328P I continue with that.