Hello All,
I have a project that requires i2c communication with an Arduino that is configured as a slave device. What I have found is that the new Photon2 appears to be inconsistent when it is receiving data from the slave.
I have a logic level converter and 4.7k pull up resistors in place. For simple testing, I loaded up the standard Wire example (master_reader) on the Photon2 and the example (slave_sender) on the Arduino. The only modifications I made were a couple of Serial prints to help with debugging the problem.
master_reader
// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
Serial.println("Sending request.");
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
slave_sender
// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin(8); // join i2c bus with address #8
Wire.onRequest(requestEvent); // register event
}
void loop() {
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
Serial.println("Received Request");
Wire.write("hello "); // respond with message of 6 bytes
// as expected by master
}
On the Photon2, I would only receive "hello" back every now and then. Sometimes it would be every half second as expected and sometimes it would be 4 or 5 seconds between receives.
I then added the Serial.println("Received Request"); to the slave sender so I could see if it was in fact receiving the request for data and it was.
For every "Sending request..." that was printed out on the serial terminal of the Photon2, I would see a "Received Request" printed on the Arduino. At which point it was sending back "hello", but again, I'd only get the "hello" printed in the Photon2 serial feed occasionally. Sometimes there would be 20 or so requests before it would be received.
After this, I decided to remove the Arduino and test with two Photon2s. The results were the same. With that setup, I had 4.7k pull up resistors on SDA and SCL to 3.3v and only three wires between the Photon2s. GND, SDA, and SCL.
To note, running this exact same setup between two Arduino nanos results in every request for data returning the "hello" string.
What am I doing wrong?
Thanks!