I2C Gyro problem

Hi, I bought an Arduino Diecimila the other day and have wrote a few mess around programs for it, works fine.

I’m trying to convert some code I wrote for the PIC18F a while back, but am having some trouble with it, the Arduino serial I/O only receives ‘5’ no matter what.

I’m interfacing the Arduino with a gyroscope that uses a MCP3421 A/D converter which enables I2C out.

The gyro is set up exactly the same and connects to the Arduino using:
SCL => Analogue 5
SDA => Analogue 4

The original code for the PIC is:

#define BAUD 2400

void main()
{
unsigned short i;
long int gyro=0;

ADCON1 = 0xF;
PORTA = 0;
TRISA = 0;
TRISB = 0;
TRISC.F0 = TRISC.F1 = TRISC.F2 = TRISC.F5 = 0;
Usart_Init(BAUD);
I2C_Init(100000);
I2C_Start();
I2C_Wr(0b11010000);
I2C_Wr(0b00011000);
I2C_Stop();

do
{
I2C_Start();
I2C_Wr(0b11010001);;
gyro = I2C_Rd(0);
Usart_Write(gyro);
I2C_Stop();
Delay_ms(10);

}
while(1);
}

The code I have wrote for the Arduino is:

#include <Wire.h>

int addr = 0x68;
void setup()
{
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(addr);
Wire.send(0b0000);
Wire.send(0b0001);
Wire.send(0b1000);
Wire.endTransmission();
}

void loop()
{
byte gyro_x = 0;
Wire.beginTransmission(addr);
Wire.send(0b0001);
Wire.endTransmission();
Wire.requestFrom(addr, 1);
while(Wire.available())
{
gyro_x = Wire.receive();
}
Serial.println(gyro_x);
delay(10);
}

I’m not sure where I am going wrong, but I have a feeling it is something to do with the: while(Wire.available()) part of the code.

Any help would be very much appreciated.

Thanks in advance.

You probably want something like

// wait until there is data to read
while(Wire.available() < 1)
 {;}
// now read it
 gyro_x = Wire.receive();

-j

You need to put a delay() between endTransmission() and requestFrom().

e.g.:

void loop()
{
 byte gyro_x = 0;
 Wire.beginTransmission(addr);
 Wire.send(0b0001);
 Wire.endTransmission();
 delay(10);
 Wire.requestFrom(addr, 1);
 while(Wire.available())
 {
   gyro_x = Wire.receive();
 }
 Serial.println(gyro_x);
 delay(10);
}

!c

Vygrx,

Did you figure out how to interface the I2C gyro?

I seem to have the same problem as you had. I'm only reciving the value 5 on the I2C link.

I tried copying you code pluss the hints on this thread, but with the same result.

I appreciate some hints ;-)

hi, first of all apologies about my bad english.

I think that the problem is the address that you use, i2c use seven bit address and the 8th bit is the write or read command but this is the LSB bit, and the wire.h library made the desplazament of the address to free the this bit and put one or cero for write or read. Then for a address like 0x64 from datasheet, in my case magnetic sensor MMC212xM, really you need to put 0x32, then when wire.h made the desplazament to put the R/W bit the address is correct. In your case i'll try with 0x34.

I hopefull you understand the explanation.

Did you solve the problem? Having the same problem here...