Basic master slave. 3 X Mega boards

Hi all. First time here.

I am writing a program to control my trainset. I need 3 mega boards. I have had a quick read re linking the boards (I2C).

However I can’t find how to do just the basics.

I want the master to gather inputs only. Then the 2 slaves to read the master (or the master broadcast to the slaves).

Then I would think it’s easier to write these inputs to internal bits in each of the slaves then undertake Boolean and drive their own out puts. There is no need to do any read back to the master from the slaves.

Can anyone assist.

Ie the master reads the state of say 30 digital inputs. (digitalRead)

The slave(s) receive these inputs.
Convert them to an internal bit (I think this will then be easier to write the 1000 lines of code for each one)
Then drive there own digital out put (digitalRead)


Why complicate it with I2C? Mega has 4 hardware serial ports, use a dedicated port from master to each slave.

Ok. Not looking to complicate anything that sound good. . Very interested on more re this. Will do a bit more "googling"

And yes. Very open to assistance from here too. Many thanks.

Cheers. Ben.

Following from my first post. I can't find how to get a slave board to read inputs from the master board.

All of my inputs outputs a simple digitalRead. Digitalwrite.

The master mega board will have all the inputs. The 2 other Arduino mega boards will look at these inputs and do Boolean code to outputs on their own pins. No communication between the two slaves is required . No read back or backwards communication with the master. It is purely the input gatherer. They are purely outputs driving LEDs

Essentially. I am after the bit of code for instance that slave 1 will use to read an input on (for argument) pin 18 on the master as either high or low. Then use this code in its own Boolean calculation (I think making an internal bit once received will help.) (And any code the master will need to link to the 2 slaves

Many many thanks.

Ok, first, don't go scattering your topics - one discussion, one topic.

Let the boards communicate over the serial ports. Board 1 sends a message to Board 2 "Hey, send me your data" Board 2 replies "Okay, here's some data. Board 3 sends a message to Board 2 "Hey, send me your data" Board 2 replies "Okay, here's some data".

Now each slave has some data bytes they can work with to do whatever.

Many thanks. No worries with the posts.

I understand the broad logic behind the request. However. I have search for the pit of code. I can't find a basic set of commands for this.

I can find how to make an led blink from a computer on the second board. Or lots of stuff using char.

But I just want the code that sits in the first slave (board 2) that says.

Read input 18 (pin 18) from master (board 1) and tell me if it's high or low.

Is it anything like.

digitalRead(board 1,18).

I am totally lost. Most of the examples I can fine seem to complicated or want to do more that this.

Alternatively. How to read all 30 -40 inputs from the master as a block per loop then in the slave. Both slaves will need to do this. From the master. Many many thanks. Thanks for the prompt replies to date.

I think you need to describe the roles of the three Megas in more detail.

From what you have said so far I get the impression that the master Mega will read the value of lots of input pins and store the values in some variables.

Then either of two things can happen - A slave can send a request with Serial.print() requesting data from the Master. The master will then send whatever bytes of data are appropriate for that slave (again using Serial.print()). This implies that the master is continually listening for a request

OR the Master can send a message to a slave containing the appropriate data. This implies that the slaves are listening continuously.

It's not clear from your posts how much Arduino experience you have. The example several things at a time might be helpful to understand how to organize your program.


Many many thanks. The above has really helped

I have let's say above basic ardrino skills. However this master slave communication is still foreign to me. I have write a basic sketch for a single board. Below. To assist. Note there will be 30 + inputs. But for the example 1 will do. I can expend. I just don't know where to start with the master slave. Read


const int sw1Pin = 9; const int sw2Pin = 11;

const int ledPinR = 1; const int ledPinY = 3;

byte SW1 = LOW; byte SW2 = LOW;

int sw1State = 0; int sw2State = 0;

void setup() {

pinMode(ledPinR, OUTPUT); pinMode(ledPinY, OUTPUT); pinMode(sw1Pin, INPUT); pinMode(sw2Pin, INPUT); } void loop(){ sw1State = digitalRead(sw1Pin); sw2State = digitalRead(sw2Pin); if (sw1State == HIGH) { SW1 = HIGH; SW2 = LOW; } if (sw1State == LOW) { SW1 = LOW; SW2 = HIGH;

} if (SW1 == HIGH) { digitalWrite(ledPinR, HIGH); } if (SW2 == HIGH) { digitalWrite(ledPinR, LOW); } }

Now I want to instead of the led blink (turn on and off when sw1Pin is pressed) on the master I want it to blink on the slave when the button is cycled. (Internal bit SW1)

Ie. When SW1 (from master is HIGH - digitalWrite a pinX on the slave or use it in a new Boolean function in the slave sketch

I hope that clears up what I am asking. The hardest thing is to understand what the terminogy is.

I am using 3 arduino mega 2560 boards.

BenMel: Now I want to instead of the led blink (turn on and off when sw1Pin is pressed) on the master I want it to blink on the slave when the button is cycled. (Internal bit SW1)

Ie. When SW1 (from master is HIGH - digitalWrite a pinX on the slave or use it in a new Boolean function in the slave sketch

That sounds like it may not be all that simple. There will be an appreciable time in sending data from the master to the slave.

How far apart are your 3 Megas? There is nothing to prevent you connecting digital pins on one Mega to another Mega for virtually instantaneous communication over a short distance.

I like model trains. I've been using an "Arduino" to convert some N-gauge locos to battery powered radio control.

I think it would help if you explained what the different Megas are intended to do with your trains - i.e. a non-Arduino explanation of the project.


Hi. The 3 megas are next to each other.

If i can "share"or parallel the inputs that will work too. Do I need to have a common ground and common 5v they will all be feed from the same 12vdc bus.

I am using the ardunio to control the signalling. The 30 ish inputs are the state of the points and the tracks. Depending on the lay of the point and if a track is clear or occupied the signal will show a green red or yellow.

I originally was going to share the input but was toying with the master slave. If I can use the same input. Ie points 1 on pin 10 board 1 can also jump to pin 10 on board 2 well so that’s the solution.

Many thanks all. Once the code is done and the system working I will post some pics and videos.

Latest pic attached. The system has for an 3mtr board 200+ mtrs of wires. (Yes I am an Aussie) metric

If connecting all 3 boards. ie paraellel inputs or 1 boards output feeding directly to another's input do a need to connects the 5v and gnd of all 3.

Many thanks ben.

If not is their a solution

You will need a common ground, but not a common 5v.

I still don’t understand the relative roles of each of your 3 Megas.

Do you really need 3 Megas?

Maybe it would be easier to give each Mega responsibility for all of the activities for a section of the layout to avoid (or reduce to a very small amount) the need for inter-Mega communication.


Thanks robin. There are 150 input outputs.

Yes tried the common ground worked fine

Essentially each arduinos control a section. But for a few inputs that need sharing.

Please confirm if the circuit attached is correct.

I can paste the bit list and code once completed if people are interested. Attached is the layout.


That diagram looks like it should work.

If I understand it correctly, some of the inputs need to go to two or three Megas but most of the inputs only need to go to one Mega.

I think it would be better to use the internal pullup resistors for the input pins and use the switch to pull them to ground. It avoids the need for lots of external resistors and avoids having wires with 5v power running around the place.

Of course it does mean that when a switch is ON digitalRead() will return LOW rather than HIGH.