3

I was wondering how I can get 4 Arduinos in total to communicate with each-other through a wired connection. The goal is to have the master Arduino send a number 0-100 to each of the other Arduinos.

The master unit will be an Arduino Uno and the other 3 Arduinos will be Arduino Pro Minis.

3
  • 3
    There are lots of ways. You're going to need to be more specific about your requirements. Commented Mar 30, 2015 at 17:10
  • how to communicate multiple ardunio wirelessly?? Commented Jul 18, 2018 at 7:44
  • @SHAUKATALIKHAN lookup NRF24L01 with radionics Commented Jul 18, 2018 at 12:48

5 Answers 5

3

You could use: U(S)ART, I²C or SPI. You should check this post for information on these protocols.

Some quick googling gave me this:

  • SPI: mainly for high data rates, approx 25 MHz clock but only bus has to be on same voltage level, single master (can be done through software), data arbitration not possible.
  • I2C: mainly for control and low data transfer (limited by bus capacity, etc...), but can be interfaced to different voltage levels and multimaster bus environment, inbuilt arbitration etc...
  • UART: single P2P comm, 115 kbps max.

http://www.edaboard.com/thread53139.html

I don't fully agree with it. You can send via UART to multiple devices, but then you have to handle the addressing in software. But other than that, UART is very easy to debug as Arduinos often have an USB connection.

"I²C is somewhat complicated at the physical level, but all that complication is handled by the hardware (the TWI interface of the AVR chips). At the user level, it may be the simplest to use". I don't yet have a lot of experience with this protocol, but if you want to use it, check out the other answer.

SPI would be a fair shot as it does enable master with multiple slaves in its standard.

Summary:

All protocols have different pros and cons. It's not really up to us to choose one for you. And you should look them up and learn about them by yourself.

Then try to choose which one applies most to your needs, and get started with that protocol. They are well documented and if you really can't get out of it, you can post another question ;D

5
  • 1
    I²C is somewhat complicated at the physical level, but all that complication is handled by the hardware (the TWI interface of the AVR chips). At the user level, it may be the simplest to use: only two wires and no need to match port configurations (baudrate, parity, stop bit(s)...). Commented Mar 31, 2015 at 20:23
  • Ah, I see, haven't yet used I²C with arduino, but for a beginner, but in general it's better to know the hardware before you start on the software. In this way you know what to do and how to debug it. But I can imagine that arduino has steady/easy libraries for this. But same is for setting up serial connection in arduino?
    – aaa
    Commented Apr 1, 2015 at 6:16
  • UARTs don't need to be single P2P; AVRs do support a form of multiple slave using a 9-bit protocol. Commented Apr 1, 2015 at 11:25
  • I also said that in the 3rd 'paragraph', though adressing isn't standard in the uart protocol? Or well you will have to handle it software. With SPI you simply have a slave select pin that selects which slave to talk to. I didn't know that 9-bit protocol is for adressing? Or well I don't believe it's defined so in the standard? But yes, an extra bit could help. You could also send one byte with the address/setting and then send (an)other byte(s) with the message. Then on each slave you should check the first byte, and only handle the second when neccesary.
    – aaa
    Commented Apr 1, 2015 at 11:55
  • I agree knowing the physical layer can help, but for using I²C you only need to know that addresses and data are multiplexed on the same lines, which simplifies the cabling (only 2 wires). On the software side, it's really simple: see the Master Writer/Slave Receiver Arduino tutorial, which can be trivially expanded to many slaves just by using different slave addresses. Commented Apr 1, 2015 at 15:45
2

You can use the I2C communication protocol through the wire.h library. That will permit you to use the arduino uno as master device and the pro minis as slave. Briefly, you use a two wire's connected to the SDA and SCL pins of all the arduino board and then the master to interact ( meaning request data and send data) with the slaves uses there address (that is user defined in the slave software). So the normal routine in I2C goes like this:

  1. The master sends the address plus the read write instruction on the bus.
  2. All the arduino boards listen and if the address matches with there internal adress they perform the instruction.
  3. If it is write command from the master the slaves prepare to receive. If it is read command from the master, the master waits until the slave send a byte.

Fr more information go on:

Arduino two wire communication

I2C official datasheet

1
  • And you are probably using I2C already to interface with sensors, displays and other components. I
    – user31481
    Commented Nov 18, 2017 at 8:13
2

You can use RS-485 connection between all arduinos using serial port of all. For a half duplex connection for 4 arduinos, the setup can be like this:

What Do You Need:

  • 4 RS-485 line drivers (like max485). One for each arduino
  • if the distance between arduinos are short (under 1 mt) these will br enough
  • if the distance between arduinos are long (over 2mt) you'll need to use at least 24 AWG twisted pair cable between them and 2 120 ohm resistors. These resistors will be line terminators.
  • You can check max485 datasheet to see how to connect them

Setting Up:

To use rs485 with arduino and half duplex, you'll need 3 pins. 2 pins for rx and tx. 1 pin for transmit-enable. In half duplex mode, normally all nodes are in listening mode. To configure transmit enable, connect "drive enable" and "receive enable" pins of rs485 driver together. This will be our "transmit-enable" pin.

Connection:

      |        |
c  RX |------->|RO     4
p  TX |------->|DI     8
u D13 |------->|TE&RE^ 5
      |        |

Software:

Download this library (Mertbus). This library implements a basic transport layer. It is in alpha stage but functional. There is sample code in it and also a .net library.

The logic is similar to TCP/IP. Give every node an address between 0 and 126. 127 is the broadcast id. Means if you send data to id 127 all nodes will receive it. If you send data to only one node (ex. 2 ...) only target node will receive.

Sample:

#include "MertBus.h" //include header

#define TXEN 13  //transmit-enable pin
#define SELF_ID 1  //id of this node

MertBus mb;  //communicator class

void setup() {
   Serial.begin(57600); //prepare the serial port as usual
   Serial.println("Begin init");
   //then initialize mertbus instance with port, pin and id
   mb = MertBus(Serial,TXEN,SELF_ID); 
}

void loop() {
  if(mb.checkData()) {    //check if and valid data received
    // reply to sender char[] and length, 26 chars
    mb.reply("Incoming Data (SendData): ",26); 
    // reply to sender the received data
    mb.reply(mb.Buffer,mb.ReceiveCount);
  } else {
    digitalWrite(13,HIGH);
    delay(400);
    digitalWrite(13,LOW);
    delay(400);
  }
}

To be a master, to send data (without replying to a message, starting a new one):

//buffer data to send, to_addr is target node address 
//buffer_size is the size of data to send. 
mb.sendData(char * buffer, uint8_t to_addr, uint8_t buffer_size)
2

If they are reasonably close to each other, by far the simplest would be to use serial comms, since you say you want to send a number (but do not mention receiving anything).

You could have the Tx pin on the master Arduino connected to the Rx pin of all the other Arduinos (plus a ground connection of course). Then send your numbers to your heart's delight. I suggest some sort of protocol like:

(42)

Where the opening and closing braces indicate to the receiver where the number starts and ends. If the distance is more than (say) 5 meters, an RS485 transceiver may be called for at each end, eg.

RS485 connections

I have a description of the sort of thing on my RS485 communications page.

You can "drop" multiple transceivers off the same pair of cables.

RS485 is reasonably resistant to line noise because it is a balanced protocol.

These transceivers can transmit or receive but not at the same time. In your case you would have the master with Tx Enable active, and the slaves the Tx Enable not active.

If the chips are dropped off a long cable run you should have a termination resistor between A and /B at the end (say, 120 Ω).

SPI could be OK, but is really for short runs. I2C could be OK too, but may be unnecessarily complex. However if you go with I2C it does have a "broadcast" mode which might suit sending the same thing to multiple devices.


References

2
  • Nick, what did you use to generate those circuit diagrams?
    – Greycon
    Commented Jul 10, 2015 at 16:37
  • ExpressSCH from expresspcb.com. It's free software for Windows that draws schematics and PCBs. It's simple to use (which is why I like it). It has a library of major parts, plus you can make your own out of boxes and lines.
    – Nick Gammon
    Commented Jul 10, 2015 at 20:57
1

PJON is a bit banged serial bus protocol for multiple nodes. You may find it useful.

PJON™ (Padded Jittering Operative Network) is an Arduino compatible, multi-master, multi-media communications bus system. It proposes a Standard, it is designed as a framework and implements a totally software emulated network protocol stack that can be easily cross-compiled on many architectures like ATtiny, ATmega, ESP8266, Teensy, Raspberry Pi, Linux and Windows x86 machines. It is a valid tool to quickly and comprehensibly build a network of devices

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.