I2C communication between 2 Arduino nanos

Im trying to get 2 nanos to communicate with each other via I2C but the example code seems to lock up each time I join the 2 Arduinos together. Im using the example code provided with the arduino enviroment (master_writer and slave_reader) they work fine by themselves, but as soon as i hook up my data or clock lines (A4 and A5 if im not mistaken) the code seems to lock up at any time of the code. I am using 10K pull up resistors and both nanos run without a hitch with any other code I use (that dosent use I2C)

Can anyone help?

Are you connecting SDA to SDA and SCL to SCL? And are you connecting the Gnd together?

See above page for more tips.

Yes I am nick... the attached picture is my setup at the moment (sorry if its not clear enough)...

I am using serial debugging (through the Arduino IDE) and I have a simple Serial.println() to tell me where the code is up to... the master runs fine without the slave hooked up, but as soon as I connect the slave, the whole thing freezes.... I dont know if I'm doing anything wrong or not... I'm just following tutorials and looking at codes on the internet

That looks OK, once I downloaded your 1.4 Megabyte, 3264 x 1952 image. You can always size them down a bit you know. 600 pixels wide is usually plenty.

I'm just following tutorials and looking at codes ...

What code? Can you post it? Or just try the master and slave example on my web page above.

Sorry about that nick :slight_smile: I'll try the examples on your site and I'll get back to you :slight_smile:

I cant remember what other codes I looked at, sorry... but I'm currently using the example ones provided with the arduino IDE

Master Code (From your site Nick)

// Written by Nick Gammon
// February 2012

#include <Wire.h>

const byte SLAVE_ADDRESS = 42;
const byte LED = 13;

void setup () 
{
  Serial.begin(9600);
  Serial.println("Starting Wire Libary");
  Wire.begin ();
  Serial.println("Setting Pin 13 to Output");  
  pinMode (LED, OUTPUT);   
  
}  // end of setup

void loop () 
{
  Serial.println("Starting For Loop");
  for (int x = 2; x <= 7; x++)
    {  
        Serial.println("Beginning Wire Transmission");
    Wire.beginTransmission (SLAVE_ADDRESS);
  Serial.print("Writing ");
    Serial.print(x);
      Serial.println(" to wire");
    Wire.write (x);
      Serial.println("Checking End Transmission"); // Displays this on serial than hangs
    if (Wire.endTransmission () == 0)
      digitalWrite (LED, HIGH); 
    else
      digitalWrite (LED, LOW); 
        Serial.println("Delay of 200ms");
    delay (200);
    }
    
}  // end of loop

Slave

// Written by Nick Gammon
// February 2012

#include <Wire.h>

const byte MY_ADDRESS = 42;

#define BETA_ARDUINO ARDUINO < 100

void setup () 
{
  Serial.begin(9600);
  Serial.println("Starting Wire Libary");
  Wire.begin (MY_ADDRESS);
  Serial.println("Setting Pins 2 to 7 to output");
  for (byte i = 2; i <= 7; i++)
    pinMode (i, OUTPUT);
    Serial.println("Enabling Wire Interrupt"); // Displays this on serial than hangs
  Wire.onReceive (receiveEvent);
}  // end of setup

void loop() 
{
// nothing in main loop
}

// called by interrupt service routine when incoming data arrives
void receiveEvent (int howMany)
 {
   Serial.println("Wire Available");
  while (Wire.available () > 0)
  {
    Serial.println("Setting c as a byte");
    byte c;
    Serial.println("Reading Wire data and setting it as c");
    c = Wire.read ();
    Serial.print("c = ");
    Serial.println(c);
    // toggle requested LED
    if (digitalRead (c) == LOW)
      digitalWrite (c, HIGH);
    else
      digitalWrite (c, LOW);
  }
}  // end of receiveEvent

Take those serial prints out of the receiveEvent. You can't do that inside an ISR. Do something else like turn on an LED. Or set a flag which you test in the main loop.

Sorry if I'm sounding so "Newbish" on this... its cause I am new to this...

The master still hangs on the same line and I removed those Serial.println() from the receive event and added this to the loop

void loop() 
{
Serial.println(c);
delay(100);
}

And all I'm getting back is 0

:~ I have no idea what is happening

It isn't the code I posted because you have littered it with serial prints. Try my exact code and see if the LEDs toggle.