Pages: [1]   Go Down
Author Topic: I2C communication between 2 Arduino nanos  (Read 872 times)
0 Members and 1 Guest are viewing this topic.
North East Coast
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19071
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://www.gammon.com.au/i2c

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

See above page for more tips.
Logged


North East Coast
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


* IMAG0145.jpg (1395.96 KB, 3264x1952 - viewed 9 times.)
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19071
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Quote
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.
Logged


North East Coast
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

North East Coast
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Master Code (From your site Nick)

Code:
// 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
Logged

North East Coast
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Slave

Code:
// 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
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19071
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


North East Coast
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

And all I'm getting back is 0

 smiley-confuse I have no idea what is happening
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 499
Posts: 19071
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: