I am trying to use the I2C bus between two Uno's and a Mega. I had it working for awhile, but then I rewired it and it no longer works so I know its not a problem with the code. When I have the two Uno's connected, it works fine, but once I go to connect the Mega, the Uno's can no longer communicate and nothing works. I only have the SDA/SCL pins connected and everything is powered separately. I have tried connecting the Grounds and 5v, but it didn't help anything. Anyone have any idea? Thanks, Kevin
windu34:
I am trying to use the I2C bus between two Uno's and a Mega. I had it working for awhile, but then I rewired it and it no longer works so I know its not a problem with the code. When I have the two Uno's connected, it works fine, but once I go to connect the Mega, the Uno's can no longer communicate and nothing works. I only have the SDA/SCL pins connected and everything is powered separately. I have tried connecting the Grounds and 5v, but it didn't help anything. Anyone have any idea? Thanks, Kevin
Are you sure you are using the correct pins?
The UNO has two pin locations from which you can access the I2C bus:
- On pins A4, A5 (A4 is SDA, A5 is SCL)
- or on the Dedicated pins near D13. GND,AREF,SDA,SCL
On the Mega There are also two pin locations from which you can access the I2C bus, but only the pins near D13 matchup.
- On pins D20,D21 (D20 is SDA, D21 is SCL)
- or on the Dedicated pins near D13. GND,AREF,SDA,SCL
So, if you are trying to use A4,A5 only the UNO's will work.
Chuck.
Can I connect the Unos via A4 and A5 and then have them connected to the Mega's Digital 20 and 21 pins?
The I2C on my Arduino Uno seems to not work. Ive checked it against one of my other Uno's to make sure its not a code/wiring problem. Is this possible?
Is this possible?
Is it possible that you have fried the board? Yes.
I can upload code to it and it is recognized by the IDE
I can upload code to it and it is recognized by the IDE
You can upload code via the I2C pins? Please explain how?
Please explain how the IDE recognizes the I2C circuit.
windu34:
The I2C on my Arduino Uno seems to not work. Ive checked it against one of my other Uno's to make sure its not a code/wiring problem. Is this possible?
Yes, you can damage an Arduino pin.
Test it.
Write a simple sketch to display the input value from A4, A5.
void setup(){
Serial.begin(9600);
pinMode(A4,INPUT);
digitalWrite(A4,HIGH); // turn on internal pullup resistor
pinMode(A5,INPUT);
digitalWrite(A5,HIGH); // turn on internal pullup resistor
Serial.println("Booted");
}
void loop(){
static bool pinA4=LOW;
static bool pinA5=LOW;
if(pinA4!=digitalRead(A4)){
pinA4 = !pinA4;
Serial.print("A4=");
Serial.println(pinA4);
}
if(pinA5!=digitalRead(A5)){
pinA5 = !pinA5;
Serial.print("A5=");
Serial.println(pinA5);
}
}
Upload this sketch to your UNO, the serial monitor should show:
Booted
A4=1
A5=1
now connect a jumper to GND, touch the other end to A4 or A5. Whenever you 'ground' A4 or A5 you should see:
A4=0 or A5=0
When you remove jumper, you should see:
A4=1 or A5 = 1
Next test, output:
bool currentVal=LOW; // set initial value
uint8_t currentPin = A5;
void switchPin(){ // swap input and output pins
if(currentPin==A4){
pinMode(A5,INPUT);
PinMode(A4,OUTPUT);
digitalWrite(A4,currentVal);
currentPin=A5;
}
else {
pinMode(A4,INPUT);
pinMode(A5,OUTPUT);
digitalWrite(A5,currentVal);
currentPin=A4;
}
}
void setup(){
Serial.begin(9600);
switchPin(); // setup initial input,output
Serial.println("booted");
}
void printState(){
if(currentPin==A4){
Serial.print("A5 is outputting: ");
Serial.println(currentVal,DEC);
if(digitalRead(currentPin) != currentVal) Serial.print("But, A4 is reading as: ");
else Serial.print("A4 is correctly reading: ");
Serial.println(digitalRead(currentPin),DEC);
}
if(currentPin==A5){
Serial.print("A4 is outputting: ");
Serial.println(currentVal,DEC);
if (digitalRead(currentPin) != currentVal) Serial.print("But, A5 is reading as: ");
else Serial.print("A5 is correctly reading: ");
Serial.println(digitalRead(currentPin),DEC);
}
}
void writeOther(uint8_t cPin,uint8_t cVal){
if(cPin==A4) digitalWrite(A5,cVal);
else digitalWrite(A4,cVal);
}
void loop(){
if(digitalRead(currentPin)==currentVal){
currentVal = !currentVal;
writeOther(currentPin,currentVal);
}
else { // problem
Serial.println(" INput does not match Output!");
printState();
return; // error out
}
if(digitalRead(currentPin)==currentVal){
switchPin();
}
else { // problem
Serial.println(" INput does not match Output!");
printState();
return; // error out
}
printState();
delay(500); // 1/2 second between tests
}
Upload this code and place a jumper between Pin A4 and A5. The Serial monitor will tell you if the Uno Failed.
Chuck.
PaulS:
You can upload code via the I2C pins? Please explain how?Please explain how the IDE recognizes the I2C circuit.
I meant it recognizes the Arduino thus it isn't fried, but I didn't know you could fry the I2C circuit.
chucktodd:
Upload this code and place a jumper between Pin A4 and A5. The Serial monitor will tell you if the Uno Failed.Chuck.
I tried this sketch and one of the Uno's didn't show anything on the Serial except "Booted" which I assume both failed. I tried it on one of my other Unos and it worked as you said. I then tried it on my Mega by changing A4 to 20 and A5 to 21 and only 21 showed response.
Note that in the output test, about line 7, PinMode(A4,OUTPUT); has an upper case P on pinMode, which should be lower case.