L3GD20H adafruit breakoutboard I2C issues

Hi, I’m building a quadcopter using an arduino due, a xbee, and a L3GD20H gyro.
I’m finding some problems with the gyro, well its strange, it works fine with arduino uno, but when i connect it to the arduino due, it start to give random values(actually they are not random values).
Here is an examples of what it does:
I’m using Wire library, with a scale of 500dps
I’ll show only one axis

Arduino Uno Arduino Due

x: 0 x:+0
x: 0 x:-0
x: 0 x:+0
x: 0 x:+0
x: 0 x:-0
x:+1 x:+1 rotating upward
x:+2 x:+2
x:+4 x:+5
x:+6 x:+4 getting back to start position.
x:+7 x:+7
x:-6 x:+6036
x:-5 x:+6035
x:-4 x:+6044
x:-2 x:+6050
x:-1 x:+5007
x: 0 x:-0
x: 0 x:+0
x: 0 x:+0

the right values are the one on the arduino uno.

I use the same sketch, for both
here it is the code that I’ve used.

#include <Wire.h> //Include the Wire.h library so we can communicate with the gyro

//Declaring variables
int cal_int;
unsigned long UL_timer;
double gyro_pitch, gyro_roll, gyro_yaw;
double gyro_roll_cal, gyro_pitch_cal, gyro_yaw_cal;
byte highByte, lowByte;

//Setup routine
void setup() {
  Wire.begin();                                      //Start the I2C as master
  Serial.begin(57600);                                //Start the serial connetion @ 9600bps

  //The gyro is disabled by default and needs to be started
  Wire.beginTransmission(107);                       //Start communication with the gyro (adress1101001)
  Wire.write(0x20);                                  //We want to write to register 20
  Wire.write(0x0F);                                  //Set the register bits as 00001111 
  Wire.endTransmission();                            //End the transmission with the gyro
  Wire.beginTransmission(106);                       //Start communication with the gyro (adress1101001)
  Wire.write(0x23);                                  //We want to write to register 23
  Wire.write(0x90);                                 
  Wire.endTransmission();                            //End the transmission with the gyro


  delay(250);                                        //Give the gyro time to start

  //Let's take multiple samples so we can determine the average gyro offset
  Serial.print("Starting calibration...");           //Print message
  for (cal_int = 0; cal_int < 4000 ; cal_int ++) {   //Take 2000 readings for calibration
    gyro_signalen();                                 //Read the gyro output
    gyro_roll_cal += gyro_roll;                      //Ad roll value to gyro_roll_cal
    gyro_pitch_cal += gyro_pitch;                    //Ad pitch value to gyro_pitch_cal
    gyro_yaw_cal += gyro_yaw;                        //Ad yaw value to gyro_yaw_cal
    if (cal_int % 100 == 0)Serial.print(".");        //Print a dot every 100 readings
    delay(4);                                        //Wait 4 milliseconds before the next loop
  }
  //Now that we have 2000 measures, we need to devide by 2000 to get the average gyro offset
  Serial.println(" done!");                          //2000 measures are done!
  gyro_roll_cal /= 4000;                             //Divide the roll total by 2000
  gyro_pitch_cal /= 4000;                            //Divide the pitch total by 2000
  gyro_yaw_cal /= 4000;                              //Divide the yaw total by 2000

}
//Main program
void loop() {
  delay(250);                                        //Wait 250 microseconds for every loop
  gyro_signalen();                                   //Read the gyro signals
  print_output();                                    //Print the output
}

void gyro_signalen() {
  Wire.beginTransmission(107);                       //Start communication with the gyro (adress1101001)
  Wire.write(168);                                   
  Wire.endTransmission();                            //End the transmission
  Wire.requestFrom(107, 6);                          //Request 6 bytes from the gyro
  while (Wire.available() < 6);                      //Wait until the 6 bytes are received
  lowByte = Wire.read();                             //First received byte is the low part of the angular data
  highByte = Wire.read();                            
  gyro_roll = ((highByte << 8) | lowByte);           //Multiply highByte by 256 and ad lowByte
  if (cal_int == 4000)gyro_roll -= gyro_roll_cal;    //Only compensate after the calibration
  lowByte = Wire.read();                             //First received byte is the low part of the angular data
  highByte = Wire.read();                            
  gyro_pitch = ((higByte << 8) | lowByte);          //Multiply highByte by 256 and ad lowByte
  gyro_pitch *= -1;                                  //Invert axis
  if (cal_int == 4000)gyro_pitch -= gyro_pitch_cal;  //Only compensate after the calibration
  lowByte = Wire.read();                             //First received byte is the low part of the angular data
  highByte = Wire.read();                           
  gyro_yaw = ((highByte << 8) | lowByte);            //Multiply highByte by 256 and ad lowByte
  gyro_yaw *= -1;                                    //Invert axis
  if (cal_int == 4000)gyro_yaw -= gyro_yaw_cal;      //Only compensate after the calibration
}

void print_output() {
  Serial.print("Pitch:");
  if (gyro_pitch >= 0)Serial.print("+");
  Serial.print(gyro_pitch / 57.14286, 0);            //Convert to degree per second
  if (gyro_pitch / 57.14286 - 2 > 0)Serial.print(" NoU");
  else if (gyro_pitch / 57.14286 + 2 < 0)Serial.print(" NoD");
  else Serial.print(" ---");
  Serial.print("  Roll:");
  if (gyro_roll >= 0)Serial.print("+");
  Serial.print(gyro_roll / 57.14286, 0);             //Convert to degree per second
  if (gyro_roll / 57.14286 - 2 > 0)Serial.print(" RwD");
  else if (gyro_roll / 57.14286 + 2 < 0)Serial.print(" RwU");
  else Serial.print(" ---");
  Serial.print("  Yaw:");
  if (gyro_yaw >= 0)Serial.print("+");
  Serial.print(gyro_yaw / 57.14286, 0);              //Convert to degree per second
  if (gyro_yaw / 57.14286 - 2 > 0)Serial.println(" NoR");
  else if (gyro_yaw / 57.14286 + 2 < 0)Serial.println(" NoL");
  else Serial.println(" ---");
}

Can someone help me?
thanks

It seems likely that problem arises from the difference in default sizes for integers (16 bits on the Uno, 32 bits on the Due), so the sensor values are being reassembled incorrectly from the data bytes. You may need to typecast variables correctly.

That said, the code won’t even compile because of this line:

  gyro_pitch = ((higByte << 8) | lowByte);          //Multiply highByte by 256 and ad lowByte

actually it compiles and uplode it, do you know a way to solve this problem?
At first i thought that it was due to the different operating volteges of the two devices.

actually it compiles and uplode it, do you know a way to solve this problem?

The code you posted does not compile and cannot possibly work, because it references an undefined variable “higbyte”.

Post the code that does compile, correctly.

word : word() - Arduino Reference