Mistery: I2C not working, blink working

What it can be?

We can safely rule out your code.

We can probably rule out your set-up / connection.

Everything else is on the table, however.

Ok, code is from examples for L3G, it works on uno, but don't work on mega. Don't think it need to be checked.
Addresses are default, that proved by uno run.
Using this imu: Pololu - MinIMU-9 Gyro, Accelerometer, and Compass (L3G4200D and LSM303DLM Carrier)

I don't use any resistors. Tried to use 3.3V and 5V, imu board uses vin voltage for I2C voltage.

And how can be explained that same circuit and same code works on arduino with 328p and hangs on arduino with 2560?
As I said before, I used different pins on uno and mega. And different voltages too.

L3G library uses wire library.

I wonder if mega I2C circuit is different to uno circuit(except I2C pins placement).

I also noticed that my imu already have pullup resistors

Ok, code is from examples for L3G, it works on uno, but don't work on mega. Don't think it need to be checked.

You would be wrong their.

And how can be explained that same circuit and same code works on arduino with 328p and hangs on arduino with 2560?

Code or how you wire it up.
There can be no other explanation. You are not helping us much because you are providing virtually no information.

Links to the code, photographs of your working setup with the Uno and non working setup with the Mega.

As requested:
Uno connection:

Mega connection:

Main code:

#include <Wire.h>
#include <L3G.h>

L3G gyro;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  
  if (!gyro.init())
  {
    Serial.println("Failed to autodetect gyro type!");
    while (1);
  }

  gyro.enableDefault();
}

void loop() {
  gyro.read();

  Serial.print("G ");
  Serial.print("X: ");
  Serial.print((int)gyro.g.x);
  Serial.print(" Y: ");
  Serial.print((int)gyro.g.y);
  Serial.print(" Z: ");
  Serial.println((int)gyro.g.z);

  delay(100);
}

Library: GitHub - pololu/l3g-arduino: Arduino library for Pololu L3G4200D and L3GD20 boards

Thanks for that.
Unfortunately (for you) everything looks fine with that to me.
You could have the two lines swapped over on the Mega but I can't see one way or the other from the photograph.
Have you done anything else with the I2C on the Mega? It could be that those pins are damaged.
Try blinking an LED on them.

Blink working on each pin. Swaping is not helpful, but i admit that circuit is right. I used this mega for i2c month ago. Can it be something with ic atmega2560?

Can it be something with ic atmega2560?

I would not have thought so.

Grumpy_Mike:

Can it be something with ic atmega2560?

I would not have thought so.

So, what is your diagnosis?

Well from my experience when this sort of thing happens, it always turns out to be the thing that is obviously right and dosn't need checking.

Use a scope to determin if the mega is sending out any I2C signals.

I don't have oscilloscope yet. :~

It may sound too simple, but how are you powering the setup? I have had I2C issues like this when trying to run from the USB power. Switch to a real power source and see if it changes your result.

Hello again. I bought oscilloscope today and tried to measure what's going on.

Same programm on arduino uno and arduino mega. Right connection to both.

This is oscillogramm from uno(it's good):
On mega no impulses appear.

I also made an experiment. I wrote this code to mega and use oscilloscpe with it:

void setup()  { 
  // nothing happens in setup 
} 

void loop()  { 
 while(true){
    analogWrite(21, 0);         
    delay(1000);
    analogWrite(21, 100);         
    delay(1000);
    analogWrite(21, 255);         
    delay(1000);    
  } 
}

But i saw only one impulse at 5V with 1sec duration and then 2sec 0V.

My theory is that DAC is broken, but I still don't know why I2C isn't working.

Anyway powering from battery doesn't give any results.

    analogWrite(21, 0);

Does pin 21 have a little squiggly next to it to indicate that it is a PWM pin? If not analogWrite isn't going to do anything.

PaulS:

    analogWrite(21, 0);

Does pin 21 have a little squiggly next to it to indicate that it is a PWM pin? If not analogWrite isn't going to do anything.

Oh, my fault, you are right. But it not explains why does not I2C working.

As I understand I2C is digital protocol, but why we can use ony 2 pins for it?

As I understand I2C is digital protocol, but why we can use ony 2 pins for it?

Because the AVR chip uses special internal hardware to support the I2C function and it only wires that function to those two specific pins. Just like the hardware serial function on a standard Uno is only available on pins 0 and 1, same thing going on there. analogWrite (pwm outputs) only works on specific pins because it uses internal chip hardware timers that only wire up to specific pins. All I/O pins can be used as simple digital input or output pins, but most of the special functions of the chip are only supported by specific pins. The AVR datasheet is the best source of information on this topic.

Lefty

retrolefty:

As I understand I2C is digital protocol, but why we can use ony 2 pins for it?

Because the AVR chip uses special internal hardware to support the I2C function and it only wires that function to those two specific pins. Just like the hardware serial function on a standard Uno is only available on pins 0 and 1, same thing going on there. analogWrite (pwm outputs) only works on specific pins because it uses internal chip hardware timers that only wire up to specific pins. All I/O pins can be used as simple digital input or output pins, but most of the special functions of the chip are only supported by specific pins. The AVR datasheet is the best source of information on this topic.

Lefty

Can this special internal hardware be damaged?

Can this special internal hardware be damaged?

Any hardware can.