Issues with I2C, MPU6050, and Teensy 3.5

Status
Not open for further replies.

Lord Turtle

New member
Hi everyone
This is my first time using a Teensy board and I'm having trouble with getting valid information from my MPU6050 (on the breakout board GY521).

I run this exact code on a Arduino Nano and on the Serial Monitor I get what I expect, an average of 0s on each axis.
However when I run it on the T3.5 I get really weird numbers . Even after accouting for the offset, the average in each axis is 60~70. When I rotate the MPU6050 anti clockwise around the pitch axis this number reduces (as expected) BUT when I rotate clockwise it becomes a really large negative number as can be seen in the attached screenshot. This occurs on each axis and only in the clockwise direction.

So I've got two issues here:
- The incorrect average value
- The weird flipping of the gyroscope values

Any insight would be greatly appreciated
I've also included a photo of my circuit just to double check everything is right. I believe the GY521 has pullup resistors builtin.

Code:
##include <Wire.h>

#define SAMPLE 1000
#define MAX_I2C_SPEED 400000
#define LSB_GYRO 131
float Gyr_raw[3];
float Gyr_offset[3];

void setup() {

  Wire.begin(); //begin the wire comunication
 // Wire.setSDA(18);
 // Wire.setSCL(19);
  Wire.setClock(MAX_I2C_SPEED);
  // Tell MPU6050 to wake up
  Wire.beginTransmission(0x68);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  // Set gyro full scale range (1000 deg/s)
  Wire.beginTransmission(0x68);
  Wire.write(0x1B);
  Wire.write(0b00001000);
  Wire.endTransmission(true);

  Serial.begin(250000);
  
  // Obtain an error value used to calculate the offset.
  for (int i = 0; i < SAMPLE; i++) { 
    Wire.beginTransmission(0x68); 
    Wire.write(0x43); //First gyro register address
    Wire.endTransmission(false);
    Wire.requestFrom(0x68, 6, true); //Request 6 registers worth of data
    
    Gyr_raw[0] = Wire.read() << 8 | Wire.read(); 
    Gyr_raw[1] = Wire.read() << 8 | Wire.read();
    Gyr_raw[2] = Wire.read() << 8 | Wire.read();

    Gyr_raw[0] /= LSB_GYRO;
    Gyr_raw[1] /= LSB_GYRO;
    Gyr_raw[2] /= LSB_GYRO;
    
    Gyr_offset[0] += Gyr_raw[0];
    Gyr_offset[1] += Gyr_raw[1];
    Gyr_offset[2] += Gyr_raw[2];

    for (int j = 0; j < 3; j++) {
      Serial.print(Gyr_raw[j]);
      Serial.print(" ");
    }
    Serial.println();
    delay(1);
  }
  Serial.print("Average values: ");
  for (int i = 0; i < 3; i++) {
    Gyr_offset[i] /= SAMPLE;  
    Serial.print(Gyr_offset[i]);
    Serial.print(" ");
  }
  delay(5000);
}

void loop() {
  // put your main code here, to run repeatedly:
  get_IMU();
  for (int j = 0; j < 3; j++) {
    Serial.print(Gyr_raw[j]);
    Serial.print(" ");
  }
  Serial.print(" ");
  Serial.println();
}

void get_IMU(void) {
  
  Wire.beginTransmission(0x68); 
  Wire.write(0x43); //Gyro data first adress
  Wire.endTransmission(false);
  Wire.requestFrom(0x68, 6, true); //Request 6 registers worth of data
  // And store it into the I2C buffer

  Gyr_raw[0] = Wire.read() << 8 | Wire.read(); //Once again we shift and sum
  Gyr_raw[1] = Wire.read() << 8 | Wire.read();
  Gyr_raw[2] = Wire.read() << 8 | Wire.read();
  
  Gyr_raw[0] /= LSB_GYRO;
  Gyr_raw[1] /= LSB_GYRO;
  Gyr_raw[2] /= LSB_GYRO;

  for (int i = 0; i < 3; i++) {
    Gyr_raw[i] -= Gyr_offset[i];
  }
  
}
 

Attachments

  • Screenshot (42).jpg
    Screenshot (42).jpg
    72.1 KB · Views: 75
  • Teensy MPU6050.jpg
    Teensy MPU6050.jpg
    142.1 KB · Views: 64
Status
Not open for further replies.
Back
Top