chipKIT® Development Platform

Inspired by Arduino™

PROBLEMS USING I2C PMODGYRO

Created Wed, 25 Apr 2012 19:44:01 +0000 by gerry27


gerry27

Wed, 25 Apr 2012 19:44:01 +0000

:roll: Hello I am a student and am working with the board chipkituno32. I have a pmodgyro and I have a problem when communicating because when you animate it. I read the gyro false readings.

here is my code please help me

#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.send(address); // send register address Wire.send(val); // send value to write Wire.endTransmission(); // end transmission }

int readRegister(int deviceAddress, byte address){

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

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

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

v = Wire.receive();
return v;

}


les1943

Wed, 25 Apr 2012 20:47:38 +0000

You will need to show / describe how you are connecting the uno ---- giro.

also giro JP1 = ?


Jason

Wed, 18 Jul 2012 19:43:51 +0000

Any update on this? I'm having the same exact problem with a Max32. It just prints out numbers around 65000. The same code works fine on the Mega.


Jason

Wed, 18 Jul 2012 23:12:07 +0000

I think I answered my own question. It looks like the gyro rate is normal when rotating in one direction. The other direction it gives values that are 2^16 - GYRO_Rate. Not sure why though.


bjosephs

Sun, 11 Aug 2013 20:45:23 +0000

Hey all,

I think this is my first post... and I'm not sure anyone is still looking at this thread, but I was having the same problem as the OP and I think I solved it. I had this exact code running fine on an Arduino Mega 2560 but was getting the same asymmetrical data (the 2^16 - actual value) problem when rotating the negative direction for any axis. I believe the root cause is that the PIC32 is 32 bit and the sample code here assumes 16 bit... I think that screws up the two's complement which defines the negative values as negative.

I changed the "int" variables (x,y,z) to int16_t types to force 16 bit interpretation. I get symmetrical data now complete with negative sign.

Regards, Brian