# I2C Help

**URL:** <https://community.sparkfun.com/t/i2c-help/22447>\
**Category:** Arduino\
**Created:** [November 8, 2011, 3:56pm UTC](https://community.sparkfun.com/t/i2c-help/22447 "2011-11-08T15:56:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjscar](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@mjscar](https://community.sparkfun.com/u/mjscar)\
**Post date:** [November 8, 2011, 3:56pm UTC](https://community.sparkfun.com/t/i2c-help/22447/1 "2011-11-08T15:56:14Z")

</div>

Good day ladies and gents,

I was wondering if you could kindly please help me with a big problem I am facing at the moment. I am trying to get a few arduinos to connect to each other through I2C but I am not getting the results I wanted. But before I go into the coding, I will give a run down of my goal.

#There’s 4 slave arduino, each connected to 4 LDRs.

#The slave simply sends the LDR analog reading to the master.

#The master receives the analog reading from each arduino.

#The master executes a few if commands, such as

if analogreadingslave11 \> analogreadingslave 23 // ie the reading from slave1LDR1 is greater than slave2LDR3

…so and so…

So in essence, its a pretty simple idea of sending analog readings from 4 arduinos to the 1 master. But I keep getting unexpected results. So without further ado, here are my coding.

**Master**

```auto
int sensorReading00;
int sensorReading01;
int sensorReading02;
int sensorReading03;
int sensorReading10;
int sensorReading11;
int sensorReading12;
int sensorReading13;
int sensorReading20;
int sensorReading21;
int sensorReading22;
int sensorReading23;
int sensorReading30;
int sensorReading31;
int sensorReading32;
int sensorReading33;
 
#include <Wire.h>
 
void requestSensor(char address, int* sensor0, int* sensor1, int* sensor2, int* sensor3)
{
  byte data[] = {0, 0, 0, 0, 0, 0, 0, 0};
 
  Wire.requestFrom(0x30, 8);
 
  byte c = 0;
 
  while (c < 8)
  {
    if (Wire.available())
    {
    data[c] = Wire.receive();
    c++;
    }
  }
 
  *sensor0 = (((int) data[0]) << 8) + data[1];
  *sensor1 = (((int) data[3]) << 8) + data[3];
  *sensor2 = (((int) data[4]) << 8) + data[5];
  *sensor3 = (((int) data[6]) << 8) + data[7];
}
 
void setup()
{
  Wire.begin();
  Serial.begin(9600);
}
 
void loop()
{
  requestSensor(0x30, &sensorReading00, &sensorReading01, &sensorReading02, &sensorReading03);
  requestSensor(0x31, &sensorReading10, &sensorReading11, &sensorReading12, &sensorReading13);
  requestSensor(0x32, &sensorReading20, &sensorReading21, &sensorReading22, &sensorReading23);
  requestSensor(0x33, &sensorReading30, &sensorReading31, &sensorReading32, &sensorReading33);
 
  Serial.print("Analog reading00 = ");
  Serial.println(sensorReading00); // the raw analog reading  
  Serial.print("Analog reading01 = ");
  Serial.println(sensorReading01); // the raw analog reading
  Serial.print("Analog reading02 = ");
  Serial.println(sensorReading02); // the raw analog reading
  Serial.print("Analog reading03 = ");
  Serial.println(sensorReading03); // the raw analog reading
  Serial.print("Analog reading10 = ");
  Serial.println(sensorReading10); // the raw analog reading  
  Serial.print("Analog reading11 = ");
  Serial.println(sensorReading11); // the raw analog reading
  Serial.print("Analog reading12 = ");
  Serial.println(sensorReading12); // the raw analog reading
  Serial.print("Analog reading13 = ");
  Serial.println(sensorReading13); // the raw analog reading 
  Serial.print("Analog reading20 = ");
  Serial.println(sensorReading20); // the raw analog reading  
  Serial.print("Analog reading21 = ");
  Serial.println(sensorReading21); // the raw analog reading
  Serial.print("Analog reading22 = ");
  Serial.println(sensorReading22); // the raw analog reading
  Serial.print("Analog reading23 = ");
  Serial.println(sensorReading23); // the raw analog reading
  Serial.print("Analog reading30 = ");
  Serial.println(sensorReading30); // the raw analog reading  
  Serial.print("Analog reading31 = ");
  Serial.println(sensorReading31); // the raw analog reading
  Serial.print("Analog reading32 = ");
  Serial.println(sensorReading32); // the raw analog reading
  Serial.print("Analog reading33 = ");
  Serial.println(sensorReading33); // the raw analog reading
 
  delay(100);
}

```

**Slave**

```auto
int photocellPin0 = 0; // the cell and 10K pulldown are connected to a0
int photocellReading0; // the analog reading from the analog resistor divider
int photocellPin1 = 1; // the cell and 10K pulldown are connected to a1
int photocellReading1; // the analog reading from the analog resistor divider
int photocellPin2 = 2; // the cell and 10K pulldown are connected to a2
int photocellReading2; // the analog reading from the analog resistor divider
int photocellPin3 = 3; // the cell and 10K pulldown are connected to a3
int photocellReading3; // the analog reading from the analog resistor divider
 
#include <Wire.h>
 
void requestHandler()
{
  int data[4];
 
  data[0] = analogRead(photocellPin0);
  data[1] = analogRead(photocellPin1);
  data[2] = analogRead(photocellPin2);
  data[3] = analogRead(photocellPin3);
 
  Wire.send((byte*) data, 4);
}
 
void setup()
{
  Wire.begin(0x30); // join i2c bus (address optional for master)
  Serial.begin(9600);
}
 
void loop() {
  photocellReading0 = analogRead(photocellPin0);
  photocellReading1 = analogRead(photocellPin1);
  photocellReading2 = analogRead(photocellPin2);
  photocellReading3 = analogRead(photocellPin3);
 
  Serial.print("Analog reading0 = ");
  Serial.println(photocellReading0); // the raw analog reading
  Serial.print("Analog reading1 = ");
  Serial.println(photocellReading1); // the raw analog reading
  Serial.print("Analog reading2 = ");
  Serial.println(photocellReading2); // the raw analog reading
  Serial.print("Analog reading3 = ");
  Serial.println(photocellReading3); // the raw analog reading
 
  delay(500);
}

```

**Serial.print result on the master**

> Analog reading00 = 255
> 
> Analog reading01 = -1
> 
> Analog reading02 = -1
> 
> Analog reading03 = -1
> 
> Analog reading10 = 255
> 
> Analog reading11 = -1
> 
> Analog reading12 = -1
> 
> Analog reading13 = -1
> 
> Analog reading20 = 255
> 
> Analog reading21 = -1
> 
> Analog reading22 = -1
> 
> Analog reading23 = -1
> 
> Analog reading30 = 255
> 
> Analog reading31 = -1
> 
> Analog reading32 = -1
> 
> Analog reading33 = -1

As you can see, not the results I was hoping for. So if you guys could please enlighten me, it would be absolutely brilliant. I am about ready to throw in the towel after frustrations after frustrations after frustrations with not getting what I expected.

Thank you very much for your time and effort. Much appreciated.

Ryan

---

<div class="post-metadata">

**Author:** ![Chanler](https://avatars.discourse-cdn.com/v4/letter/c/a6a055/32.png) [@Chanler](https://community.sparkfun.com/u/Chanler)\
**Post date:** [November 9, 2011, 5:14am UTC](https://community.sparkfun.com/t/i2c-help/22447/2 "2011-11-09T05:14:58Z")

</div>

Ok, does your slave output correctly over serial? It looks really funky to me, but it might work.

I would change a few things in it if it doesn’t work.

int photocellPin0 = 0; should probably be, #define photocellPin0 = 0 (note, no semicolon here)

Since you have photocellReading0-3 declared globally, I’d change the next line as follows:

data[0] = analogRead(photocellPin0);

to

data[0] = photocellReading0;

etc…

You’ve already read the value into a variable, might as well put it to use instead of getting a new reading.

---

<div class="post-metadata">

**Author:** ![ScottH](https://avatars.discourse-cdn.com/v4/letter/s/b9e5f3/32.png) [@ScottH](https://community.sparkfun.com/u/ScottH)\
**Post date:** [November 10, 2011, 7:46pm UTC](https://community.sparkfun.com/t/i2c-help/22447/3 "2011-11-10T19:46:57Z")

</div>

You can’t send 4 ints with 4 bytes.

---

<div class="post-metadata">

**Author:** ![elevator4](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@elevator4](https://community.sparkfun.com/u/elevator4)\
**Post date:** [November 13, 2011, 10:18pm UTC](https://community.sparkfun.com/t/i2c-help/22447/4 "2011-11-13T22:18:19Z")

</div>

> ScottH:  
> You can’t send 4 ints with 4 bytes.

Hi mjscar,

what ScottH means is that the slave is collecting 4 ints, but sending the collection as 4 bytes. An int is 16 bits wide, taking two bytes. So, in the code

```auto
void requestHandler()
{
  int data[4];

  data[0] = analogRead(photocellPin0);
  data[1] = analogRead(photocellPin1);
  data[2] = analogRead(photocellPin2);
  data[3] = analogRead(photocellPin3);

  Wire.send((byte*) data, 4);
}

```

the Wire.send() is only sending the first two ints of data
