Electronic – arduino – multiple arduino communication (1 master, n slaves)

arduinocommunicationi2cserialslave

I would like to develop a master/slave network that consists of:

  • 1 Arduino master that reads sensors and generates velocity ramp profiles based on the sensor signals and then sends those ramps to slaves

  • 3 (or more) Arduino slaves that control the velocity of 12V servo motors following the ramps sent by the master

What is a good communication protocol to achieve this? Serial (SPI)? I2C? Something else? If it is serial, is the new Arduino Leonardo a good choice? What issues should I be considering in selecting a protocol?

I'm imagining something like:

Master:

void loop() {
    update_ramps()
    for(int i=0; i< num_slaves; i++) {
        send_to_all(i, ramps[i]);
    }
}

Slave 1:

const int id = 1;
int recived_id, recived_value;
void loop() {
    read_data();
    if(recived_id == id) { 
        do_motor_step(recived_value);
    }
}

And serial communication in which RX/TX from the master is sent to all slaves.

Does this seem like a reasonable solution?

Best Answer

As I understand it you want to send different data to each of the slaves, but the slaves don't have to send data back.

I2C is an addressed bus, so if you assign a different I2C address to each of the slaves you'll need only two wires to send the data. If needed you can ask data back as well. The Arduino's AVRs have an I2C compatible serial bus. And you can extend to more than 3 slaves without extra hardware, up to a maximum of 127.

UARTs don't have addressing, so you would need either 3 UARTs (which the AVR doesn't have), or add external logic to switch between UART lines (which costs money). Each additional slave means extra cost. Not recommended.
edit
Like Chris says you can use UART to create a multidrop bus. And then you'll have to add addressing, which makes your UART work a bit like I2C, but then asynchronous, and without address matching hardware like the I2C has. So still not really an advantage. end of edit

SPI also uses shared lines for data: a single MOSI, and the MISO lines connected. To address each slave individually you'll need one SS (Slave Select) line per slave. So that's at least 5 I/Os: MOSI, SCK, 3 \$\times\$ SS, and MISO if you also want to read data from the slaves. Each additional slave adds 1 I/O pin on the master.

enter image description here

I think the I2C is the best solution, requiring the least number of wires. The protocol is a bit more complex than UART or SPI, but since the AVR has the hardware for it, it should be easy to use.