Outputting wrong values(Gyro - L3G4200D)

#include "I2Cdev.h"
#include <Wire.h>

#define gyro_Addr    0x69
#define CTRL_REG1    0x20
#define CTRL_REG2    0x21
#define CTRL_REG3    0x22
#define CTRL_REG4    0x23
#define CTRL_REG5    0x24
#define OUT_X_L      0x28
#define OUT_X_H      0x29
#define OUT_Y_L      0x2A
#define OUT_Y_H      0x2B
#define OUT_Z_L      0x2C
#define OUT_Z_H      0x2D

int16_t gx, gy, gz;
uint8_t buffer[6];
void setup(){
  Serial.begin(9600);
  Wire.begin();
  init_gyro();
}

void loop(){
  read_gyro(&gx, &gy, &gz);

  Serial.print(gx); Serial.print("    ");
  Serial.print(gy); Serial.print("    ");
  Serial.println(gz);
  
  delay(100);
}

void init_gyro(){
  I2Cdev::writeByte(gyro_Addr, CTRL_REG1, 0x00);  //Nollställer sensorn
  
  I2Cdev::writeByte(gyro_Addr, CTRL_REG1, 0x0F);  //Aktiverar alla axlar(x,y & z)
  
  I2Cdev::writeByte(gyro_Addr, CTRL_REG2, 0x00);  //Sätter "Normal mode"
  
  I2Cdev::writeByte(gyro_Addr, CTRL_REG3, 0x00);  //Avaktiverar interrupts och FIFO
  
  I2Cdev::writeByte(gyro_Addr, CTRL_REG4, 0x20);  //Sätter DPS(degree per sec) = 2 000

  I2Cdev::writeByte(gyro_Addr, CTRL_REG5, 0x10);  //Aktiverar högpassfilter
 
}

void read_gyro(int16_t* gx, int16_t* gy, int16_t* gz){
  
  I2Cdev::readBytes(gyro_Addr, OUT_X_L, 6, buffer);
  
  *gx = (((int16_t)buffer[1]) << 8 ) | buffer[0];
  *gy = (((int16_t)buffer[3]) << 8 ) | buffer[2];
  *gz = (((int16_t)buffer[5]) << 8 ) | buffer[4];


}

edit: Fixed “storage_buffer” to just “storage”

When I use this code to read the values from X, Y and Z axis I get the same values on all of them, anyone know the reason for this ?

Seems pretty solid to me. Would be nice if some1 else could take a closer look at his code.

You read the values in 'buffer', and use 'storage_buffer' to make the x,y and z.

*gx = word( buffer[1], buffer[0]);

Erdin: You read the values in 'buffer', and use 'storage_buffer' to make the x,y and z.

I fixed this... was trying things and forgot to change it back before posting it here...

Thing is I can get the code to compile, and I can get values from the gyroscope, BUT the values on X, Y and Z are always all the same... I've tried to see what the problem is, but I can't find it...

Either show us the whole code (so we can see how many large arrays you have) or use the partial code yourself to test the sensor.

What is the value you are getting which is "all the same". Is it all zeros ?

Your code looks reasonable. I'd be wondering if you are actually getting any response at all. Write some specific known value into your six-byte buffer before you start, and see if it is still there afterwards.

michinyon: What is the value you are getting which is "all the same". Is it all zeros ?

I get different values when I move the sensor, BUT X, Y & Z are always the same (i.e. X = Y = Z). I get values like 2000 depending on how I move the gyroscope...

Well your code still looks reasonable, so it is a mystery.

I would go back to the datasheet and read through the instructions very carefully about how you are
doing the initialization.

I’d look for anyone else’s code for this device and see how they are doing the initialization.

I’d print the contents of that 6-byte buffer to see exactly what is in it ( although the code where
you put it into gx, gy, gz looks ok ).

I’d check the scope of all of the variables you are using in your program, carefully.

Maybe you just have a dud device.

Hi,
i have a similar problem with my L3G4200D.

The setup is nearly the same too.

These are my defined registers:

#define L3G4200D_RA_CTRL_REG1 		0x20
#define L3G4200D_RA_CTRL_REG2 		0x21
#define L3G4200D_RA_CTRL_REG3 		0x22
#define L3G4200D_RA_CTRL_REG4 		0x23
#define L3G4200D_RA_CTRL_REG5 		0x24

#define L3G4200D_RA_OUT_X_L 		0x28
#define L3G4200D_RA_OUT_X_H 		0x29
#define L3G4200D_RA_OUT_Y_L 		0x2A
#define L3G4200D_RA_OUT_Y_H 		0x2B
#define L3G4200D_RA_OUT_Z_L 		0x2C
#define L3G4200D_RA_OUT_Z_H 		0x2D

This is my initial setup:

void L3G4200D::initialize() {
	I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG1, 0b11011111);
	I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG2, 0b00000000);
	I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG3, 0b00001000);
	I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG4, 0b00110000);
	I2Cdev::writeByte(devAddr, L3G4200D_RA_CTRL_REG5, 0b00000000);
}

And these are my methods to read the values:

void L3G4200D::getRotations(int16_t* pitch, int16_t* yaw, int16_t* roll){
    I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_X_L, 6, buffer);
    *pitch = (((int16_t)buffer[1]) << 8) | buffer[0];
    *yaw   = (((int16_t)buffer[3]) << 8) | buffer[2];
    *roll  = (((int16_t)buffer[5]) << 8) | buffer[4];
}

int16_t L3G4200D::getPitch(){
	I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_X_L, 2, buffer);
	return (((int16_t)buffer[1]) << 8) | buffer[0];
}

int16_t L3G4200D::getYaw(){
    I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_Y_L, 2, buffer);
    return (((int16_t)buffer[1]) << 8) | buffer[0];
}

int16_t L3G4200D::getRoll(){
    I2Cdev::readBytes(devAddr, L3G4200D_RA_OUT_Z_L, 2, buffer);
    return (((int16_t)buffer[1]) << 8) | buffer[0];
}

My buffer is
uint8_t buffer[6];

I have the same behavior as matstra when i try to get the values with a call of getRotations( … ).
But not everytime!
And when i call the other methods to get the values i get some “random” values.

getSingle: getPitch() getYaw() getRoll()

getSingle:	-1029	-1029	1545
getRotations:	-1	-1	-1
getSingle:	-1029	1799	771
getRotations:	-3342	-3342	-3342
getSingle:	257	0	1542
getRotations:	-2314	-1290	-1286
getSingle:	-4113	-258	-1543
getRotations:	514	514	1028
getSingle:	257	1285	1028
getRotations:	0	0	0
getSingle:	-1286	1542	257
getRotations:	-3599	-3599	-3599
getSingle:	-258	257	1283
getRotations:	514	514	514
getSingle:	257	1799	0
getRotations:	-2314	-2314	-2314
getSingle:	-3342	1799	1542
getRotations:	-1029	-1029	-1029
getSingle:	-1286	2570	-1029
getRotations:	3855	3855	3855
getSingle:	-2057	-1029	2827
getRotations:	-1286	-1286	-1286
getSingle:	257	-515	3084
getRotations:	-2314	-2314	-2314

I have no idea whats wrong with my code.

Maybe someone of you find the problem or could give a hint.

My Gyro seems to work fine.
I testet it. Test is attached. The code is stripped to the important parts.

Also attached my L3G4200D.h and L3G4200D.cpp

L3G4200DSensorTest.ino (4.03 KB)

L3G4200D.h (13.5 KB)

L3G4200D.cpp (13.6 KB)

I think MSB of OUT_X_L adress should be high in case of i2c communication for auto increment of address adress should be A8 instead of 28......