Ic2 between 2 arduino Not working...

Hi all!
I’m trying to establish a communiation between two arduino in I2C but that not working.

I have build breadboard arduino like this http://www.arduino.cc/en/Main/Standalone
The power connection and the clock is same but I use this usb connection:http://www.robotshop.ca/sfe-arduino-serial-usb-board-1.html They give me the 5v.

I have connect the pin 4 and 5 of both arduino whit a 1k resistor to 5v on each link. I have try a 10k too.

Master

#include <Wire.h>

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

void loop()
{
  Serial.print("beginTransmission");
  Serial.println();
  Wire.beginTransmission(0x04);
  
  Serial.print("send");
  Serial.println();
  Wire.send(true);
  
  Serial.print("endTransmission");
  Serial.println();
  Wire.endTransmission();
  
  Serial.print("delay");
  Serial.println();
  delay(1000);
  
  Serial.println();
}

Slave

#include <Wire.h>

void setup()
{
  Serial.begin(115200);
  Wire.begin(0x04);
  Wire.onReceive(receiveEvent);
}

void loop()
{
  delay(100);
}

void receiveEvent(int howMany)
{
  while (Wire.available()) Serial.print(Wire.receive(), BIN);
  Serial.println();
}

Thx for the help! The receiveEvent is never call.

:) Help!

For starters, try taking out the Serial.print statements inbetween Wire.begintransmission and Wire.endtransmission. You are forcing the Arduino to spend a lot of time doing other things in the middle of the I2C transmission.

And maybe start with a simpler slave program: just have see if Wire.available is ever true in your main loop. If it is, add back the interrupt, prints, etc.

Finally, the usual value for the pullup resistors is 4.7k ohms.

while (Wire.available()) Serial.print(Wire.receive(), BIN);

How are you monitoring the Serial prints of the slave? If you are watching in a serial monitor, then you probably won't see anything. You are sending the number "1" which if you look at an ASCII chart, isn't a character. Instead of BIN, try DEC.

I wouldn't call delay() in your loop. Instead, just leave the loop blank.

All right! No more communication for me today!

Professor Chaos, I have follow your indication; no time lose between begin and endtransmission + I put some check before the while. But I have to wait for Monday to by a 4.7k resistor. The sentance: “Ho! Lord! The master arduino taking to me!” never apear! Well, The function receiveEvent are never call!

James C4S, I have remove the delay in the master loop function and i now send the number 666 print whit the DEC attribute. Always no communication…

Thx for the help! I will buy the proper resistance this Monday! If anyone have a other think. This is my new test code:

Master:

#include <Wire.h>

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

void loop()
{
  Serial.print("beginTransmission");
  Serial.print('\t');
  
  Wire.beginTransmission(0x11);
  Wire.send(666); //Send the devil number whitout losing time whit some other manipulation.
  Wire.endTransmission();
  
  Serial.print("endTransmission");
  Serial.println();
  delay(1000);
}

Slave:

#include <Wire.h>

void setup()
{
  Serial.begin(115200);
  Wire.begin(0x11);
  Wire.onReceive(receiveEvent);
  Serial.println("I'm initialize!");
}

void loop()
{
  //Empty
}

void receiveEvent(int howMany)
{
  Serial.print("Ho! Lord! The master arduino taking to me!");
  
  //Print the state of Wire
  if(Wire.available())
  {
    Serial.print("Available");
  }
  else
  {
    Serial.print("No Available");
  }
  
  //Receive data
  int myInt = Wire.receive();
  
  //Print data
  Serial.print(myInt, DEC);
  Serial.println();
}

By the way, This is what I receive when I plug the arduino in a console:

Master:

beginTransmission endTransmission
beginTransmission endTransmission
beginTransmission endTransmission
beginTransmission endTransmission

Slave:

I’m initialize!

Have a Nice Halloween day!

The basic forms of Wire.send and Wire.receive are byte functions, not int, so use a number between 0 and 255.

But I realize we are overlooking the most basic questions:

Have you connected each Arduino's ground to each other?

Since you are using a breadboard Arduino, are you using pins 27 and 28 on the chip, which are analog inputs 4 and 5 (the I2C pins), or are you using pins 4 and 5 on the chip (not the I2C pins!)?

i now send the number 666

:o Uh oh now you’re in trouble, you’ll have to get both Arduinos exorcised.


Rob

Some of the "connect two devices" issues are also covered in my essays on connecting two devices by a serial link. (It is a "dumb" connection, no hardware handshake, and no software checksums implemented in the demo stuff.)

http://sheepdogguides.com/arduino/art4SeIntro.htm

two 10k resistors in parallel might have sufficed as an alternative to a 4.7k

Bulleye Professor Chaos, I make a very stupid mistake in my connection pin.... And yes my 2 ground are connected. Well, whit wire at good place and a 10k pullup resitor (thx daveg360) the communication begun!!! Youpi! And effectively my Devil number didnt make the way. That a good news, only one arduino have to repent and confess its sins.

Thx for your help. Have a nice day.