Arduino to Arduino Communication

Okay so I'm making a large security system using arduino Megas - it's so big that I can't control it using just one and at this point I have 4 Megas in the system - I usually use relays and pull-up resistors and digitalRead but on this scale the amount of wires needed makes it very inefficient - I need a new way to allow the arduinos to communicate - all 4 must be able to communicate with each other - they can be simple transmissions of one letter or number

For example if a fingerprint is accepted one arduino must tell the central one that it is accepted and the central one must tell the lcd controller to display that it was accepted

Thanks for the help!

OK, so you have a "central" Arduino which can control the communication with the "slaves". Then it's just a matter of picking an electrical bus which will do what you want it to do and programming the central one and the slaves.

If you have the Mega, then you have 3 extra serial TX/RX pairs, so your central one can talk to all three slaves. If you are able to change your wiring to use them, then you have a relatively simple solution.

If you think you might have more than 3 slaves in the future, then I2C is the solution.

I tried looking up how to implement both those methods but didn't come up with much

Can you show me how to do both of those?

I think the main difference between the two options is: Using serial communication, each slave can initiate a communication. Using I2C, the master must request info from each slave.

The I2C will require less wires/pins, since it is on a bus.

Okay I think serial sounds like the best option? How do I go about creating a program that can use a Mega's multiple rx/tx pins to communicate with the slaves and have the slaves send the master needed info?

Thanks!

In the Arduino examples, under 04. Communication, look for MultiSerialMega. This should get you started. Plug two in, RX to TX and TX to RX and see what you get on the serial monitor. (You will need to start the Arduino IDE twice to get two different instances that are using two different serial ports.)

Do you need to send info the the slaves, or just listen for info from the slaves?

If you have a central controlling arduino and a number of slaves, then you may be able to get by with only using one tx/rx pair with diodes on the slave arduino tx pins.

The slaves have to communicate to the central and the central has to communicate some info to the slaves - but if I have to I can make it so only the slaves talk to the central

In a master slave situation make it so only the master can initiate communications in that way you avoid bus conflicts where two slaves try and talk at once. A sort of speak only when spoken to arrangement.

To add a little bit to what @Grumpy_MIke has said ...

If you are using Serial for communication you need to realize that the serial lines idle HIGH which means that if two Tx lines are connected to one Rx line the idle one will interfere with the active one.

You can get around that by using diodes oriented to prevent each Tx line from pulling Rx HIGH and using an external pullup resistor at the Rx pin. That way an active slave can pull Rx to GND and the resistor can pull it HIGH but an idle slave won't have any effect because the diode will block its HIGH. (Hopefully someone else will verify or correct this)

The master Tx can be connected to several slave RXs without interference

...R

and using an external pullup resistor at the Rx pin.

There appears to already be an internal pull up resistor performing this function, so another resistor is probably not needed.

There appears to already be an internal pull up resistor performing this function, so another resistor is probably not needed.

Is the internal pull up resistor on the rx or tx pin ?

So then it sounds logical that the tx will not pull the line high, just release it from being low (open collector style)?

Is the internal pull up resistor on the rx or tx pin ?

Most likely both.

So then it sounds logical that the tx will not pull the line high, just release it from being low (open collector style)?

In a resting state, both tx and rx are high. The tx goes low pulling the attached rx low to signal a start bit to the rx pin. Use a multimeter to check the resting voltage on the tx and rx pins.

UART is tricky because two TX pins cannot be normally tied together. They will not get damaged but will garble communication. I say normally, because it's possible to do it by disabling transmitter (making it input) and enabling it only during transmission, but you would need to modify HardwareSerial library.

Second option that does not require HardwareSerial modification is multiplexer, e.g. 4051, 4052, for example Atlas Scientific uses 4052 to multiplex UART from master to up to 4 slave sensors. If you implement addressing, you only need to multiplex TX pins and all nodes will receive all master's traffic but only addressee will be able to respond. That can get you 8 slaves with 4051

I2C is multi-drop bus from the start. No additional hardware and software beyond what you already have needed for what you need to accomplish. Disadvantage from UART is that uses passive/active levels (vs active/active on UART) so transmission distance will be lower, but if nodes are in close proximity the I2C is the way to go.

If your distance is greater, RS485 or NRF24L01 would be way to go. Unfortunately Arduino HardwareSerial does not have RS485 support.

Lots of good info here, I learned from it.
With all the info, I would suggest one of two choices.
If you want to use serial, don’t try to combine wires, use the three serial ports available.
If you want to use I2C, you can have more devices connected if you think you would ever need that later.
Just my thoughts.

zoomkat: There appears to already be an internal pull up resistor performing this function, so another resistor is probably not needed.

I was not / am not / sure if this is set when using Serial

...R