Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Issues with I2C, MPU6050, and Teensy 3.5

  1. #1
    Junior Member
    Join Date
    Feb 2020
    Posts
    1

    Issues with I2C, MPU6050, and Teensy 3.5

    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];
      }
      
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Screenshot (42).jpg 
Views:	6 
Size:	72.1 KB 
ID:	18932   Click image for larger version. 

Name:	Teensy MPU6050.jpg 
Views:	14 
Size:	142.1 KB 
ID:	18933  


  2. #2
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,581
    Looked over the wiring and it does look correct. Did notice you have vcc going to 3.3v. The GY-521 states vcc: 3v-5v. You can try adding pullups to SDA/SCL as a test case.

    As a double check you could try running the simple code here: https:https://dummyscodes.blogspot.com/201...uino-mega.html - its more complete.

  3. #3
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,315
    are the header pins soldered to your T3.5?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •