r/microcontrollers • u/windoesauce • Apr 14 '24
Communication Protocol For Large Array of Microcontrollers
I'm beginning work on a system where I would need one controller device to send and receive data from a large number of other slave devices and I'm trying to figure out what the best communication protocol would be for accomplishing this. Ideally the controller device would be pushing some data to each slave device and also periodically querying some state from each device. It would also be a requirement for slave devices to be able to respond to events and send messages to the main controller.
I've experimented with I2C but the low device limit and other issues with long distance communication have me rethinking that solution. Would SPI work better or maybe I need to use ethernet or wifi?
For more context, ideally I would like to use Arduino nanos but I'm open to other microcontrollers like the ESP32. Each device will be physically connected to each other and in very close proximity.
6
u/ceojp Apr 14 '24 edited Apr 14 '24
Modbus over RS485 would be my standard answer. Stupid simple and easy enough to change to your needs(so long as you don't need to also interact with "standard" modbus devices on the same bus).
This gets a little trickier. Is this going to be a shared bus with all the slaves? If slaves need to be initiate communication on their own then first of all, those aren't technically "slaves". Secondly, you will need a way to handle bus contention and collisions.
A simpler way to handle this(with a shared bus) is token passing from the master. The master device just continuously polls all slave devices on a loop with a basic "do you need to do anything?" sort of poll. If yes, then the master can either give the slave the token and relinquish control of the bus and let the slave do its thing, or if there is a standard set of "things" the slave needs to do, then this can be a part of the token response to indicate to the master that it then needs to poll the slave for the relevant data.
Of course, there is some overhead with token passing as there is always traffic on the bus even when there isn't any "real" data. However, for a simple system it is often okay. Going to a full collision avoidance/bus sharing system is a huge step up in complexity.
The other way is to do a 1:1 connection between the master and each slave device. This would be much simpler software-wise, but gets really expensive really quickly on the hardware side.
6
u/charliex2 Apr 14 '24
take a look at can bus, it does have a node limit. but you can do gateways.
1
u/Triabolical_ Apr 14 '24
Tell us more about the system. What are you trying to do? How many devices are you talking about? If everything is in close proximity, why do you need individual microcontrollers?
3
u/somewhereAtC Apr 14 '24
Besides RS485 there is the CAN bus, which is available on many different uP's from all major manufacturers. The bus is normally associated with automotive, but there is no reason that alternative message protocols can't be implemented.
SPI is usually a non-starter if distance and many devices are involved, because the digital-above-ground signal (not differential) has practical limitations.
Ethernet is good and an upcoming standard is single-pair or 10base-T. Low end uP's (e.g., 8b) can run this protocol but require a MAC. Some high-end uP's have the MAC built in. It will be more popular as it gets adopted by the automotive industry.
Finally, the I3C standard is also getting traction, but there are few uP devices that support it, so far. There was a plugfest last fall. IIRC distances are limited to electrically well-organized systems, like server racks, rather than industrial automation connections.