Category Archives: i2c

Arduino Master and Slave Using I2C

By : Antonius (sw0rdm4n)

http:///www.ringlayer.net

Fritzing

schematic

On more complex electronic or robotic application sometimes require more than 1 arduino as controller. Connection between 2 arduino where one act as a master and other act as a slave can be accomplished using i2c. On above schematic we connect each ground pin on each arduino. We use analog input 4 and 5 for i2c communication. Analog input 4 on master connects to analog input 4 on slave, meanwhile analog input 5 on master connects to analog input 5 on slave. For testing we connect slave to pc and we use 9 V battery for master to send command.

i2c

Arduino IDE Code

For master use this code:

/*
Simple master to send command
by : Antonius (sw0rdm4n)
www.ringlayer.net
*/
#include <Wire.h>
void setup()
{
   Wire.begin();
   Serial.begin(9600);
   Serial.println("Master Test Start");
}
void loop()
{
   Wire.beginTransmission(4);
   Serial.println("Sending command to slave");
   Wire.write('R');

Serial.println("Command Sent !"); Wire.endTransmission(); delay(2000); }

On Slave we use this code :

/*
Arduino slave test using i2c
by : Antonius (sw0rdm4n)
www.ringlayer.net
*/
#include <Wire.h>
void setup()
{
         Wire.begin(4);
         Serial.begin(9600);
         Wire.onReceive(receive);
}
void loop()
{
          delay(1000);
}
void receive(int X)
{
    char dataslave = Wire.read();

    Serial.println("\nGot command  :\n");
    Serial.println(dataslave);
}

Testing command received from master on slave (slave connected to pc) :

slave

n.b:

Actually i2c can also be use to connect arduino and raspberry pi, since it has different voltage we can use pull up resistors to prevent any electronic component to get burnt.

Thanks :

Indonesian Backtrack Team and Members ( www.indonesianbacktrack.or.id )

Cr0 Team (www.cr0security.com)

 

Advertisements