I2C Communication with two Arduinos

Hello

I have problems getting a simple I2C Master-Slave-Comminucation working with two Arduinos. Sending an recieving one byte of data is no problem but I couldn’t get it working with an array of bytes.

Here is my code:
Master

#include <Wire.h>

#define SLAVE_ADDRESS 0x60

//Variablen für Empfangene Daten
int streammode = 1;
int streammin = 2;
int streammax = 150;
int relay1 = 1;
int relay2 = 0;
int relay3 = 1;
int relay4 = 1;
int relay5 = 1;
int relay6 = 0;
int relay7 = 1;
int relay8 = 1;
int techlevelsetpoint = 1000;
int maintenance = 0;
int settimeday = 1;
int settimemonth = 12;
int settimeyear = 14; 
int settimehour = 12;
int settimeminute = 50;
byte recvbuf[21];

//Variablen für Ausgehende Daten
int error = 0;
int timeday = 1;
int timemonth = 12;
int timeyear = 14;
int timehour = 12;
int timeminute = 30;
int relay1status = 1;
int relay2status = 0;
int relay3status = 1;
int relay4status = 1;
int relay5status = 1;
int relay6status = 0;
int relay7status = 1;
int relay8status = 1;
int snr1 = 500;
int snr2 = 50;
byte sendbuf[18];

void fillSendBuf()
{
	sendbuf[0] = (byte)error;
	sendbuf[1] = (byte)timeday;
	sendbuf[2] = (byte)timemonth;
	sendbuf[3] = (byte)timeyear;
	sendbuf[4] = (byte)timehour;
	sendbuf[5] = (byte)timeminute;
	sendbuf[6] = (byte)relay1status;
	sendbuf[7] = (byte)relay2status;
	sendbuf[8] = (byte)relay3status;
	sendbuf[9] = (byte)relay4status;
	sendbuf[10] = (byte)relay5status;
	sendbuf[11] = (byte)relay6status;
	sendbuf[12] = (byte)relay7status;
	sendbuf[13] = (byte)relay8status;
	sendbuf[14] = highByte(snr1);
	sendbuf[15] = lowByte(snr1);
	sendbuf[16] = highByte(snr2);
	sendbuf[17] = lowByte(snr2);
}

void readRecvBuf()
{
	maintenance = (int)recvbuf[0];

	relay1 = (int)recvbuf[1];
	relay2 = (int)recvbuf[2];
	relay3 = (int)recvbuf[3];
	relay4 = (int)recvbuf[4];
	relay5 = (int)recvbuf[5];
	relay6 = (int)recvbuf[6];
	relay7 = (int)recvbuf[7];
	relay8 = (int)recvbuf[8];

	techlevelsetpoint = word(recvbuf[9], recvbuf[10]);

	streammode = (int)recvbuf[11];

	streammin = word(recvbuf[12], recvbuf[13]);

	streammax = word(recvbuf[14], recvbuf[15]);

	settimeday = (int)recvbuf[16];
	settimemonth = (int)recvbuf[17];
	settimeyear = (int)recvbuf[18]; 
	settimehour = (int)recvbuf[19];
	settimeminute = (int)recvbuf[20];
}


void setup()
{
  Wire.begin(); 
  Serial.begin(9600);  
}

void loop()
{
  fillSendBuf();
  Serial.print("Sent: ");
  Serial.print(sendbuf[17], HEX);
  Serial.print("\n");
  Wire.beginTransmission(SLAVE_ADDRESS);    
  for (int i = 0; i<18; i++)
	{
		Wire.write(sendbuf[i]);   
                Serial.println(sendbuf[i], HEX);
	}  
  Wire.endTransmission();   
  delay(500);
  
  Serial.println("Requesting Data"); 
  Wire.requestFrom(SLAVE_ADDRESS, 21);

  int bytes = Wire.available();
  Serial.print("Slave sent ");
  Serial.print(bytes);
  Serial.print(" of information\n");
  for(int i = 0; i< bytes; i++)
  {
    recvbuf[i] = Wire.read();
    Serial.print("Slave Sent: ");
    Serial.print(recvbuf[i], HEX);
    Serial.print("\n");
  }  
  delay(500);
}

Slave

#include <Wire.h>

#define SLAVE_ADDRESS 0x60

//Variablen für Ausgehende Daten
int streammode = 1;
int streammin = 20;
int streammax = 150;
int relay1 = 1;
int relay2 = 1;
int relay3 = 1;
int relay4 = 1;
int relay5 = 0;
int relay6 = 0;
int relay7 = 1;
int relay8 = 1;
int techlevelsetpoint = 1000;
int maintenance = 0;
int settimeday = 1;
int settimemonth = 12;
int settimeyear = 14;
int settimehour = 12;
int settimeminute = 20;
byte sendbuf[21];

//Variablen für Empfangene Daten
int error = 0;
int timeday = 1;
int timemonth = 11;
int timeyear = 14;
int timehour = 12;
int timeminute = 0;
int relay1status = 0;
int relay2status = 0;
int relay3status = 0;
int relay4status = 0;
int relay5status = 0;
int relay6status = 0;
int relay7status = 0;
int relay8status = 0;
int sonar1 = 1000;
int sonar2 = 1000;
byte recvbuf[18];


void setup()
{
  Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
  Serial.begin(9600);
}

void loop()
{
}

void requestEvent() 
{
  Serial.print("Request from Master. Sending: ");
  Serial.print(x, HEX);
  Serial.print("\n");

  Wire.write(sendbuf, 21);
}


void receiveEvent(int bytes)
{
  Serial.println("Recieve Event");
  if(Wire.available() != 0)
  {
    for(int i = 0; i< bytes; i++)
    {
      recvbuf[i] = Wire.read();
      Serial.print("Received: ");
      Serial.print(recvbuf[i], HEX);
      Serial.print("\n"); 
    }
  }
}

When I send and recieve only one byte then everything is working great. When sending the array both arduinos seems to freeze. I tried several thing but don’t understand where the problem is…
Hope someone could help me.

regards Maik

I haven't used an array yet, but I was able to get this to work using strings.

Check out post #15 on this threat for working code. Maybe you can use the logic behind it to reverse engineer it to your needs. Like you, I have a little trouble figuring out how to what I think of as buffer.Until http://forum.arduino.cc/index.php?topic=278188.15

rather than a sequence of one byte writes try writting the array in one mywire.write(myarray,17)
untested code line check it for syntax but iirc write(variable,size) is the correct format.

@Thomas499
I first write the programm to send an array of char but changed it to datatype byte because there is a limit of 32bytes which the slave could send to the master. in the future there will be some more values which I have to send so sending bytes was for me more “elegant”

@filk
Testet it but with the same result :frowning: Master tried 5 times sending then freezes

cant see any begintransmission() and endtransmission() calls ??

#include <Wire.h>

byte val = 0;

void setup()
{
  Wire.begin(); // join i2c bus
}

void loop()
{
  Wire.beginTransmission(44); // transmit to device #44 (0x2c)
                              // device address is specified in datasheet
  Wire.write(val);             // sends value byte  
  Wire.endTransmission();     // stop transmitting

  val++;        // increment value
  if(val == 64) // if reached 64th position (max)
  {
    val = 0;    // start over from lowest value
  }
  delay(500);
}

But I already have them in my Master code if you look at the loop()

Get it working now :slight_smile:

The problem were the the Serial.print’s in the Slave recieve event function:

for(int i = 0; i< bytes; i++)
    {
      recvbuf[i] = Wire.read();
      Serial.print("Received: ");
      Serial.print(recvbuf[i], HEX);
      Serial.print("\n"); 
    }

When I comment them out:

for(int i = 0; i< bytes; i++)
    {
      recvbuf[i] = Wire.read();
      //Serial.print("Received: ");
      //Serial.print(recvbuf[i], HEX);
      //Serial.print("\n"); 
    }

It is working perfect :slight_smile: