I am setting an I2C system between a Raspberry Pi 3 (running a UWP program in Windows 10 IoT Core) as Master connecting to an Arduino DUE as Slave (with Arduino 1.6/1.8 ). Right after the Pi 3 writes to the Arduino DUE, it fails with the following error:
"The transfer could not be performed due to a clock stretch timeout. Make sure the clock line is not being held low by the slave device."
I had no error messages connecting the Pi 3 to the Arduino UNO. I thought the Pi 3 would be more compatible with Arduino DUE and had less issues since both boards run on 3.3V.
I have done the following wiring connections:
- Connected to SDA(20)/SCL(21) directly.
- Connected to SDA(20)/SCL(21) with a resistor of 1k ohm and 4.6k ohm to 3.3V.
- Connected to SDA(20)/SCL(21) with a resistor of 1k ohm and 4.6k ohm to 5V.
- Connected to SDA1(70)/SCL1(71) directly.
- Connected to SDA1(70)/SCL1(71) with a resistor of 1k ohm and 4.6k ohm to 3.3V.
- On all connections both are Grounded directly.
- Even connected one time the Pi 3 3.3V to the Due 3.3V directly (and disconnected right after testing).
This is the Pi 3 Master code (only the relevant part that deals with Arduino):
public static async void InitCommArduino()
{
I2cConnectionSettings settings = new I2cConnectionSettings(0x40); // Arduino address
settings.BusSpeed =I2cBusSpeed.FastMode; //.StandardMode;
/*
// Get a selector string that will return all I2C controllers on the system
string aqs = I2cDevice.GetDeviceSelector("I2C1");
// Find the I2C bus controller devices with our selector string
DeviceInformationCollection dis = await DeviceInformation.FindAllAsync(aqs);
// Create an I2cArduinoDevice with our selected bus controller and I2C settings
I2cToArduino = await I2cDevice.FromIdAsync(dis[0].Id, settings);
*/
I2cController controller = await Windows.Devices.I2c.I2cController.GetDefaultAsync();
//Create an I2cDevice with our selected bus controller and I2C settings
I2cToArduino = controller.GetDevice(settings);
SignalArduinoToggleLights(new byte[] { 1 }, 1);
}
public static void SignalArduinoToggleLights(byte[] LightBuf, int IntBuf)
{
/* Write the register settings */
// When WriteBuf is
// 0 = Red
// 1 = Green
try
{
I2cToArduino.Write(LightBuf);
}
/* If the write fails display the error and stop running */
catch (Exception ex)
{
string some = ex.Message;
//CommToArduino.Text = "Failed to communicate with device: " + ex.Message;
return;
}
}
This is the Arduino Due Slave code:
#include <Wire.h>
#define Wire Wire1
void setup() {
Serial.begin(115200);
InitiateWire();
ResetLights();
}
void InitiateWire() {
Wire1.begin(0x40);
Wire1.setClock(400000L);
Wire1.onReceive(i2cReceiveFromPi3);
Serial.println("I2C init: done.");
}
void loop() {
// some code here...
}
void i2cReceiveFromPi3(int byteCount) { //byte i2cHandleRx(byte command)
while (Wire1.available()) { // slave may send less than requested
int x = Wire1.read(); // receive a byte as character
Serial.print("x value from Pi3: ");
Serial.println(x);
if (x == 1) { // checks to see byte is an e
TurnOnOffLight(1); // a Zero turns light ON
}
else if (x == 0) {
TurnOnOffLight(0);
}
}
}
void TurnOnOffLight(int DoAction) {
if (DoAction == 1) { // GREEN
digitalWrite(Relay_Red_Light, 0); // Turns Off RED Light
digitalWrite(Relay_Green_Light, 1); // Turns On GREEN Light
}
else if (DoAction == 0) {
digitalWrite(Relay_Green_Light, 0); // Turns Off GREEN Light
digitalWrite(Relay_Red_Light, 1); // Turns On RED Light
}
}
void ResetLights() {
pinMode(Relay_Green_Light, OUTPUT);
digitalWrite(Relay_Green_Light, 0); // Turns OFF Relay to start
pinMode(Relay_Red_Light, OUTPUT);
digitalWrite(Relay_Red_Light, 0); // Turns OFF Relay to start
}
Some say the Arduino Wire library is not for the DUE model; however, others argue that DUE has its own Wire library and the IDE correctly integrates it when compiling and deploying/uploading to the DUE board.
Questions:
- What code am I missing on Pi 3 and/or Arduino DUE?
- Is there any wiring in particular that I should follow explicitly using the Arduino DUE (since I have seen many using the Arduino UNO and that doesn't help with my case)? If you can provide images or fritzing sketches I will appreciate a lot!!!
- Is there any board I could setup between the Pi 3 and Arduino DUE that could resolve this communication problem?
I am relatively new to Pi 3 and Arduino (and boards in general). So I will appreciate if you provide as much details as possible.
Thanks in advance for your response!