I2C problem with L3GD20 and LSM303

by vaporlock on Tue Jan 01, 2013 5:01 pm

Happy New Year! Thought I'd start the new year with a little project but it is off track already.

I'm trying to interface these two Adafruit breakout modules with an Arduino. If I try to initialize both devices, only the lsm303 works. Either works fine by itself. I've tried this with an Arduino Mega 2560R3 and an Arduino Micro with the same result. Tried 3.3v and 5v. Tried adding 2K pullups on the SDA and SCL lines. Looking at the .h files, the devices have the same ID but different addresses. I think the l3gd20 is actually using SPI based on the code and suggested wiring although the sample comment implies that it is using i2c. Still a bit over my head. I don't have any other i2c devices to try.

Any help would be appreciated.

The code I'm using is a mashup of the example code:
Code: Select all
#include <Wire.h>
#include <Adafruit_LSM303.h>
#include <Adafruit_L3GD20.h>

#define GYRO_CS 4 // labeled CS
#define GYRO_DO 5 // labeled SA0
#define GYRO_DI 20  // labeled SDA (2 for Arduino Micro)
#define GYRO_CLK 21 // labeled SCL (3 for Arduino Micro)

// this works by itself but isn't i2c is it?
//Adafruit_L3GD20 gyro(GYRO_CS, GYRO_DO, GYRO_DI, GYRO_CLK);

// not in the sample but based on the library, i2c runs should do this -- THIS DOES NOT WORK AT ALL
Adafruit_L3GD20 gyro;

// this always works
Adafruit_LSM303 lsm;

int trygyro = 1;
int trylsm  = 1;

void setup() {
  Serial.begin(9600);
  // add delay so I can open the serial monitor before the init code runs to catch errors
  for(int i = 0;i < 5;i++) {
    Serial.println(i);
    delay(i * 1000);
  }
  Serial.println("go!");
 
  if(trylsm) {
    if (!lsm.begin()) {
      Serial.println("Oops ... unable to initialize the LSM303. Check your wiring!");
      trylsm=0;
    } else {
      Serial.println("lsm working!");
    }
  }
  delay(1000); // throwing in more delays
  if(trygyro) {
    if (!gyro.begin(gyro.L3DS20_RANGE_250DPS))
    //if (!gyro.begin(gyro.L3DS20_RANGE_500DPS))
    //if (!gyro.begin(gyro.L3DS20_RANGE_2000DPS))
    {
      Serial.println("Oops ... unable to initialize the L3GD20. Check your wiring!");
      trygyro=0;
    } else {
      Serial.println("gyro working!");
    }
  }
}

void loop() {
  dolsm();
  delay(1000);
  dogyro();
  delay(1000);
}

void dogyro() {
  if(trygyro) {
    gyro.read();
    Serial.print("X: "); Serial.print((int)gyro.data.x);   Serial.print(" ");
    Serial.print("Y: "); Serial.print((int)gyro.data.y);   Serial.print(" ");
    Serial.print("Z: "); Serial.println((int)gyro.data.z); Serial.print(" ");
  }
}

void dolsm() {
  if(trylsm) {
    lsm.read();
    Serial.print("Accel X: "); Serial.print((int)lsm.accelData.x); Serial.print(" ");
    Serial.print("Y: "); Serial.print((int)lsm.accelData.y);       Serial.print(" ");
    Serial.print("Z: "); Serial.println((int)lsm.accelData.z);     Serial.print(" ");
    Serial.print("Mag X: "); Serial.print((int)lsm.magData.x);     Serial.print(" ");
    Serial.print("Y: "); Serial.print((int)lsm.magData.y);         Serial.print(" ");
    Serial.print("Z: "); Serial.println((int)lsm.magData.z);       Serial.print(" ");
  }
}


The result is:
0
1
2
3
4
go!
Wire
lsm working!
Oops ... unable to initialize the L3GD20. Check your wiring!
Accel X: 24 Y: 4 Z: 1132
Mag X: -27 Y: -12 Z: -586
Accel X: 16 Y: 0 Z: 1120
Mag X: -30 Y: -14 Z: -587

Is the 'Wire' output significant?
vaporlock
 
Posts: 2
Joined: Tue Jan 01, 2013 4:17 pm

Re: I2C problem with L3GD20 and LSM303

by adafruit_support_bill on Wed Jan 02, 2013 6:00 am

The example in the L3GD20 library uses SPI. There was a new version of the library posted last night. I will be testing with it later today.
User avatar
adafruit_support_bill
 
Posts: 25536
Joined: Sat Feb 07, 2009 9:11 am

Re: I2C problem with L3GD20 and LSM303

by vaporlock on Wed Jan 02, 2013 10:17 am

So I'm not going crazy (at least not in that way.) Thanks!
vaporlock
 
Posts: 2
Joined: Tue Jan 01, 2013 4:17 pm

Re: I2C problem with L3GD20 and LSM303

by adafruit_support_bill on Wed Jan 02, 2013 6:15 pm

I tested the new library and it works fine with both I2C and SPI. Just be sure to call the right constructor:

For I2C, use I2C wiring (SDA to SDA and SCL to SCL) and the default constructor:
Code: Select all
// No need to specify pins for I2C
Adafruit_L3GD20 gyro();


For SPI, use this constructor to specify the pins:
Code: Select all
// Define the pins for SPI
#define GYRO_CS 4 // labeled CS
#define GYRO_DO 5 // labeled SA0
#define GYRO_DI 6  // labeled SDA
#define GYRO_CLK 7 // labeled SCL
 
Adafruit_L3GD20 gyro(GYRO_CS, GYRO_DO, GYRO_DI, GYRO_CLK);


We should have full tutorial posted shortly. :D

[edit]
Here's the link: http://learn.adafruit.com/adafruit-trip ... t/overview
User avatar
adafruit_support_bill
 
Posts: 25536
Joined: Sat Feb 07, 2009 9:11 am