Problem connecting Arduino Leonardo with Arduino Mega

Hello,

I searched the forum but didn't find anything for my problem.
I followed these instrunctions in Instructables website but instead I used an Arduino Mega and an Arduino Leonardo. I load the examples given though I cant' establish a connection between them.

I connected Leonardo SCL with Mega SCL/21 and Leo SDA with Mega SDA/20. Also I connected one random ground for each.
Both Arduinos are connected to my computer's USBs at the same time for power (COM3 and COM11) and I don't know if that could be a problem.

Here is an image preview from my arduinos.

Instructables are often just about useless. I'm not about to wade through 47 links and shitloads of advertising to find the code you used on each Arduino. You post the code or live with the problem.

Did you at least run the I2C scanner on both Arduinos to make sure everything was connected correctly?

Sorry Paul for didn't post the code. I will do it asap.
I don't know how to use the l2c scanner. How can I do it?

How can I do it?

Google Arduino + I2C scanner. It's a sketch that you load onto the Arduino.

So I connect 2 Arduinos and from 1 Arduino I send anything to another where I load the scanner code and if it gets anything it prints it on screen. Or I don't have to load code to the first Arduino but only the scanner code to any of them?
Sorry but I am new...

So I connect 2 Arduinos

Yes.

and from 1 Arduino I send anything to another

Not necessary.

where I load the scanner code and if it gets anything it prints it on screen.

No. The scanner sketch simply reports the address of any connected I2C devices. Run it on the Mega, when the Leonardo is connected (and running the current sketch). Does the Mega see the Leonardo?

Scanning...
No I2C devices found

What do I do wrong?

Looking at the photo, I think your wiring is okay.

Do you use Arduino IDE 1.6.6 ? If not, please update.

Which Arduino board is the Master and which one is the Slave ?

This is the i2c_scanner : Arduino Playground - I2cScanner
This is a good tutorial (also with a I2C Scanner) : Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

Can you load the Slave sketch into the Slave ?
Either that instructables Slave sketch or the second sketch on this page : https://www.arduino.cc/en/Tutorial/MasterReader
After that, upload the i2c_scanner in the Master and it should detect the Slave. As soon as the Slave is detected, the I2C communication is working and after that you need a good sketch.

The I2C between two Arduino boards has a few things that you must know. A global variable that is changed in the onRequest or onReceive handler must be 'volatile' and you may not use the Serial in the onRequest or onReceive handler. That means that both examples (the instructables and the Arduino example) are both wrong.

Did you know that you can develop both sketches at the same time ?
Start the Arduino IDE, select the serial port and move it to one side of the screen.
Start another instance of the Arduino IDE, select the other serial port.
Then both Arduino IDE work independent of each other, and they both have also each their own serial monitor.

I have 2 MEGAs and 1 Leonardo.
I tried every connection possible but nothing in the scanner.

I repeat:
-I have 2 Arduinos
-connected via USB to my PC for power
-connected SDA-SDA and SCL-SCL and GND-GND

I get no connection...

IDE is 1.0.5.
Now I am downloading newer version and checking again.
Thanks!

Could you describe exactly what your are doing ? Did you load the Slave sketch into the Slave ? which Slave sketch ? Please be very specific, don't mention the page, but tell us which sketch on that page.
Do you have a I2C sensor or some other I2C device, just to check that the i2c_scanner should see it.
If Arduino IDE 1.6.6 still can't make it work, there must be something we are missing.
Perhaps one of the wires is broken ?

Koepel:
Do you use Arduino IDE 1.6.6 ? If not, please update.

Just Installed it.

Koepel:
Which Arduino board is the Master and which one is the Slave ?

Don't have any preferation. Tried both.

Koepel:
This is the i2c_scanner : Arduino Playground - HomePage
This is a good tutorial (also with a I2C Scanner) : Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino

Can you load the Slave sketch into the Slave ?
Either that instructables Slave sketch or the second sketch on this page : https://www.arduino.cc/en/Tutorial/MasterReader
After that, upload the i2c_scanner in the Master and it should detect the Slave. As soon as the Slave is detected, the I2C communication is working and after that you need a good sketch.

I loaded l2C scanner to Leonardo and Slave to MEGA. Both messages
"Scanning...
No I2C devices found"

Is there a specific way to declare which one is the master and which one is the slave?

I can't believe it...

Koepel:
Perhaps one of the wires is broken ?

I can't believe it!!! I changed wires and worked! Wires are brand new but maybe one was defective.
Scanning...
I2C device found at address 0x08 !
done

Thanks a lot!

Though only 1 gives this message.
The other is blank.

Ok I tried the example with "hello" master/slave and works fine!
Thanks again!

Blank ? If it is not wired correctly then nothing is found. But if the i2c_scanner sketch halts, then the SDA and SCL are shortcut to each other or one of them is shortcut to ground.