Wire.h with Mega2560 -> Uno Rev 3 problem

Is there any reason that I should not be able to use the Mega2560 as the master and the Uno Rev3 as the slave using the Wire.h library and the example sketches (master_writer on the Mega2560 and slave_receiver on the Uno). They are wired …

Mega GND → Uno GND
Mega SDA (pin 20) → Uno SDA (pin A4)
Mega SCL (pin 21) → Uno SCL (pin A5)

Seems to hang on endTransmission on the master with no activity ever on the slave

On the Mega (some serial output added for debugging) …

// Wire Master Writer
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Writes data to an I2C/TWI slave device
// Refer to the "Wire Slave Receiver" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

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

byte x = 0;

void loop()
{
  Serial.println("starting transmission");
  Wire.beginTransmission(4); // transmit to device #4
  Serial.println("write value");
  Wire.write("x is ");        // sends five bytes
  Wire.write(x);              // sends one byte  
  Serial.println("end transmission");
  Wire.endTransmission();    // stop transmitting
  Serial.println("increment value");
  x++;
  Serial.println(x);
  delay(500);
}

On the Uno

// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>

// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this

// Created 29 March 2006

// This example code is in the public domain.


#include <Wire.h>

void setup()
{
  Wire.begin(4);                // join i2c bus with address #4
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);  // start serial for output
  Serial.println("waiting...");
}

void loop()
{
  delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
  while(1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

I tried it with and without a pullup on the Uno pins A4 and A5

Worked fine for me, with no pull-up.

x is 68
x is 69
x is 70
x is 71
x is 72
x is 73
x is 74
x is 75
x is 76
x is 77
x is 78
x is 79
x is 80
x is 81
x is 82
x is 83
x is 84
x is 85
x is 86
x is 87
x is 88
x is 89
x is 90
x is 91
x is 92
x is 93

Is there any reason that I should not be able to use the Mega2560 as the master and the Uno Rev3 as the slave using the Wire.h library and the example sketches (master_writer on the Mega2560 and slave_receiver on the Uno).

No, there isn't.

Are both boards powered? You should see the green power LED on both of them.

Thank you very much for confirming that it should work.

I have separate power to both and both are lit up.

The mega, unfortunately is a counterfeit (Says Arduino Mega 2560, cost the same, but turned out to be a knock off). I don't completely trust that it is not the problem.

I will swap master and slave and see what happens. If still no go, I will breadboard an atmega328 with the uno and try it that way.

I have taken this apart and put it together many times, so I am very confident that what I described is what I am doing so I have to think something is not behaving right.

Thank you again.