i2c and MPU650 isue

good afternoon

I am trying to create an i2c bus that can get the gyro and accelerometer data that I intend to use to create my own spin on the balancing robot , the main objective of the build is not to build a robot but to lean about i2c, coding and the formulas and theory that make this project possible.

I have been scratching my head over this for about 2 weeks now with no luck at all.
have decided to brake the code up and then re read the registers after I have set them however i haven't even made it through the void set up.

when open the serial monitor and check the 3 registers that are printed I get values of 225 which in binary are 0b11111111 and i have set them to 0b00000000

also i have written all the code to read the gyro and I only receive 65535 which is 16 1s.

if anyone could shed a little light on this and explaine where i have gone wrong that would be great

Adam

Balanceing_robot.ino (1.76 KB)

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

Do you mean MPU6050?

Did you google arduino MPU6050

There is a library to do the job you are trying to do.

https://playground.arduino.cc/Main/MPU-6050

Check that site out.

Tom.... :slight_smile:

Which Arduino do you use? The MPU works on 3.3V, so that you need an I2C level shifter when connecting to a 5V Arduino.

Hi,
OPs code

#include <Wire.h>

const int MPU = 0x68;                      // addresses for I2C devices 

float GX, GY, GZ;                          // float values for Gyro

void setup() {

  Wire.begin ();                           //Start I2C Communication 
  Serial.begin(9600);                      //Start Serial comunication

  Wire.beginTransmission (MPU);            // address MPU
  Wire.write(0x6B);                        // address regiser 0x6b
  Wire.write(0x00);                        // set all bits to 0
  Wire.endTransmission(true);

  Wire.beginTransmission(MPU);             // address MPU
  Wire.write(0x1b);                        //address register 0x1b
  Wire.write(0x00);                        // set all bits to 0
  Wire.endTransmission();

  Wire.beginTransmission(MPU);             // address MPU
  Wire.write(0x1c);                        // address register 0x1c
  Wire.write(0x00);                        // set all bits to 0
  Wire.endTransmission();

  Wire.beginTransmission(MPU);              // check 8 bit values writen to the registers  
  Wire.requestFrom (0x6b,1);
  uint8_t A = Wire.read();
  Wire.endTransmission();

  Wire.beginTransmission(MPU);
  Wire.requestFrom (0x1B, 2);
  uint8_t B = Wire.read();
  uint8_t C = Wire.read();
  Wire.endTransmission();

   Serial.print("A =");                    // print all 8 bit values to serial mounitor to be checked.           
   Serial.println(A);
   Serial.print(" B =");
   Serial.println(B); 
   Serial.print (" C =");
   Serial.print(C);  
  
  

  

}

void loop() {
  // put your main code here, to run repeatedly:

  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission();
  Wire.requestFrom(0x3B,2);
  uint16_t D = Wire.read()<<8|Wire.read();

  Serial.print (" D = ");
  Serial.println(D);
  

}

I'm no expert on om Wire or I2C, but I think this line in void loop()

Wire.requestFrom(0x3B,2);

Should be;

Wire.requestFrom(MPU,2);

You are requesting data from the MPU address.

tronixstuff.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/

It looks like a good tutorial

Tom.. :slight_smile:

Good morning all

thank you for the reply’s I will have a look at the tutorial tonight

I know there is a library for the MPU6050 however the whole aim of this exercise is to learn how to extract data from i2c devices and not just to use a reconstructed library this should enable me to only draw the data that I require for my project and keep the program size to a minimum.

The mpu6050 i am using is a generic one from the wounderfull eBay so I am not sold on it being perfect either I may order a new one from ardrifruit for peace of mind.

Also I was not aware that the device was 3.3v I have operated it at 5v so there is always a chance it is now damaged.

I will make some amendment and post the results once complet

Many thanks
Adam

Most boards include a 3.3V voltage regulator, so that feeding 5V at Vcc is okay. But the I2C signal levels may not be sufficient.