GY-50 L3G4200 gyroscope

I recently purchased the title component:

and I found this guide to do the wiring (step 5). It's pretty similar other than that my power-in says VCC instead of VIN โ€” clearly I'm new to a lot of this stuff :slight_smile: I'm using a duemilanove Arduino to read the values. The problem is that both the SDA and SCL values are always coming across in the range [668,671], and won't change no matter how I move the component around. I've got GND hooked up to GND, VCC to 5V and SCL / SDA to analog pins 0 and 1.

I assume I'm doing something wrong (or my component is screwed up). Does anyone have some obvious fix / advice / a direction to try? I'm a bit lost after experimenting. Thanks!

You should do this step by step.

What voltage needs your breakout board ? 5V or 3.3V ?
Are there pull-up resistors on your breakout board for the SDA and SLC lines ?
I check the pictures, and there is a voltage regulator on the breakout board for 3.3V. So you may connect +5V to Vcc. It seems that the pull-up resistors for the SDA and SCL are 10k. It is safer if they would be 4k7. So you could try it as it is, and add later two extra 10k resistors from the SDA and SCL lines to the 3.3V (not the 5V !).

If you have solved that, run the i2c_scanner to see if the sensor can be detected : http://arduino.cc/playground/Main/I2cScanner
If the i2c_scanner can find your sensor, you should find a good sketch for the sensor.

Thanks for taking a look.

What do you mean by from the SDA and SCL lines to the 3.3v?
I tried running the I2C scanner, and it couldn't find anything. Do I need to change my wiring at all to get that to work?

edit: found some diagrams on the I2C page, so I'm going to try that first.

edit2: I tried this: and got the same values and the I2C scanner still came up with nothing.

The breakout board has a pull-up of 10k for the CS, so it is already in i2c mode.

You only need to make 4 connections.
+5V to Vcc, GND, SCL and SDA.
Could you try that ?
You could upload a photo of it, so we can see how it is connected.

I just got it working. Here's the wiring:

and here's the code:

//Arduino 1.0+ only

#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

int L3G4200D_Address = 105; //I2C address of the L3G4200D

int x;
int y;
int z;

void setup(){

  Wire.begin();
  Serial.begin(9600);

  Serial.println("starting up L3G4200D");
  setupL3G4200D(2000); // Configure L3G4200  - 250, 500 or 2000 deg/sec

  delay(1500); //wait for the sensor to be ready 
}

void loop(){
   getGyroValues();  // This will update x, y, and z with new values

  Serial.print("X:");
  Serial.print(x);

  Serial.print(" Y:");
  Serial.print(y);

  Serial.print(" Z:");
  Serial.println(z);

  delay(100); //Just here to slow down the serial to make it more readable
}

void getGyroValues(){

  byte xMSB = readRegister(L3G4200D_Address, 0x29);
  byte xLSB = readRegister(L3G4200D_Address, 0x28);
  x = ((xMSB << 8) | xLSB);

  byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  y = ((yMSB << 8) | yLSB);

  byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  z = ((zMSB << 8) | zLSB);
}

int setupL3G4200D(int scale){
  //From  Jim Lindblom of Sparkfun's code

  // Enable x, y, z and turn off power down:
  writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

  // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

  // Configure CTRL_REG3 to generate data ready interrupt on INT2
  // No interrupts used on INT1, if you'd like to configure INT1
  // or INT2 otherwise, consult the datasheet:
  writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

  // CTRL_REG4 controls the full-scale range, among other things:

  if(scale == 250){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  }else if(scale == 500){
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
  }else{
    writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
  }

  // CTRL_REG5 controls high-pass filtering of outputs, use it
  // if you'd like:
  writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}

void writeRegister(int deviceAddress, byte address, byte val) {
    Wire.beginTransmission(deviceAddress); // start transmission to device 
    Wire.write(address);       // send register address
    Wire.write(val);         // send value to write
    Wire.endTransmission();     // end transmission
}

int readRegister(int deviceAddress, byte address){

    int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // register to read
    Wire.endTransmission();

    Wire.requestFrom(deviceAddress, 1); // read a byte

    while(!Wire.available()) {
        // waiting
    }

    v = Wire.read();
    return v;
}

Thanks again for the help!

this might be helpful: Digital gyro, acc, kalman, pitch, roll tutorial - Sensors - Arduino Forum

richardpianka:
I just got it working.

Good job !

richardpianka:
I just got it working. Here's the wiring:

and here's the code:

//Arduino 1.0+ only

#include <Wire.h>

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24

int L3G4200D_Address = 105; //I2C address of the L3G4200D

int x;
int y;
int z;

void setup(){

Wire.begin();
Serial.begin(9600);

Serial.println("starting up L3G4200D");
setupL3G4200D(2000); // Configure L3G4200 - 250, 500 or 2000 deg/sec

delay(1500); //wait for the sensor to be ready
}

void loop(){
getGyroValues(); // This will update x, y, and z with new values

Serial.print("X:");
Serial.print(x);

Serial.print(" Y:");
Serial.print(y);

Serial.print(" Z:");
Serial.println(z);

delay(100); //Just here to slow down the serial to make it more readable
}

void getGyroValues(){

byte xMSB = readRegister(L3G4200D_Address, 0x29);
byte xLSB = readRegister(L3G4200D_Address, 0x28);
x = ((xMSB << 8) | xLSB);

byte yMSB = readRegister(L3G4200D_Address, 0x2B);
byte yLSB = readRegister(L3G4200D_Address, 0x2A);
y = ((yMSB << 8) | yLSB);

byte zMSB = readRegister(L3G4200D_Address, 0x2D);
byte zLSB = readRegister(L3G4200D_Address, 0x2C);
z = ((zMSB << 8) | zLSB);
}

int setupL3G4200D(int scale){
//From Jim Lindblom of Sparkfun's code

// Enable x, y, z and turn off power down:
writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

// If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

// Configure CTRL_REG3 to generate data ready interrupt on INT2
// No interrupts used on INT1, if you'd like to configure INT1
// or INT2 otherwise, consult the datasheet:
writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

// CTRL_REG4 controls the full-scale range, among other things:

if(scale == 250){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
}else if(scale == 500){
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
}else{
writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
}

// CTRL_REG5 controls high-pass filtering of outputs, use it
// if you'd like:
writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
}

void writeRegister(int deviceAddress, byte address, byte val) {
Wire.beginTransmission(deviceAddress); // start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); // end transmission
}

int readRegister(int deviceAddress, byte address){

int v;
Wire.beginTransmission(deviceAddress);
Wire.write(address); // register to read
Wire.endTransmission();

Wire.requestFrom(deviceAddress, 1); // read a byte

while(!Wire.available()) {
    // waiting
}

v = Wire.read();
return v;

}




Thanks again for the help!

I have the same 3-axis sensor as you do, which is exactly the same as that picture shows.

However, i think the schematics is not completely accurate for the sensor you used in the picture; it works when you connect "CS" pin to Vcc instead of "SDO" because datasheet of L3G4200D said CS = '1' stands for I2C communication, CS = '0' stands for SPI communication, and the code we use is for I2C.

Wish it helps for the people who are using the same sensor :slight_smile:

What is the range of outputs for x,y, and z. Are they in degrees per second or how do I convert them?

how do we set up a code to run a servo or stepper motor? i have the same setup as well UNO plus L3G4200D thanks guys!