I2C Photon -> Electron

Hi all, I've never used the Wire library before and I've never used I2C as well. I'm trying to communicate between my Electron and Photon but it doesn't seem to be working. I'm using 10k pull-up resistors. I've been trying to research the library but obviously it hasn't been doing me any good. I was hoping you guys could tell me what I'm doing wrong... :grinning:

This is the master code that runs on the Electron
I've gotten my code from Particle Reference, switching between Photon and Electron

void setup()
{
Wire.begin(); // join i2c bus as master
Serial.begin(9600); // start serial for output
}

void loop()
{
Wire.requestFrom(2, 6); // request 6 bytes from slave device #2

while(Wire.available()) // slave may send less than requested
{
char c = Wire.read(); // receive a byte as character
Serial.print("Data:");
Serial.print(c); // print the character
}

delay(500);
}

This is the one the Photon slave runs:

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

byte x = 0;

void loop()
{
Wire.beginTransmission(2);
Wire.write("x is ");
Wire.write(x);
Serial.print(x);
Wire.endTransmission();

x++;
delay(500);
}

I was checking the data I got from the Electron's serial monitor.
Hope you guys can help

-Jack

@JackD12, you need to set up one device as a master and the other as a slave. The slave is setup using Wire.begin(address); where address is the I2C address of the slave. In your code, neither of the devices is setup as a slave! :wink:

2 Likes

@peekay123 , so you don’t type Wire.begin(address); you type Wire.begin(2); or whatever the address should be. If you only have one slave device should the address be 1 or any random number? And how would that effect the master?

@JackD12, you give the slave any non-conflicting address from 0x01 to 0x7F (1-127) using Wire.begin(2) for example for a slave with I2C address 0x02. I always prefer HEX notation for addresses. On the master, you refer to the slave with the address you specified using Wire.requestFrom(0x2, 6); // request 6 bytes from slave device at I2C address 0x02. :wink:

1 Like

@peekay123 I just have one more question. In the slave code I say "Wire.beginTransmission(2); " Is that alright since I’m transmitting to the master? I wasn’t sure if the 2 referred to the master or the slave… Also I did change the slave address to 0x02, so should the 2 there should be 0x02 correct? In the IDE when I wrote “Wire.beginTransmission();” it came up with an error so that’s the only reason I have the two there. Sorry I’m clueless :joy: :sweat_smile:

As a slave your device is not allowed to begin a transmission but needs to react on transmissions or requests initiated by the master.

Have a look at these
https://docs.particle.io/reference/firmware/photon/#onreceive-
https://docs.particle.io/reference/firmware/photon/#onrequest-

@JackD12, follow @ScruffR’s instructions to understand how to use an I2C slave. As for the 2 vs 0x02, they are both the same value. I just prefer the HEX representation, that’s all. :wink: