Communication Mega to Uno

Hi everybody, i'm new in the Arduino world and i have a project to do. (Sorry for my english i'm french).

For this i need to communicate from my arduino Mega to my Uno. Something very easy just like transmit a word or a value.

I know that i've to connect the tx with rx and rx to tx pins and also both gnd together.

I have seen a lot of topics like that in internet but nothing fit and whatever i do in order to change the code i've collected, nothing works ....

I've been working on this for a long time but i don't anything.

Does something have something to help me ? Thank you very much for your help

On a Mega, you have additional serial ports; use one of those. If you do not need the serial port of the Uno, you can use that. Else you'll have to use software serial. There are limitations with software serial. There are also other libraries that you can use instead of software serial.

You can also use spi or i2c for communicayion between arduinos.

Show what you tried to get better advice.

First thank you for your reply sterretje.

Here a code i've tried to make work.

I've this error : ".(Port busy)" (the reminded message is in french ^^').

I don't know why it doesn't work.

master.ino (189 Bytes)

slave.ino (451 Bytes)

Please insert code in the post using code tags (it's small enough). I'm usinga cellphone and can't view the attachments.

Type [code]
Paste your master code after that
Type [/code] after that

Repeat for the slave.

Your com3 error does not have to do with the code. I assume that you try to develop both sketches at the same time. What I do in that situation is opening both from windows explorer by double clicking the ino file and next select the correct com ports from the comms menu (your mega and uno will have different com ports in that menu.

Here the code for the master, Mega.

#include <Wire.h>
void setup() {
  Wire.begin(3); 

}

void loop() {
  Wire.beginTransmission(9); 
  Wire.write("z");        
             
  Wire.endTransmission();    

  delay(500);
}

And here we have the slave code, that corresponds to Uno

#include <Wire.h>
void setup() {
  Wire.begin();                // join i2c bus with address #9
  Serial.begin(9600);           // start serial for output
}

void loop() {

  Wire.requestFrom(2, 6);    // request 6 bytes from slave device #2
    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);
}

There is things that i don't understand such as when it's written : "slave device#2", i don't know what it corresponds to. (the commentaries come from the copied code, i didn't erase them)

Please follow the wiring and code of the example. Your I2C addressing seems wrong (2 vs 3, client vs master, etc etc). Also make sure you have the wiring right.

Your RX and TX in the opening post put me on the wrong foot :wink: You're not using serial but I2C. So you have to connect SDA to SDA and SCL to SCL (and obviously the ground).

On the Uno R3 and the Mega, you can use the pins that are marked SDA and SCL; they are on the same connector as pins 8..13. It's my understanding that on older Unos those pins are not there and you can use the pins A4 and A5.

With regards to the code, Wire.requestFrom is use by a master to request data from the slave. You however use it in the slave code. Also, as you don't specify an address in the slave code, that code is the master.

Next your code for the master simply writes without waiting for a request; this will more than likely result in collisions on the bus.

If you have a master that requests data (as you seem to have), have a look at the Master Reader/Slave Sender example.

Ah ok i'm going to change the pins, so i connect the pins SDA and SCL of the mega to pins A4 and A5 of my uno, is that right ?

And then, to answer your questions, i don't want my slave (UNO) to send informations to the master (MEGA), i just need the mega to send information (it's going to be values between 1 to 32) to the uno.

So i remove the Wire.requestFrom from the slave ?
In the Slave setup, i put Wire.begin(9), and in the master it's Wire.beginTransmission(9).

Is that correct like that ?

A4 will be SDA and A5 will be SCL

If you want the Mega (master) to send data to the Uno (slave), you can follow this example

Below the schematic of the Uno R3
Uno schematic
and for completeness the schematic of the Mega R3
Mega schematic

Thank you very much it works very well !!! :smiley: