Connect/communicate between 2 Arduino Mega boards

Hello . I started a personal project and i need to connect 2 Arduino Mega boards together.
One should be the "brain" with the display and the other should be the "hands" that directly interact with sensors , relay, outputs ... etc and sends the sensor readings to the first one to take the decisions , so basically the first should be a type of remote for the second.
I was thinking to make it over serial port but i do not know how. I can also use a wireless type of connection between the two arduino but i prefer for the beginning a wired solution.

Any help / example would be highly appreciated.
Thank you in advance.

Connect pin 18 of one to pin 19 of the other and vica versa.

Within your setup() function set the baud rate for Serial1 on both boards something like this

Serial1.begin(115200);

Elsewhere in your code, to send a character use

Serial1.print(something);

To check for incoming do something like this

if ( Serial1.available() )
  someVariable=Serial1.read();

For debugging purposes you can switch out Serial1 to plain vanilla Serial, then use Serial monitor to see what's going on.

but this way both boards will be able to send and receive ?

SSerpente:
but this way both boards will be able to send and receive ?

To each other, yes.

SSerpente:
but this way both boards will be able to send and receive ?

TX is transmit.
RX is receive.

Serial data is 1 bit at a time on 1 wire for 1 direction, transmit or receive.

Mega has 3 extra serial ports (first one is to the USB chip) so RX1/TX1, RX2/TX2, RX3/TX3 labeled right on the board.

The more you know about the machine, the less mystery you will have about making it work.
Whatever you don't know, start from the start and learn enough through examples to have no questions.
The hard way is to have software with multiple unknowns while trying to learn basics at the same time.

SSerpente:
but this way both boards will be able to send and receive ?

Yes. But the program on each board will have to be properly designed to avoid the confusion that you get in a pub where everyone is talking at the same time. It would probably be easiest if one of the boards (call it boardA) was "in charge" of communication and would request data from the other. Then, in the normal course boardB would be waiting for a message from boardA. However that does NOT mean that boardB could do nothing except listen.

The examples in serial input basics listen without blocking other activities. I suggest you use the system of the 3rd example. You can use the same code on both devices.

...R

This what you need.

Robin2:
The examples in serial input basics listen without blocking other activities. I suggest you use the system of the 3rd example. You can use the same code on both devices.
...R

Thank you !!! Already started to play :slight_smile:

luxxtek:
This what you need.

EasyTransfer Arduino Library « The Mind of Bill Porter

This is also a great option that i will explore .. tomorrow.

I guess my problem is solved. A big thank you to all ...i am grateful for the help .

SSerpente:
Already started to play :slight_smile:

So obviously you realised that you also have to connect GND of each board together too. Happy diddling :slight_smile:

KenF:
So obviously you realised that you also have to connect GND of each board together too. Happy diddling :slight_smile:

No i didn't ... but it works without the common ground

SSerpente:
No i didn't ... but it works without the common ground

Perhaps you've been lucky that the GNDs are commoned some other way. Perhaps you're powering them both from the same supply?

If the two boards were getting their power from completely isolated sources (say from two different wall warts) it's unlikely to work UNLESS you connect those grounds.

are both using the same wall wart but different power supply.

SSerpente:
are both using the same wall wart but different power supply.

OK But if you change your power supply arrangements, you'll know where to look when if your communication gets flakey.

SSerpente:
are both using the same wall wart but different power supply.

Huh?

Same wall wart is same power supply. Is there other power supply(s) as well?
If both are connected to the wall wart power and ground then both have a common path to ground.

Just connect the GNDs between the two boards - whether you need it or not.

...R

GoForSmoke:
Huh?
Same wall wart is same power supply. Is there other power supply(s) as well?
If both are connected to the wall wart power and ground then both have a common path to ground.

by extension of what you say connecting 2 different power supply (AC/DC converter ) in 2 different wall warts (anywhere in the house ) in the end they have the same ground (because all the electric network of a house/city in the end have a single starting point from where thy split) ?
I am a complete noob ... but i was thinking every single AC/DC converter has his own + and - (+ and ground ) and i understood that i have to connect the 2 - poles of the power supply (or i got it wrong ?).

SSerpente:
by extension of what you say connecting 2 different power supply (AC/DC converter ) in 2 different wall warts (anywhere in the house ) in the end they have the same ground (because all the electric network of a house/city in the end have a single starting point from where thy split) ?
I am a complete noob ... but i was thinking every single AC/DC converter has his own + and - (+ and ground ) and i understood that i have to connect the 2 - poles of the power supply (or i got it wrong ?).

No. Your words before are not clear. If both boards connect to the same wall wart then they would share ground.

Yes, each power supply has + and - but only to itself. Unless the grounds are connected, the - of one may be + relative to the - of the other. Voltage is relative, not absolute. You may be lucky and they match or by some design they match perhaps if both are on the same house circuit and nothing strong happens between. What works by luck may fail by luck as well.

If you want to electrically isolate the boards then there are opto-isolators that can be put between the boards. The chip has per channel, a led and a detector with no electric connection. If you use those, you do not connect the grounds. But otherwise connect the grounds so all your circuits are on the same base.

thank you. I will do so .

P.S. Excuse my English .. by wall wart i understood wall socket (the 220V) not the power adapter . My mistake .

SSerpente:
thank you. I will do so .

P.S. Excuse my English .. by wall wart i understood wall socket (the 220V) not the power adapter . My mistake .

That wasn't my confusion. The bold part below seems to say both boards connect to one wall wart.

are both using the same wall wart but different power supply.

I've used the below simple test setup to use serial communications between two arduinos. Note that i used a diode between the master rx and slave tx to get bidirectional communication.

//zoomkat 3-5-12 simple delimited ',' string tx/rx 
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin. 
//Connect the arduino grounds together. 
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.print(readString); //prints string to serial port out
        Serial.println(','); //prints delimiting ","
        //do stuff with the captured readString 
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}