I2C two way comunication

Hello Everyone!

I am trying to make a sort of two way communication with two arduinos, a Mega (master) and a nano(slave). The main problem I have is that it seems like the arduino nano which has the slave code, the wire.onReceive() seems to not be working... I can't figure out what is wrong with the code. Here are the two codes:

MASTER:

#include <Wire.h>

#define SLAVE_ADDRESS 0x04

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

void loop() {
  
  Wire.beginTransmission(SLAVE_ADDRESS); // transmit to slave device
  Wire.write(0x00); // sends one byte
  Wire.endTransmission(); // stop transmitting
  delay(10);
  Wire.requestFrom(SLAVE_ADDRESS, 6); // request 6 bytes from slave device
  while(Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.println(c); // print the character
  }
  delay(500); // wait half a second
  
  Wire.beginTransmission(SLAVE_ADDRESS); // transmit to slave device
  Wire.write(0x01); // sends one byte
  Wire.endTransmission(); // stop transmitting
  delay(10);
  Wire.requestFrom(SLAVE_ADDRESS, 6); // request 6 bytes from slave device
  while(Wire.available()) { // slave may send less than requested
    char c = Wire.read(); // receive a byte as character
    Serial.println(c); // print the character
  }
  delay(500); // wait half a second
}

SLAVE:

#include <Wire.h>
int flag = 0;
#define SLAVE_ADDRESS 0x04

void setup() {
  Wire.begin(SLAVE_ADDRESS); // join i2c bus with address
  Wire.onReceive(receiveEvent); // register receive event
  Wire.onRequest(requestEvent); // register request event
}

void loop() {
 delay(100);
}

void receiveEvent(int numBytes) {
  int c;
  while(Wire.available()) {
    c = Wire.read(); // receive byte as a character
    Serial.print(c); // print the character
  }
    if(c == 0x00) flag = 1;
    else if (c == 0x01)flag = 0;
}
void requestEvent() {
  
  if(flag == 0) Wire.write("hello!");
  else if(flag == 1) Wire.write("bye!");
  
}

Thank you!

I hope that you're not using A4 and A5 on the Mega; those are not the I2C pins.

no, I am using 20 and 21

I2C was designed for chip to chip communications on a single PCB and generally doesn't work with wires more than a few cm long. It is a very poor choice for Arduino to Arduino communications.

UART serial is much preferred.

Just making sure, you wouldn't be the first one :wink:

If you run the I2C scanner on the Mega, does it pick up the I2C address of the Nano?

Addresses 0..7 are officially reserved; you can try a higher address as well on the Nano.

Hi,
Did you connect GND between UNO and MEAG?
I tested your codes here with a UNO and a MEGA,
mega printed correctly:

h
e
l
l
o
!
b
y
e
!
⸮
⸮
h
e
l
l
o
!
b
y
e
!
⸮
⸮
h
e

Addresses up to 8 are reserved for special functions and should never be used for slaves.

I have logic analyser connected to the SDA and SCL lines and I get communication between both of them:



And I see the Wire.onRequest() is working but not the Wire.onReceive()...

Yes, the arduino nano I am using is not an original... maybe this is causing the problem? I will order original one and try again.

You don't have to buy an original Arduino board, as long as you have a original ATmega328P on your Nano clone.

Looking at the code, I don't see the problem. @ruilviana even tested it, and it behaves exactly as expected (including the two '?').

What is going on ?
Did you show the same sketch that you used for the test ?
Do you have pullup resistors ? Is the GND connected ? How long are your wires ? I hope you don't have a flat ribbon cable with SCL next to SDA.

The I2C address should be at least 8, but for other boards, even 8 might be too low. Can you make the I2C address 0x20 or so ?

Can you read all the answers once more ? We mention GND and I2C address for a good reason.

Where does the word "Nextion" come from ? Is there a Nextion display somewhere in your project ?

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