I2c not working between arduinos

I’m trying to set up an i2c connection between two arduinos (duemilanove and diecimila). I’m using a program that I’ve used before, so I know it works, but the hardware doesn’t work for some reason.

  • I have connected the ground pins of both arduinos with a hookup wire.

  • I have connected both 5v pins of both arduinos to my breadboard (row 0)

  • I have two resistors connected to row 0 as well

  • I connected the other side of those resistors to rows 1 and 2

  • I connected pin 4 of both arduinos to row 1

  • I connected pin 5 of both arduinos to row 2

I hope my description is satisfactory. Do any of you see any problems with my curcuit?

P.S. This is one of my first posts, so if this is the wrong place or format, please let me know.

A picture would help me. I know I could draw one, but it might not match yours (and I'm lazy).

So would seeing your code on the two Arduionos. I know that you say it works, but it isn't working here, so there could be some subtle problem.

At least from the code, we could see what you are trying to communicate between the two Arduinos.

| or - means wire
o means plugged into breadboard

R1 and R2 are 2k resistors

MASTER [GND] [5v] [5] [4]
| | | |
Row 0 | o-R1-o |
Row 1 | o-R2----o
| | | |
| | | |
SLAVE [GND] [5v] [5] [4]

Master code:
#include <Wire.h>

#define LED_OUT 13

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
pinMode(LED_OUT, OUTPUT);
digitalWrite(LED_OUT, LOW);
}

byte x = 0;

void loop()
{
for (int i=0; i<x; i++) {
digitalWrite(LED_OUT, HIGH);
delay(200);
digitalWrite(LED_OUT, LOW);
delay(200);
}

Wire.beginTransmission(4); // transmit to device #4
Wire.send(x); // sends one byte
Wire.endTransmission(); // stop transmitting

x++;
if (x == 10)
x = 0;

delay(5000);
}

Slave code:
#include <Wire.h>
extern “C” void __cxa_pure_virtual(void);
void __cxa_pure_virtual(void) {};

#define LED_OUT 13

int x;

void receiveEvent(int howmany);

void setup() {
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode(LED_OUT, OUTPUT);
digitalWrite(LED_OUT, LOW);
x = 0;
}

void loop() {
if (x > 0) {
for (int i=0; i<x; i++) {
digitalWrite(LED_OUT, HIGH);
delay(200);
digitalWrite(LED_OUT, LOW);
delay(200);
}
x = 0;
delay(1000);
}
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
x = Wire.receive(); // receive byte as an integer
}

bump.

Here's a checklist for you:

  • Pin 4 and 5 should be analog 4 and analog 5 (AtMega pin 27 and 28)
  • Resistors should be 4k7 (or thereabout)
  • Your definition of "x" must be prefixed with the "volatile" keyword
  • Grounds on both boards must be connected
  • +5V should only be connected if both boards are powered from the same source - otherwise not

Thank you very much.

I was using digital pins as opposed to analog ones. It works now!

I was wondering for a while as well if I should use analog or digital 4 and 5. Reading the specs led me to try analog first, but I was not entirely sure. Perhaps this could be written more explicit, especially where I looked first: http://arduino.cc/en/Main/ArduinoBoardNano