11 years, 1 month ago.

i2c communication with L3GD20

I am starting to communicate with an L3GD20 compass with I2C communication. I have added a few debug lines in to check over serial what is stored in registers. The problem I am running into is that it seems the data I am receiving from the gryo is incorrect.

byte L3G::readReg(byte reg)

byte L3G::readReg(byte reg)
{
    byte test;
    char value[1];
    char data[1];
    data[0] = reg;
    Wire.write(address,data,1);
    test = Wire.read(address,value,1);
    #ifdef _DEBUG
        debug.printf("ReadReg()\n\r Reg: %X | Data: %X | Value: %X | Test: %X \n\r",reg,data[0],value[0],test);
    #endif
    return value[0];

Output

This is the output.
_device: 0 | device: 0
ReadReg()
 Reg: F | Data: F | Value: 74 | Test: 48
ReadReg()
 Reg: F | Data: F | Value: 74 | Test: 48
ReadReg()
 Reg: F | Data: F | Value: 74 | Test: 48
ReadReg()
 Reg: F | Data: F | Value: 74 | Test: 48
Failed to autodetect gyro type!

This is the function I am getting stuck at. byte is just a simple definition of unsigned char. I noticed that the value for test is different then the value for data[0], which from my understanding they should be the same. This value should be equal to 0xD3 or 0xD4.

1 Answer

11 years, 1 month ago.

I assume you mean the value of test would be equal to the value of of 'value'? Since data is the byte you send to the device.

Not that it matters, test is what the function returns, which is not the same as what it reads. The value read is placed in 'value'. In test the return code of the I2C bus is placed, unless everything went fine, then 0 is returned. You got 0x48 back, which means slave address + read has been sent, but no ACK received: Your L3GD20 doesn't respond.

Are you using the correct 8-bit address? So the address you setup should be between 0xD0 and 0xD3, depending on your address pin.

Accepted Answer

Yeah sorry the words are similar. The address that I am writing to is is 0xD0,0xD2,0xD4,0xD6. These are the four type of gyro's I am checking for. So I do except some not to respond as this method is used to find which of the four registers it will respond to but i guess it isn't responding to any.

Thank you for the clarification on the return part of the function.

0xD0 and 0xD3 is the expected response back from writing to the correct register and this would let me define all the proper registers for the right version of the device.

posted by Brion Fuller 10 Mar 2013

I found the problem. After you stated correct 8-bit Address it hit me. I was shifting the address to a 7-Bit Addressing scheme and this caused the address to be incorrect. I changed the address and it works like a charm. Thank you for the help, it always nice to get some help form another mind.

posted by Brion Fuller 12 Mar 2013