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?

