1
votes

I am trying connect multiple Arduino Mega Boards via their Serial pins to allow communication between the boards. I want to be able to connect an arbitrary amount of arduinos by daisy-chaning them and I want one board to be the master, taking control over the actions of the other boards. The master should be determined dynamically by the boards. I am aware that the daisy chaining method introduces delays to the communication due to the forwarding of packets, but so far I am planning on connection 4 boards at most. In the future this might increase to maybe 10 boards. My boards all have a separate power source, since they are connected to some other hardware which has its own power source.

My idea was to connect the boards in such a way, that the master would be determined by the wireing of the boards. I thought about having the "Serial" port as 'To-Master' serial port and the "Serial1" port as "To-Child" serial port. The boards send hello messages on the "To-Master" serial port and the master replies if it received such a message on the "To-Child" serial port. If no answer is received after some seconds, the board determines itself to be the master.

I wired the boards up by connecting the ground pins, and wiring RX1 of the master to TX0 of the child and TX1 of the master to RX0 of the child:

Wiring of the boards

Basically my setup is working, since the boards do detect each other and exchange hello messages and replies. There is however a significant amount of packet loss or corruption which I would like to eliminate.

As a simple measure of packet verification, I begin each packet with a "magic number". The receiving board looks for this byte and only tries to read a packet after receiving this byte. Any other bytes received are simply discarded.

As it seems, it happens quite often that something is received on either serial port that does not start with the magic number and is therefore discarded. The timestamps of these events are however consistent with the timestamps of sending of the other board meaning that the packet was at least partially transmitted but somehow the magic byte got corrupted or discarded.

Is this a known problem with the arduinos serial ports?

Can it be related to my wiring?

Are there any measures I can take to ensure a save delivery of the packets?

Can it be a problem of the boards not reading the signal at the correct time (I used a baud rate of 9600)?

I also looked into I2C communication, but I did not find any resource or information if it is possible to dynamically choose the master for this type of communication. Also in the documentation it stated, that it is important that all devices share a common power source which is not possible in my scenario. However, the basic master-slave principle of this I2C conforms with my requirements, as I have a master that sends commands to all other boards. Could I2C be utilized in my case?

Thank you for your thoughts!

2
Using a common bus, like I2C, requires a different kind of electrical interface to the bus. Making your scheme work requires a protocol, a reliable way to get packets of data from one node to another. Being able to detect the start and end of a packet so each node can interpret the specific fields inside the packet, like the node address, is very important. Having multiple masters requires something similar to a "token ring". Once used in networking as well but largely forgotten due to reliability problems.Hans Passant
Thank you. Actually I do not want to have multiple masters at the same time. I just want the boards to automatically determine one as master without the user having to explicitly select one.Tim
Hmya, that's a chicken-and-egg problem. In order to negotiate which one is the master, you do need the option to allow an arbitrary master. Maybe you ought to aim a bit lower, you are not close yet to making this work at all.Hans Passant

2 Answers

0
votes

Here is a discussion about multi-master I2C topology of Arduinos, seems that it is supported (haven't tested it myself). - http://forum.arduino.cc/index.php/topic,13579.0.html

You can test SPI as well, here is a comparison between the two - http://components.about.com/od/Theory/a/Selecting-Between-I2c-And-Spi.htm. Slave might be selected with generic GPIOs

I don't know any known implementation of multi-clients on top of Serial bus (usually it is intended for peer2peer communication only) - even though, your configuration seems reasonable, I would be considering other options.

BTW, from your comment about different power sources, I assume your boards are away from each-other. Have you considered very cheap ($2) RF modules, such as nRF24L01+ (http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/). THere is a library for networking those in multi-node network

0
votes

Might be better off with I2C or SPI like people have suggested here.

However, to address your question directly, it is most likely wiring. I am assuming you are using cheap jumper wires to plug directly into the Arduino Headers. Noise on this connection is the most likely problem or garbled serial messages. Try implementing with twisted pair cables and connecting directly to the board.

SPI or I2C might have better error correction than your customer serial protocol. I would see the other answers for that.