Wednesday, December 25, 2013

Connecting Multiple Arduinos Using I²C

For certain applications, the speed Arduino's embedded microprocessor might be a little underpowered.  Owing to the low cost of the Arduino, it allows us to add Arduinos processors to various parts of the system to allow for parallel processing.  Such a setup is achievable using the standard I²C bus.  According to Wikipedia:
I²C (Inter-Integrated Circuit, referred to as I-squared-C, I-two-C, or IIC) is a multimaster serial single-ended computer bus invented by Philips used for attaching low-speed peripherals to a motherboard, embedded system, cellphone, or other electronic device.

Arduino Nano v3.0 Pin Layout
One thing to note is that, in general, when connecting any devices using the I²C bus, a pull-up resistor should be used.  In the case of connecting an Arduino, however, this is unnecessary as the Arduino has the pull-up resistor built in to its circuitry.  The concept is demonstrated using three Arduino Nanos which I picked up for $10 a piece (can't beat that!).  In the case of the Arduino Uno and Nano (and most, but not all, of the others) the I²C interface makes using of analog pins 4 and 5 which are used for the serial data (SDA) and serial clock (SCL) lines respectively.  

Breadboard Layout
We proceed  by placing three Arduino Nanos on a breadboard as shown in the diagram.  Because there is limited space on the breadboard, when the Nanos are inserted, the USB ports on two of the Nanos cannot be accessed.  These two nanos will be designated the slave devices and will communicate data to the master device (the Nanos which does have an accessible USB port).  This port will be connected to a USB port on my laptop to provide power to the Nano and also to allow for monitoring of the master Nano's serial port.  The two slave Nanos will receive their power from the master Nano.  In order to provide power to the slaves devices, the 5V+ and GND pins on the master is used to provide power and ground to the side rails on the breadboard to which the Vin and GND pins respectively of the slave devices are connected.  In order to provide the I²C interface connectivity, pin 4 on each of the Arduinos are connected together (white wire) and similarly pin 5 of each are connected (yellow wire).  

Three Nanos communicating via I²C.
All that remains is to upload the appropriate sketch to each Arduino and connect the USB cable to the master.  The sketches used are very simple and intended simply to demonstrate the ability to network together multiple Arduinos in order to allow for distributed parallel processing.  In order to use the I²C interface for communication of data, the Wire library provides the needed functionality.  The sketch we use is based upon the sample code given in the Arduino IDE, but has been altered somewhat to extend it to multiple slave devices.  A copy of the source code is available here.

The Nano in the middle of the breadboard is removed temporarily, connected to the laptop via USB has has the sketch slave_sender1 uploaded to it.  Similarly, the second slave device is connected to the laptop and sketch slave_sender2 is uploaded to it.  The USB cable is removed and the slave is replaced onto the breadboard taking care to replace it into the exact location from where it was removed.

Serial Port Monitor
Finally, the USB cable is connected to the master device and the sketch master_reader is uploaded to it.  If everything is working correctly, the two slave devices (which are ostensibly to be used as sensors) should each be assigned an address and should then report data to the master when asked.  In this case, each slave device will simply send its name ("Sensor1" or "Sensor 2" respectively) to the master device upon the master's request for data.  The code running on the master device simply sets up a loop that asks each of the slave devices in turn (by address) to send some data (9 bytes) to which the slave device will respond "Sensor 1" or "Sensor 2".  The master device then outputs the data it receives to the serial port.  If all is working correctly, then monitoring the serial port on the master device should show this.


2 comments:

  1. I can't download your code. Can send the code to me please? Send to adesurya.tkj@gmail.com
    Thanks

    ReplyDelete
  2. I also can't download your code. Pls give a link or send it to drkkwell-at-gmail-dot-com
    Thanks a lot

    ReplyDelete