communication between arduino mega 2560 and arduino mega 2560 using i2c

Hi i am new to arduino software and i had a hard time having the arduino mega 2560 to communicate
i have use these code to communicate between the arduino mega 2560 however there the LED does not seem to light up.i have taken this code from Arduino Tutorial #10: Arduino to Arduino I2C - YouTube

master code

[#include <Wire.h>

void setup()
{
Serial.begin(9600);

Wire.begin();
}

void loop()
{
while(Serial.available())
{
char c = Serial.read();

if(c == 'H')
{
Wire.beginTransmission(5);
Wire.write('H');
Wire.endTransmission();
}
else if(c == 'L')
{
Wire.beginTransmission(5);
Wire.write('L');
Wire.endTransmission();
}
}
}]

slave
[//i2c Slave Code
#include <Wire.h>

void setup()
{
Wire.begin(5);
Wire.onReceive(receiveEvent);

pinMode(13,OUTPUT);
digitalWrite(13,LOW);
}

void loop()
{
}

void receiveEvent(int howMany)
{
while(Wire.available())
{
char c = Wire.read();

if(c == 'H')
{
digitalWrite(13,HIGH);
}
else if(c == 'L')
{
digitalWrite(13,LOW);
}
}
}]

Linda04:
Hi i am new to arduino software and i had a hard time having the arduino mega 2560 to communicate
i have use these code to communicate between the arduino mega 2560 however there the LED does not seem to light up.i have taken this code from https://www.youtube.com/watch?v=Jndb2vpAWwU

master code

#include <Wire.h>

void setup()
{
 Serial.begin(9600);
 
 Wire.begin();
}

void loop()
{
 while(Serial.available())
 {
   char c = Serial.read();
   
   if(c == 'H')
   {
     Wire.beginTransmission(5);
     Wire.write('H');
     Wire.endTransmission();
   }
   else if(c == 'L')
   {
     Wire.beginTransmission(5);
     Wire.write('L');
     Wire.endTransmission();
   }
 }
}




slave


//i2c Slave Code
#include <Wire.h>

void setup()
{
 Wire.begin(5);
 Wire.onReceive(receiveEvent);
 
 pinMode(13,OUTPUT);
 digitalWrite(13,LOW);
}

void loop()
{
}

void receiveEvent(int howMany)
{
 while(Wire.available())
 {
   char c = Wire.read();
   
   if(c == 'H')
   {
     digitalWrite(13,HIGH);
   }
   else if(c == 'L')
   {
     digitalWrite(13,LOW);
   }
 }
}

when they say put your code between code tags, they mean

[code]  and  [/code]

not [ and ]

The code looks like it would work, but they used UNO's in their example. The I2C bus uses different pins on the MEGA, There are two places the I2C bus is connected on the UNO, pin's A4, A5 and SDA, SCL (next to the Reset Button, past digital pin13)

The Mega also uses SDA and SCL pins near the USB connection , past digital pin 13, and on pin20(SDA), and pin 21 (SCL).

How have you connected your Mega's?

Have you connected the 4.7k pullups?

Chuck.

Linda04:
Hi i am new to arduino software and i had a hard time having the arduino mega 2560 to communicate

With four available serial ports on the Mega, I wonder why you want to do it this way

so which is better i2c method or the serial ports on the mega?

because i need to use more than 2 arduino mega 2560

Linda04:
so which is better i2c method or the serial ports on the mega?

I thought I would ask you. I can understand using I2C as a serial port substitute on a Uno, as it is probably a better bet than software serial, but I bet you haven't got a good reason to use I2C for comms when a Mega has four hardware serial ports.

Linda04:
because i need to use more than 2 arduino mega 2560

That answers nothing. Tell me when you need to have a Mega talk to four Mega 2560s - and your serial monitor.

Linda04:
so which is better i2c method or the serial ports on the mega?

Better without qualifiers is hard to answer.

The Mega's have 4 hardware Serial ports, the Arduino software environment support the Serial devices pretty good. Each of the port has a 63 byte input and output buffer, the are interrupt driven in the back ground. As long as you can allocate the 8 pins and 4*(64+64+overhead ~20) databytes of RAM, Serial is easier to use.

I2C is a shared databus so all of the Mega's only need 3 wires GND,SCL,SDA. The Wire.h library use 3, 32byte buffer plus about 20 bytes of overhead. so I2C uses less resources, but ONLY ONE Mega could talk to ONE Mega at a time. You would have to hand multiple master collisions, Retry and less through put.

The Serial ports can communicate at up to almost a mega baud. (very short wires, clean or with RS232 drivers and slower (19200 baud) upto 50M of cable.), the I2C bus is much slower, either 100k or 400k and should be limited to about 50cm of network.

With 5 Mega's cross connected, each could have a direct line to each of the other 4 Mega's. so you could have 4, 2way conversations ongoing at the same time, were as the I2C bus only support one conversation in one direction at a time.

the Serial port offer higher bandwidth, but consume more resources. Depending on you data flow, That would be my deciding factor. I use Serial communication in a multidrop configuration using RS485 drivers, but there is software overhead, and the RS485 bus is simplex (one way conversations).

But RS485 allow me to connect up to 32 Mega's over 1000' of network. Each has trade offs, you have to choose what is most important to you.

Chuck.

For the project I need to use three mega along with two lcd
For the first mega there is three led ,red, yellow ,green with button
When the button is press (this is when the first mega talk to we)the second mega will display "time to stop"while the program is running the led will keep on blinking once display,(this is when second mega talk to third mega) the third mega will turn off green led and turn on red led and the second lcd display "ok info received".
Throughout the whole program I will not be using the serial monitor

To Nick_Pyner
It is due to my lack of research for communication between the mega and confusion due various methods to use .Also the lack of understanding of software serial and hardware serial port which made me choose i2c.

Linda04:
To Nick_Pyner
It is due to my lack of research for communication between the mega and confusion due various methods to use .Also the lack of understanding of software serial and hardware serial port which made me choose i2c.

Well I guess that's right - on both counts, and knowing a little about what you are holding in your hand would be very helpful. As it is, what you are doing, and the goals, are incomprehensible but, if you really need three Arduinos talking, using Megas is probably a wise move.

Since the Mega has four proper serial ports. I submit they would be the thing to use, simply because that is the common practice, the description of your project surely doesn't suggest any theorising is merited, and using software serial is even dumber than using I2C.

I'm sure there is a swag of pre-existing code around for turning on LEDs with simple commands via serial, probably in the examples in the IDE.

Ok thank you both of you so much