Is possible connect two arduino with i2c?

Is possible connect two arduino with i2c?

i only need to send data from arduino A to arduino B, ::), thanks!

Should be possible. You could also use SPI.

Not sure if the Wire library allows slave mode. You may need to do it manually.
The Wire library's source would help.

The problem is that i using the serial with another device... mmm theres another way, maybe with PWM?

SPI does not use (or does not need to use) the serial pins. pin 10,11,12,13 are what it uses for the eeprom example and for the digital potentiometer example

http://www.arduino.cc/en/Tutorial/SPIEEPROM
http://www.arduino.cc/en/Tutorial/SPIDigitalPot

Much easier than I2C as no one seems to want to share how to do it.

The problem is that i using the serial with another device... mmm theres another way, maybe with PWM?

Thanks very much :slight_smile:

Hi,

SPI does not use (or does not need to use) the serial pins. pin 10,11,12,13 are what it uses for the eeprom example and for the digital potentiometer example

http://www.arduino.cc/en/Tutorial/SPIEEPROM
http://www.arduino.cc/en/Tutorial/SPIDigitalPot

Much easier than I2C as no one seems to want to share how to do it.

I don't think so.
I haven't tried to setup a communication between 2 arduino with either IIC or SPI, but I think you need one board act as a master and the other one as a slave.
The master initiates a communication sequence and the slave board just listens and send data back if requested to do so.

The Wire-Lib for IIC has build-in support for this master- and slave thing.
The master-part of SPI is documented very well in the examples, but for the slave part there is not even some experimental code to be found on the arduino-site.

I would definitly recommed using IIC (the Wire-lib).

Eberhard

P.S.
If someone has a link to SPI-slave-code please speak up, Thanks.

If someone has a link to SPI-slave-code please speak up, Thanks.

Here ya go: http://www.atmel.com/dyn/resources/prod_documents/doc2585.pdf

I haven't tried to implement this under Arduino, but it's a starting point at least.

If you don't need to move a lot of data, I2C may be the way to go if the Wire library has already implemented it, but SPI is much faster than I2C if you are moving lots of data, or your processor is too busy to tie up waiting on IO.

-j

Good point on the slave function.

http://www.nearfuturelaboratory.com/index.php?p=276

// SPI Enable (SPE) to one
// Master/Slave Select (MSTR) to one turns the ATMega8 into the master
// Clock Phase (CPHA) set to 1 means sample on the trailing edge
// sample on trailing edge of clk for the DS1306
SPCR = (1<<SPE)|(1<<MSTR)|(1<<CPHA);
clr=SPSR;
clr=SPDR;

I am assuming from this that 0<<MSTR would make a device a slave but beyond that I'm lost.

Hi,
just a few more thoughts on IIC vs.SPI

The code for a SPI-slave must look very similar to the IIC-Slave code from the Wire-Lib code.
One would need to create a callback-function that is registered with the SPI-Interrupt for receiving a byte through SPI.

The high speed of SPI is mainly due to the fact that the slaves are usually chips that implement the whole SPI-slave processing in hardware. A software SPI-slave will never reach one of the higher SPI-clock-rates of the Ardunio as a SPI-master.

One more argument for IIC: The protocol implements basic error detection. SPI has nothing on this.

Eberhard

i was able to make an excellent com. between two arduinos using i2C.

Im usining it to make my arducopter autopilot, and ill use arduino A just to do the heavy duty of catching data from GPS, accelerometers, and Gyros, and sent it through I2c to arduino b, to preccess it, and send the PMM to the aircraft, in case of a failure on arduino A, i able to select from autopilot to manual. But now im in develomept and investigation. My work is based on this "open source" autopliot: PaparazziUAV
BUT it uses two Philips LPC2148 ARM7, 32-bit RISC processor core, 512KB on-chip Flash ROM, 40KB RAM and can be clocked at 60MHz. :o, a lot of power.....

Ok how i made it? Easy, just connect analogic pin 5 to pin 5, a pin 6 to pin 6. of both arduinos.

I, using arduino A as master, because i need to read nintendo Wii acceleromter (slave), and the example code to send data is:

#include <Wire.h> 
 
int x;
void setup() 
{ 
  Wire.begin(); // join i2c bus (address optional for master) 
}  
 
void loop() 
{ 
  Wire.beginTransmission(4); // transmit to device #4 
  Wire.send("TEST1 ");        // sends six bytes
  Wire.send(x);              // sends int
  Wire.endTransmission();    // stop transmitting 
  x++;         //same: x=x+1;
  delay(500); //:P
}

And arduino b is slave, and the code to recive data is:

#include <Wire.h>
 
void setup() 
{ 
  Wire.begin(4);                // join i2c bus with address #4 
  Wire.onReceive(receiveEvent); // register event 
  Serial.begin(9600);           // start serial for output 
} 
 
void loop() 
{ 
  delay(100); 
} 
 
// function that executes whenever data is received from master 
// this function is registered as an event, see setup() 
void receiveEvent(int howMany) 
{ 
  while(1 < Wire.available()) // loop through all but the last 
  { 
    char c = Wire.receive(); // receive byte as a character 
    Serial.print(c);         // print the character 
  } 
  int x = Wire.receive();    // receive byte as an integer 
  Serial.println(x);         // print the integer
}

Remember i using channel 4, you can change it whatever you one, just be careful if you are using another devices with i2c, to void conflict.

Good luck everybody! :wink:

Here's an example using two Lilypads... with photo.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1201476854/5#5

BTW. It's analog pins 4 & 5, not 5 & 6

wow, looks neat and wonderful.