Passing serial data over I2C between two Arduino’s

Below you will find some arduino code for passing serial data over I2C between two Arduino’s. Parts of the following code were salvaged from various places around the internet.

8536568652_0500c802fd

This can also be used for:

1) General communication between two Arduino’s over I2C

2) Using a second Arduino to expand the peripherals, for example: add a a second serial port device, add a second arduino for dedicated monitoring of certain interfaces

I used this to communicate between a Diavolino@ 5V with a Arduino Pro @ 3.3V using the Sparkfun level converter TX lines. My attempts at trying to run this without the level converter using pullup registers tied to 3.3V did not work. I will reattempt that once I get a scope for debug.

I2C Master code:

#include <Wire.h>
// SCL - analog pin 5
// SDA - analog pin 4

int led = 13;
int debug = 0;

void setup() {
  pinMode(led, OUTPUT);     
  Wire.begin(); //join I2C as master
  Serial.begin(9600); //setup serial for input and output
  digitalWrite(led, HIGH);
  Serial.println("I2C MASTER 3.3V");
  delay(5000); // the master should be ready after the slave
  Serial.println("Ready");
}

void loop() {
  if (Serial.available()) {
    char inChar = (char)Serial.read(); 
    if (debug) {
      Serial.print("serial:");
      Serial.println(inChar);
    }
    Wire.beginTransmission(4);
    Wire.write(inChar);
    Wire.endTransmission();
  } 
  else {
    if (debug) Serial.println("!serial");
  }

  Wire.requestFrom(4, 1); // request 1 byte from slave device #4
  if (debug) Serial.println("waitOnReq");
  while(!Wire.available());
  char d = Wire.read();
  if (debug) {
    Serial.print(" reqData:");
    Serial.println(d);
    delay(50);
  } 
  else { // ignore special character ?
    if (d != '?') Serial.print(d);
  }

  if (debug) digitalWrite(led, HIGH);
  if (debug) delay(200);
}

I2C Slave code:

#include <Wire.h>

// SCL - analog pin 5
// SDA - analog pin 4
int led = 13;
int debug = 0;
void setup() {
    pinMode(led, OUTPUT);     
    Wire.begin(4);                // join i2c bus with address #4
    Wire.onReceive(receiveEvent); // register event
    Wire.onRequest(requestEvent); // register event
    Serial.begin(9600);           // start serial for input and output
    digitalWrite(led, HIGH);
    Serial.println("I2C SLAVE 5V");
    delay(2000); // the slave should become ready first
    Serial.println("Ready");
}

void loop() {
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
    if (debug) {
        Serial.print("rxEvent:");
        Serial.println(howMany);
    }
    while(Wire.available())
    {
        char c = Wire.read();
        if (debug) {
            Serial.print("i2c_rx:");
            Serial.println(c);
        } 
        else {
            Serial.print(c);
        }
    }
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
    if (debug) Serial.println("reqEvent");
    if (debug) digitalWrite(led, LOW);
    if (Serial.available()) {
        char inChar = '0'; 
        inChar = (char)Serial.read(); 
        Wire.write(inChar);
        if (debug) {
            Serial.print("sent:");
            Serial.println(inChar);
        }
    } else { //send some dummy data if no data is available from serial
        Wire.write("?");
        if (debug) Serial.println("!sent:");
    }
}
Advertisements

3 thoughts on “Passing serial data over I2C between two Arduino’s

  1. hi! i am using two pic controller interface with i2c… one master another slave. i got success i2c between pic and eeprom. eeprom has slave address. but my question is that how can i know address of slave controller…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s