Can not make the I2C work

hardware and wiring:
I am using a Mega 2560 (chinese) as master and a RF-Nano (chinese) as slave. I already checked that SCL and the SDA in the Nano are positioned as usual on the pin A5 and A4, while I am not sure which of the Mega pin should I use; those on 20 and 21 to which SCL and SDA seem to be assigned, or the pins directly named SCL and SDA? From what I read online it seems to be irrelevant and they give me the same result (which is notting) anyway.

The Mega, which is alimented by USB which allow serial communication with the computer, power the nano and the wiring is the following:

mega - nano
SDA - A4
SCL - A5
GND - GND
V3.3 - 3V3

forgive me for not uploading an image but is is very short and simple, there is no resistors or anything. if needed soon I will upload an image but in this moment I have to run.

I have copied this code from Arduino website, without applying any change
insert code here

Master:

// Wire Master Reader
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Reads data from an I2C/TWI slave device
// Refer to the "Wire Slave Sender" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop() {
  Wire.requestFrom(8, 6);    // request 6 bytes from slave device #8

  while (Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}

Slave:

// Wire Slave Sender
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Sends data as an I2C/TWI slave device
// Refer to the "Wire Master Reader" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup() {
  Wire.begin(8);                // join i2c bus with address #8
  Wire.onRequest(requestEvent); // register event
}

void loop() {
  delay(100);
}

// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void requestEvent() {
  Wire.write("hello "); // respond with message of 6 bytes
  // as expected by master
}

When I check the serial communication with the computer for debugging, nothign appears.
I would expect the to receive “hello “ written repeatedly, as the master is calling the slave char, to print it, but the sheet is completely blanc.
Would you help me to find out which is the problem? Thanks!

Hello,
In case of I2C troubles: first scan bus before you do anything else. Then you know what is available on your bus and what not. There are many demo's available how to do this.

Best Regards,
Johi.

Thanks! No device found, so it seems to be something wrong in my wiring..

Scanning...
No I2C devices found

The Mega 2560 and (I think) also the Nano RF run at 5V.
They have a voltage regulator for 3.3V for sensors.

If you want to power the Nano with the Mega, then connect the 5V pins.

thanks man I had no Idea, will try soon!

1. Connect MEGA and NANO as per Fig-1.
i2cMegaNano
Figure-1:

2. Upload the following sketch into MEGA

#include<Wire.h>

void setup()
{
    Serial.begin(9600);
    Wire.begin();
    Wire.beginTransmission(0x08);
    byte busStatus = Wire.endTransmission();
    if(buStatus !=0)
    {
        Serial.print("Slave is not found.");
        while(1);    //wait for ever
    }
    Serial.println("Slave is found.");
}

void loop()
{
    byte m = Wire.requestFrom(0x08, 6);
    for(int i=0; i<m; i++)
    {
          char x = Wire.read();
          Serial.print(x);
    }
    Serial.printn();
    delay(1000);
}

3. Upload the following sketch into NANO

#include<Wire.h>

void setup()
{
     Serial.begin(9600);
     Wire.begin(0x08);
     Wire.onRequest(sendEvent);
}

void loop()
{

}

void sendEvent()
{
    Wire.write("Hello ");
}

4. Bring in Serial Monitor of MEGA
5. Check that Hello appears on SM1 at 1-sec interval.

Hi! It has been a while from the last time I had time to continue working on this, I have loaded the 2 codes adjusting the following in the master code:

  • I declared buStatus as byte value before setup (otherwise it wouldn't allow me to compile)
  • I changed "Serial.printn()" to Serial.println()
    It seems to work properly, next weekend I will take some time to read it in dept, in particular I am curious to understand if the slave's address had any part in the code and how. Of course if you are reading you could give me some hints! Thanks!

This is the updated master code:

#include<Wire.h>

byte buStatus;

void setup()
{
    Serial.begin(9600);
    Wire.begin();
    Wire.beginTransmission(0x08);
    //byte busStatus = Wire.endTransmission();
    if(buStatus !=0)
    {
        Serial.print("Slave is not found.");
        while(1);    //wait for ever
    }
    Serial.println("Slave is found.");
}

void loop()
{
    
    byte m = Wire.requestFrom(0x08, 6);
    for(int i=0; i<m; i++)
    {
          char x = Wire.read();
          Serial.print(x);
    }
    Serial.println();
    delay(1000);
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.