I2C between mega2560 and uno

i want to use mega 2560 as master and uno(mega2560) as slave and act as :

https://www.arduino.cc/en/Tutorial/MasterWriter

im wireing like pic and uploading the programs in uno and mega but i dont observe anything in serial monitor.....

please help me

who can help me?

I have no experience of I2C. I suggest you edit your Original post and add I2C to the title.

...R

TNX ROBIN2....AND WHO ONE CAN HELP ME????

any one????

ANOOSHEH, I feel your pain. I just bought a 'Mega 2560 R3' I am using the 'Arduino_as_slave' code and use RPi as Master. The 'i2cdetect -y 1" **Utility on RPi **sees the Arduino, but at address '0x21', which does not make sense, because I have the Slave address set at '0xA' in the code !! My Microcontroller address comes out as "34", which is correct. So, go figure.

i2cdetect -y 1

0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- 34 -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

Maybe someone can explain that.

Regards,

Givi

Givi: ANOOSHEH, I feel your pain. I just bought a 'Mega 2560 R3' I am using the 'Arduino_as_slave' code and use RPi as Master. The 'i2cdetect -y 1" Utility on RPi sees the Arduino, but at address '0x21', which does not make sense, because I have the Slave address set at '0xA' in the code !! My Microcontroller address comes out as "34", which is correct. So, go figure.

i2cdetect -y 1

0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- UU -- -- -- -- 20: 20 21 -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- 34 -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --

Maybe someone can explain that.

Regards,

Givi

Post your code, without out it, the possibilities are infinite.

Also, You say you are using a Rpi as a master; Do you have a voltage level shifter on the I2C circuit?

The Mega is 5V the Rpi is 3.3v.

Chuck.

Hi Chuck,

Thanks for prompt response. Yes, I do have a ‘voltage level shifter’ and PIC-F182525 is also connected to the same I2C. Please see attached code on Mega 2560.

Let me know please.

Regards,

Givi

Mega2560.txt (927 Bytes)

I have a RPi as master and a Mega2560 as a slave, and it is working ok. The i2cdetect shows it at the correct address 0D hex.

What logic level shifter are you using? Are you certain it is wired correctly?

Givi:
Hi Chuck,

Thanks for prompt response. Yes, I do have a ‘voltage level shifter’ and PIC-F182525 is also connected to the same I2C. Please see attached code on Mega 2560.

Let me know please.

Regards,

Givi

I commented a couple of critiques in your code.
I changed your two global integers to volatile byte, The volatile modifier is because you are usings these variables in an interrupt service routine, In this EXPLICIT example, they were not necessary. You were not using their value in your main code, but if you ever write a program that does use the variable in it’s main loop, the compiler may create code that holds the ‘current’ value in a CPU register. This allows the compiler to generate faster, smaller code. BUT, your ISR does not know the current value is NOT in RAM, It uses/updates the RAM value which the Main loop code ignores.

So, any time your access a global variable in an ISR, mark that variable as volatile.

Also, I change from int to byte because the I2C Wire library only reads one byte at a time. It allows you to either write ONE byte or a char array. When you use Wire.write(int); it only sends the lower byte, it ignores the upper.

The third nit pick is your use of Serial.print() in the ISR, The Serial() object sends and receives data as a background (interrupt) process. The Arduino’s do not support nested interrupts, so the Serial() processing STOPS while you are processing the receive() callback.

None of the nits should have generated the results you described, as @SurferTim said:

SurferTim:
I have a RPi as master and a Mega2560 as a slave, and it is working ok. The i2cdetect shows it at the correct address 0D hex.

What logic level shifter are you using? Are you certain it is wired correctly?

Your code works, Something else is the problem.

#include <Wire.h>

#define SLAVE_ADDRESS 0xA
volatile byte number = 0; //VOLATILE because value can/will be changed in an interrupt
volatile byte state = 0;

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(9600); // start serial for output
  // initialize i2c as slave
  Wire.begin(SLAVE_ADDRESS);

  // define callbacks for i2c communication
  Wire.onReceive(receiveData);
  Wire.onRequest(sendData);

  //Serial.println(“Ready!”);
}

void loop() {
  delay(100);
}

// callback for received data
void receiveData(int byteCount) {

  while (Wire.available()) { //loop through each byte received
    number = Wire.read();
    Serial.print("data received: "); //possible problem, Serial Output buffer is only 64 bytes deep
  // since we are already in an interrupt callback, no other Interrupts are being processed
 // if these print statements fill the 64byte buffer the program will hang waiting for
 // space to become available in the output queue.  Since interrupts are already disable,
 // it will wait FOREVER  (HANG!!!!!)
    Serial.println(number,DEC);

  if (number == 1) {
    state = !state; // State alternates from 0 to 1
    digitalWrite(13,state); // if state is high led is on
    }
  }
}

// callback for sending data
void sendData() {
  Wire.write(number); //send one byte
}

Chuck.