I am trying to find a way to use a second I2C device on my Arduino Due using the SDA1 and SCL1 ports (NOT 20 and 21, the other 2). However, 20 and 21 do seem to work with the wire.h library but SDA1 and SCL1 are not working. I am also not able to find a "Wire1.h" file which should(?) support the secondary I2C ports. Can you help me out?
#include <Wire.h>
extern TwoWire Wire1;
void setup()
{
Wire1.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop()
{
Wire1.requestFrom(2, 6); // request 6 bytes from slave device #2
while(Wire1.available()) // slave may send less than requested
{
char c = Wire1.read(); // receive a byte as character
Serial.print(c); // print the character
}
delay(500);
}
Remember to use pins 70 (SDA1), 71 (SCL1)
Regards,
Palliser
My prior recommendation (reply#3) does not work. I just tested it using two DUEs with the master_writer and slave_receiver sketches.
I made a couple extra tests that work OK.
Test A: Master SDA1/SCL1 writes on Slave SDA/SCL.
(Using Wire1.xxx() in the master code. Using Wire.xxx() in the slave code).
Test B: Master SDA/SCL writes on Slave SDA1/SCL1
(using Wire.xxx() in the master code. Using Wire1.xxx() in the slave code).
So far so good, but maksudcan's problem remains. I can't establish communication between Master and Slave both in SDA1/SCL1.
There is only one Wire library (.h,.cpp) and one class (TwoWire).
The pin definition for Wire1 is defined in Wire (.h,.cpp) and variant (.h,.cpp).
I will try to make more time soon and do more tests.
I've been trying to do the same thing, and have been getting very frustrated. I'm using two Adafruit LSM303 accelerometers at different places in the system. Since the slave address is the same for both, I can't have them on the same I2C bus. I finally gave up and called a friend who's an embedded software ninja, and he had me running in no time.
Basically it's what Palliser said: you don't need any additional libraries, you just substitute "Wire1" for "Wire".
Let me know if you haven't solved your issue yet, and I'll post more details that hopefully will be helpful.
I just tested the master reader/slave sender example with two DUEs for second I2C (SDA1/SCL1) using 1K pull-up resistors and the communication is OK. Please, verify that. Regards, Palliser.
NOTE: Remember to use wire1.xxx() instead of wire.xxx() for the functions.
-P
I can verify that it works with your first recommendation, however, some devices wont work ond SDA1 and SCL1 and do work on SDA and SCL and vice versa, but I got my devices working:)!.
Here is my code for I2C wire0 and wire1
I use Arduino Mega.. But works on DUE as well:
some cpp file or directly in project.ino file:
#include <wire.h>
TwoWire Wire1;
//logMessage is my logger.. replace this by Serial.println or something..
logMessage(0, "I2C scanner. Scanning bus 0...");
for (byte i = 8; i < 127; i++)
{
Wire.beginTransmission(i);
if (Wire.endTransmission() == 0)
{
logMessage(0, "Found address: " + String(i));
// check address
if ((i == 32) or (i == 39))
{
// found device, do something..
}
delay(1);
}
}
Wire.endTransmission();
logMessage(0, "I2C scanner. Scanning bus 1...");
for (byte i = 8; i < 127; i++)
{
Wire1.beginTransmission(i);
if (Wire1.endTransmission() == 0)
{
logMessage(0, "Found address: " + String(i));
// check address
if (i == 64)
{
// found device, do something..
}
delay(1);
}
}
Wire1.endTransmission();
logMessage(1, "I2C device check DONE.");
Members
I have read the above and Im not sure if this is correct. I am currently saving 1 number to the ext eeprom, but when I change the number from 123 to 456 I get a wrong Serial.print number back.
/*save2numbers
DUE + one 24LC256
*/
#include <Wire.h> //use SCl & SDA
extern TwoWire Wire1;// use SCL1 & SDA1
#define disk1 0x50 //Address of 24LC256 eeprom chip
void setup(void)
{
Serial.begin(9600);
Wire.begin();
Wire1.begin(); // join i2c bus
unsigned int address = 0;
writeEEPROM(disk1, address, 123);//save the number '123'
Serial.print(readEEPROM(disk1, address), DEC);
}
void loop() {}
void writeEEPROM(int deviceaddress, unsigned int eeaddress, byte data )
{
Wire1.beginTransmission(deviceaddress);
Wire1.write((int)(eeaddress >> 8)); // MSB
Wire1.write((int)(eeaddress & 0xFF)); // LSB
Wire1.write(data);
Wire1.endTransmission();
delay(5);
}
byte readEEPROM(int deviceaddress, unsigned int eeaddress )
{
byte rdata = 0xFF;
Wire1.beginTransmission(deviceaddress);
Wire1.write((int)(eeaddress >> 8)); // MSB
Wire1.write((int)(eeaddress & 0xFF)); // LSB
Wire1.endTransmission();
Wire1.requestFrom(deviceaddress, 1);
if (Wire1.available()) rdata = Wire1.read();
return rdata;
}