MEGA and I2C comm

I'm trying to talk to a I2C device using the Arduino MEGA and the wire library. I'm using the SDA and SCL ports. According to the schematics they already have 10k pull-up resistors, so I just hooked it up directly.

I'm fairly certain I got the address right and the chip is powered on. However, if I'm requesting, what's supposed to be a constant value, I get various read backs all over the place. I'm wondering if there's something wrong with the timing.

I was wondering if anybody had any luck hooking up a MEGA to an I2C. Also I have seen different implementations for endTransmission().

Here's my prototype:

#include <Wire.h>
unsigned int data1, data2, data3;
float temp2;
void setup()
{

  • Wire.begin();*
    }
    void loop()
    {

  • Wire.beginTransmission(0x48);*

  • Wire.send(0x04);*

  • Wire.endTransmission();*

  • Wire.requestFrom(0x48, 2);*

  • if (Wire.available())*

  • {*

  • data1 = Wire.receive();*

  • }*

  • if (Wire.available())*

  • {*

  • data2 = Wire.receive();*

  • }*
    _ data3 = ((data1 * 256 + (data2 & 0xF8))) >> 3;_

  • temp2 = float(data3) / 16;*
    }

I am not sure why, since I have not actually checked the code used by Arduino, but once I was able to make it work just by commenting out the if (Wire.available()) clauses. You could try this, or if you have a logic analyzer or an oscilloscope you could have a look at the actual data transfer.

I've hooked up a I2C RTC module based on the DS1307 and used one of the standard user contributed libary routines and it works fine.

Lefty

I can also confirm that I2C works perfectly..
As Johan Adler already said - try to check only once and receive both bytes a t a time. I have in mind that there are certain setbacks in the wire library that might read the returned bytes wrong if you call available() in between running transmissions...

 if (Wire.available())
 {
   data1 = Wire.receive();
   data2 = Wire.receive();
 }

The other question is qhat 0x04 actually commands the device to do, maybe it is some sort of reset signal and is meant only to be sent once? Maybe it would help to put it into setup?

I tried leaving out the Wire.available() to no avail. Also, if I actually read out the Wire.available() I get 2 bytes being available and 0 after I read them out, so it seems to be doing something.

The 0x04 is setting the address register to return an internal fixed value (I'm using a ADT7410 temperature sensor).

According to the schematics they already have 10k pull-up resistors, so I just hooked it up directly.

Schematics of what? The data sheet suggests a pull up but you have to put it in. Have you wired A0 & A1 to ground? Have you got supply decoupling on the chip?
The mega uses different pins for I2C than the other arduinos, these are pin 20 for data and 21 for clock.

I looked at the Arduino MEGA schematics at http://arduino.cc/en/uploads/Main/arduino-mega-schematic.pdf. On the top right corner it shows that two 10k resistors are connected to SCL and SDA, respectively, hence I assume I don't need to put in another two pull-up resistors. I did wire A0 & A1 to ground and am using address 0x48. I also am using pins 20 and 21.

I didn't try the decoupling and that seems to improve things quite a bit. The values that I'm reading back are much more reasonable. However, it seems that the send still doesn't work quite well. No matter what address register byte I send, I'm always seem to be getting back the current temperature reading.

At least it's a step in the right direction - Thanks!

HI.
I think you have to use A0 & A1 = ADC0 & ADC1 instead of the SCL SDA.
Remember to use external pull up resistors.

anyone get it to talk to a nunchuck rather through the SCL & SDA or through A0 & A1?

anyone get it to talk to a nunchuck rather through the SCL & SDA or through A0 & A1?

See here