Gyroscope L3G4200D in arduino due

Hello guys!
I’m using the gyroscope L3G4200D and i have created two codes,one for the protocol I2C and one for the
SPI.

Code for i2c

#include <Wire.h>

double x, y, z;
byte highByte, lowByte;

void setup()
{
Wire.begin();
Serial.begin(9600);

Wire.beginTransmission(105);
Wire.write(0x20);
Wire.write(0x0F);
Wire.endTransmission();
Wire.beginTransmission(105);
Wire.write(0x23);
Wire.write(0x90);
Wire.endTransmission();

}

void loop()
{
delay(250);
gyro_value();
print_xyz();
}

void gyro_value()
{
Wire.beginTransmission(105);
Wire.write(168);
Wire.endTransmission();
Wire.requestFrom(105, 6);
while(Wire.available() < 6);

lowByte = Wire.read();
highByte = Wire.read();
x = ((highByte<<8)|lowByte); smiley = number “8” , i can not change

lowByte = Wire.read();
highByte = Wire.read();
y = ((highByte<<8)|lowByte); smiley = number “8” , i can not change

lowByte = Wire.read();
highByte = Wire.read();
z = ((highByte<<8)|lowByte); smiley = number “8” , i can not change

}

void print_output()
{
Serial.print(“x:”);
Serial.print(x/57.14286,0);

Serial.print(" y:");
Serial.print(y/57.14286,0);

Serial.print(" z:");
Serial.print(z/57.14286,0);
}

code for spi

#include <SPI.h>

#define CS 53
#define MOSI 51
#define MISO 50
#define SCK 52

double x, y, z;
byte highByte, lowByte;

void setup()
{
Serial.begin(9600);

SPI.begin();
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV2);

pinMode(CS, OUTPUT);

digitalWrite(CS, LOW);
SPI.transfer(0x20 & 0x7F);
SPI.transfer(0x0F);
digitalWrite(CS, HIGH);

digitalWrite(CS, LOW);
SPI.transfer(0x23 & 0x7F);
SPI.transfer(0x90);
digitalWrite(CS, HIGH);

}

void loop()
{
delay(250);
gyro_value();
print_xyz();
}

void gyro_value()
{

digitalWrite(CS, LOW);
SPI.transfer(0x28 | 0x80);
lowByte = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
digitalWrite(CS, LOW);
SPI.transfer(0x29 | 0x80);
highByte = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
x = ((highByte<<8)|lowByte); smiley = number “8” , i can not change

digitalWrite(CS, LOW);
SPI.transfer(0x2A | 0x80);
lowByte = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
digitalWrite(CS, LOW);
SPI.transfer(0x2B | 0x80);
highByte = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
y = ((highByte<<8)|lowByte); smiley = number “8” , i can not change

digitalWrite(CS, LOW);
SPI.transfer(0x2C | 0x80);
lowByte = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
digitalWrite(CS, LOW);
SPI.transfer(0x2D | 0x80);
highByte = SPI.transfer(0x00);
digitalWrite(CS, HIGH);
z = ((highByte<<8)|lowByte); smiley = number “8” , i can not change

}

void print_output()
{
Serial.print(“x:”);
Serial.print(x/57.14286,0);

Serial.print(" y:");
Serial.print(y/57.14286,0);

Serial.print(" z:");
Serial.print(z/57.14286,0);
}

I’m using them in Arduino mega and the output(x,y,z) i’m getting is definetely right.
When i’m running these two codes in Arduino due i’m getting wrong reults that don’t fit with the previous.
Do you have any idea why is this happening?
I have tried using other libraries but they also didn’t work out on Arduino due.
Any help would be really appreciated!

Please edit your post to change the quote tags to code tags. Otherwise the forum software eats a little bit of your code.

See the smileys? That's your code being misinterpreted.

You might also take the opportunity to edit the typo in the title.

Add an “(int16_t)” typecast, like this:

x = (int16_t)((highByte<<8)|lowByte);

Spot on Paul!
I’m getting the desired results now,many thanks! :slight_smile:

I have another question now:
As you can see my variables (x,y,z) are registered as “double”,what else can i put instead of (int16_t) in order to state that?

The typecast must be int16_t, because the sensor's data is fundamentally a 16 bit signed integer.

This code happens to work by mistake on AVR chips, because the default type for integer expressions is 16 bits signed. Consider that both highByte and lowByte are unsigned 8 bit numbers. The expression should always produce a positive result, always a number from 0 to 65535, because a number of 0 to 255 is left shifted and then combined with another unsigned number. As written, this expression can't produce any negative numbers. It only works on AVR because of the limited size of "int", so results which should have been from 32768 to 65535 are turned negative due to the limited data size. On ARM chips, int is 32 bits, so you get the "correct" result.

The typecast tells the compiler to treat the result as the intended 16 bit signed data the sensor produced.

Thanks paul. :slight_smile: