Pages: 1 [2] 3   Go Down
Author Topic: MPU6050 SparkFun Breakout to Arduino Uno - No Communication  (Read 24424 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I've made some progress.
I Found the code for a I2C Scanner, the problem is that the library twi.h was updated so I've added some code there to make it work. Here It is

Quote
/**
 * I2CScanner.pde -- I2C bus scanner for Arduino
 *
 * 2009, Tod E. Kurt, http://todbot.com/blog/
 *
 */

#include "Wire.h"
extern "C" {
#include "utility/twi.h"  // from Wire library, so we can do bus scanning
}

// Scan the I2C bus between addresses from_addr and to_addr.
// On each address, call the callback function with the address and result.
// If result==0, address was found, otherwise, address wasn't found
// (can use result to potentially get other status on the I2C bus, see twi.c)
// Assumes Wire.begin() has already been called
void scanI2CBus(byte from_addr, byte to_addr,
                void(*callback)(byte address, byte result) )
{
  byte rc;
  byte data = 0; // not used, just an address to feed to twi_writeTo()
  for( byte addr = from_addr; addr <= to_addr; addr++ ) {
    rc = twi_writeTo(addr, &data, 0, 1, 0);
    callback( addr, rc );
  }
}

// Called when address is found in scanI2CBus()
// Feel free to change this as needed
// (like adding I2C comm code to figure out what kind of I2C device is there)
void scanFunc( byte addr, byte result ) {
  Serial.print("addr: ");
  Serial.print(addr,DEC);
  Serial.print( (result==0) ? " found!":"       ");
  Serial.print( (addr%4) ? "\t":"\n");
}


byte start_address = 1;
byte end_address = 200;

// standard Arduino setup()
void setup()
{
    Wire.begin();

    Serial.begin(19200);
    Serial.println("\nI2CScanner ready!");

    Serial.print("starting scanning of I2C bus from ");
    Serial.print(start_address,DEC);
    Serial.print(" to ");
    Serial.print(end_address,DEC);
    Serial.println("...");

    // start the scan, will call "scanFunc()" on result from each address
    scanI2CBus( start_address, end_address, scanFunc );

    Serial.println("\ndone");
}

// standard Arduino loop()
void loop()
{
    // Nothing to do here, so we'll just blink the built-in LED
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300);
}

ok, this scan tell me that my device it's actually in the por 105, meaning, 0x69 not 0x68. Now I need to make everithing else work, but it's a start.

Ah, another thing, My board can be suplied with 5v or 3.3v, it's the same thing, but you need the pullup resistors to 3.3v to make it work, otherwise the scan doesn't detect the device. Douh!!

now I need a nice piece of code thar takes all the raw data and sends it back to me smiley-wink over serial.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, noy I want to use this code

Quote
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//     2011-10-07 - initial release

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#include "Wire.h"

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"
#include "MPU6050.h"

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

    Serial.begin(9600);

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
}

void loop() {
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

    // these methods (and a few others) are also available
    //accelgyro.getAcceleration(&ax, &ay, &az);
    //accelgyro.getRotation(&gx, &gy, &gz);

    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);

    // blink LED to indicate activity
    blinkState = !blinkState;
    digitalWrite(LED_PIN, blinkState);
}

from https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino

but I don't know how to change the address to 0x69. any ideas?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ran the port scanner code. None of my ports were found. Does that mean that my chip is fried?

I2CScanner ready!
starting scanning of I2C bus from 1 to 200...
addr: 1          addr: 2          addr: 3          addr: 4       
addr: 5          addr: 6          addr: 7          addr: 8       
addr: 9          addr: 10          addr: 11          addr: 12       
addr: 13          addr: 14          addr: 15          addr: 16       
addr: 17          addr: 18          addr: 19          addr: 20       
addr: 21          addr: 22          addr: 23          addr: 24       
addr: 25          addr: 26          addr: 27          addr: 28       
addr: 29          addr: 30          addr: 31          addr: 32       
addr: 33          addr: 34          addr: 35          addr: 36       
addr: 37          addr: 38          addr: 39          addr: 40       
addr: 41          addr: 42          addr: 43          addr: 44       
addr: 45          addr: 46          addr: 47          addr: 48       
addr: 49          addr: 50          addr: 51          addr: 52       
addr: 53          addr: 54          addr: 55          addr: 56       
addr: 57          addr: 58          addr: 59          addr: 60       
addr: 61          addr: 62          addr: 63          addr: 64       
addr: 65          addr: 66          addr: 67          addr: 68       
addr: 69          addr: 70          addr: 71          addr: 72       
addr: 73          addr: 74          addr: 75          addr: 76       
addr: 77          addr: 78          addr: 79          addr: 80       
addr: 81          addr: 82          addr: 83          addr: 84       
addr: 85          addr: 86          addr: 87          addr: 88       
addr: 89          addr: 90          addr: 91          addr: 92       
addr: 93          addr: 94          addr: 95          addr: 96       
addr: 97          addr: 98          addr: 99          addr: 100       
addr: 101          addr: 102          addr: 103          addr: 104       
addr: 105          addr: 106          addr: 107          addr: 108       
addr: 109          addr: 110          addr: 111          addr: 112       
addr: 113          addr: 114          addr: 115          addr: 116       
addr: 117          addr: 118          addr: 119          addr: 120       
addr: 121          addr: 122          addr: 123          addr: 124       
addr: 125          addr: 126          addr: 127          addr: 128       
addr: 129          addr: 130          addr: 131          addr: 132       
addr: 133          addr: 134          addr: 135          addr: 136       
addr: 137          addr: 138          addr: 139          addr: 140       
addr: 141          addr: 142          addr: 143          addr: 144       
addr: 145          addr: 146          addr: 147          addr: 148       
addr: 149          addr: 150          addr: 151          addr: 152       
addr: 153          addr: 154          addr: 155          addr: 156       
addr: 157          addr: 158          addr: 159          addr: 160       
addr: 161          addr: 162          addr: 163          addr: 164       
addr: 165          addr: 166          addr: 167          addr: 168       
addr: 169          addr: 170          addr: 171          addr: 172       
addr: 173          addr: 174          addr: 175          addr: 176       
addr: 177          addr: 178          addr: 179          addr: 180       
addr: 181          addr: 182          addr: 183          addr: 184       
addr: 185          addr: 186          addr: 187          addr: 188       
addr: 189          addr: 190          addr: 191          addr: 192       
addr: 193          addr: 194          addr: 195          addr: 196       
addr: 197          addr: 198          addr: 199          addr: 200       

done

Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Check your wiring once more.
AD0 must be connected to ground or 3.3V (with solder jumper). Connect it to ground for I2C address 0x68.
SCL and SDA are connected directly to the Arduino, but you could add extra pull-up resistors to the 3.3V. The resistors could be 4k7 or 10k (the board already has 10k pull-up resistors, but I prefer 4k7).
You don't need the INT, you could leave it open.
The CLKIN and FSYNC can be tied to ground, because they are not used.

If you have all this, the scanner sketch should be able to find it.


I'm confused !
Is this crossposting ?  http://arduino.cc/forum/index.php/topic,111698.0.html
If you bought a new sensor module, and started a new topic, you should have mentioned at the beginning that you made this posting before.
It's okay, as long as you let us know.
« Last Edit: June 26, 2012, 06:22:49 pm by Krodal » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is my wiring, all direct connections, no resistors. The simplest wiring possible i think.

VDD ----> 3.3v
GND ----> GND
SDA ----> A4
SCL ----> A5
VIO ----> 3.3v (same VDD supply)



chazm try this exactly this wiring, take out the resistors, and try to disconnect not mentioned pins.
I may be wrong about the sleep bit, so this is a little more complex code which solves that, it should output accelerometer x values.

This is a video of me executing this code.

http://youtu.be/mtuQPSGQCHE

Code:
#include <Wire.h>

#define MPU6050_ACCEL_XOUT_H  0x3B
#define MPU6050_ACCEL_XOUT_L  0x3C
#define MPU6050_ACCEL_YOUT_H  0x3D
#define MPU6050_ACCEL_YOUT_L  0x3E
#define MPU6050_ACCEL_ZOUT_H  0x3F
#define MPU6050_ACCEL_ZOUT_L  0x40
#define MPU6050_GYRO_XOUT_H   0x43
#define MPU6050_GYRO_XOUT_L   0x44
#define MPU6050_GYRO_YOUT_H   0x45
#define MPU6050_GYRO_YOUT_L   0x46
#define MPU6050_GYRO_ZOUT_H   0x47
#define MPU6050_GYRO_ZOUT_L   0x48
#define MPU6050_PWR_MGMT_1    0x6B

#define MPU6050_I2C_ADDRESS   0x68

uint16_t ax, ay, az, gx, gy, gz;

void setup() {
  uint8_t c = 0;
  
  Serial.begin(9600);
  Wire.begin();
  
  MPU6050_write(MPU6050_PWR_MGMT_1, &c, 1); //Stop device from reseting/sleeping

  Serial.println("START");
}

void loop() {
  
  ax = calculate_value(MPU6050_ACCEL_XOUT_H, MPU6050_ACCEL_XOUT_L, 1);
  ax = (ax-32500)/8.192;
  
  Serial.println(ax);
}

uint16_t calculate_value(int address_h, int address_l, int size) {
  
  uint8_t hi, lo;
  uint16_t value;
  
  hi = MPU6050_read(address_h, size);
  lo = MPU6050_read(address_l, size);
  
  value = ((hi)<<8);
  value |= lo;
  
  return(value);
  
}

uint8_t MPU6050_read(int address, int size) {
  
  uint8_t value;
  
  Wire.beginTransmission(MPU6050_I2C_ADDRESS);
  Wire.write(address);
  Wire.endTransmission(false);

  Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
  while(Wire.available()) {
    value = Wire.read();
  }
  
  return (value);
}

int MPU6050_write(int address, uint8_t *data, int size) {
  
  Wire.beginTransmission(MPU6050_I2C_ADDRESS);
  
  Wire.write(address);        // write the start address

  Wire.write(data, size);  // write data bytes

  Wire.endTransmission(true); // release the I2C-bus
  
  return (0);         // return : no error
}
« Last Edit: June 26, 2012, 07:09:53 pm by Obakemono » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Obakemono I simplified my wiring and tried your code. I keep getting a constant stream of 5883. Thanks for all your help thus far.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...I keep getting a constant stream of 5883...

Can you try my sketch : http://arduino.cc/playground/Main/MPU-6050
It checks for every error that could occur during the i2c communication.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Krodal for your sketch. I tried your sketch without resistors and then with resistors, at address 0x68 and 0x69, and the result was the same:

InvenSense MPU-6050
June 2012
WHO_AM_I : 3, error = 2
PWR_MGMT_2 : 3, error = 2

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: -31117, 283, 2288
temperature: 38.035 degrees Celsius
gyro x,y,z : -3838, -16640, -29695,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: 29574, 6913, -4088
temperature: 42.535 degrees Celsius
gyro x,y,z : 753, 191, 396,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: -31117, 283, 2288
temperature: 38.035 degrees Celsius
gyro x,y,z : -3838, -16640, -29695,

MPU-6050
Read accel, temp and gyro, error = 2
accel x,y,z: 29574, 6913, -4088
temperature: 42.535 degrees Celsius
gyro x,y,z : 753, 191, 396,
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You don't have I2C communication (or the sensor is broken).

I read that you already did the scanner sketch.
As I wrote in the other thread: I used this code by user RandallR for scanning. See here: http://arduino.cc/forum/index.php/topic,96952.msg738703.html#msg738703
Can you try the scanner sketch once more?
Do you have any other I2C device, to test the I2C bus with the scanner sketch ?
I use a I2C EEPROM to test the I2C bus, that chip operates at 3.3V, but also at 5V.

Is your AD0 soldered on the sensor board to either one (ground or 3.3V)? Just as in the photo of Obakemono ?


Meanwhile in the other thread, the pull-up resistors seem critical: http://arduino.cc/forum/index.php/topic,111698.msg840273.html#msg840273

« Last Edit: June 27, 2012, 04:04:07 am by Krodal » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I ran the scanner test and this was the result:


I2C Scanner
Scanning I2C bus from 0 to 127...
00 2      01 2      02 2      03 2      04 2      05 2      06 2      07 2     
08 2      09 2      0A 2      0B 2      0C 2      0D 2      0E 2      0F 2     
10 2      11 2      12 2      13 2      14 2      15 2      16 2      17 2     
18 2      19 2      1A 2      1B 2      1C 2      1D 2      1E 2      1F 2     
20 2      21 2      22 2      23 2      24 2      25 2      26 2      27 2     
28 2      29 2      2A 2      2B 2      2C 2      2D 2      2E 2      2F 2     
30 2      31 2      32 2      33 2      34 2      35 2      36 2      37 2     
38 2      39 2      3A 2      3B 2      3C 2      3D 2      3E 2      3F 2     
40 2      41 2      42 2      43 2      44 2      45 2      46 2      47 2     
48 2      49 2      4A 2      4B 2      4C 2      4D 2      4E 2      4F 2     
50 2      51 2      52 2      53 2      54 2      55 2      56 2      57 2     
58 2      59 2      5A 2      5B 2      5C 2      5D 2      5E 2      5F 2     
60 2      61 2      62 2      63 2      64 2      65 2      66 2      67 2     
68 2      69 2      6A 2      6B 2      6C 2      6D 2      6E 2      6F 2     
70 2      71 2      72 2      73 2      74 2      75 2      76 2      77 2     
78 2      79 2      7A 2      7B 2      7C 2      7D 2      7E 2      7F 2     

-------------------------------
Possible devices:

done

I don't currently have any other I2C devices, but I will have one by Friday. My AD0 is soldered just like Obakemono, I don't know if that's ground or 3.3v though. I'm currently using 4k7 ohm resistors for my pull-up resistors. So when my other I2C device comes in I should be able to find out if it's my Arduino or the mpu 6050.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've executed the I2C scanner and it finds a device in 0x68 as expected, but if i disconnect VIO from VDD voltage supply it gives the same output than the one from chazm.

Check this connection chazm it's fundamental in spark fun's board.

Yellow cable 3.3v to VDD and green cable VDD to VIO works for me.

« Last Edit: June 27, 2012, 11:32:02 am by Obakemono » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have some great news, I think that my connections were loose. My friend and I soldered the headers to the board. I ran the scanner code and I found a port.


I2C Scanner
Scanning I2C bus from 0 to 127...
00 2      01 2      02 2      03 2      04 2      05 2      06 2      07 2     
08 2      09 2      0A 2      0B 2      0C 2      0D 2      0E 2      0F 2     
10 2      11 2      12 2      13 2      14 2      15 2      16 2      17 2     
18 2      19 2      1A 2      1B 2      1C 2      1D 2      1E 2      1F 2     
20 2      21 2      22 2      23 2      24 2      25 2      26 2      27 2     
28 2      29 2      2A 2      2B 2      2C 2      2D 2      2E 2      2F 2     
30 2      31 2      32 2      33 2      34 2      35 2      36 2      37 2     
38 2      39 2      3A 2      3B 2      3C 2      3D 2      3E 2      3F 2     
40 2      41 2      42 2      43 2      44 2      45 2      46 2      47 2     
48 2      49 2      4A 2      4B 2      4C 2      4D 2      4E 2      4F 2     
50 2      51 2      52 2      53 2      54 2      55 2      56 2      57 2     
58 2      59 2      5A 2      5B 2      5C 2      5D 2      5E 2      5F 2     
60 2      61 2      62 2      63 2      64 2      65 2      66 2      67 2     
68 found! 69 2      6A 2      6B 2      6C 2      6D 2      6E 2      6F 2     
70 2      71 2      72 2      73 2      74 2      75 2      76 2      77 2     
78 2      79 2      7A 2      7B 2      7C 2      7D 2      7E 2      7F 2     

-------------------------------
Possible devices:
68 = DS1307

done

I'm going to start running some more code, hopefully my problems are done.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good!

My sketch http://arduino.cc/playground/Main/MPU-6050 should work now without any problem.
If the sketch needs more comment, or more explanation or so, please let me know.
For serious use, you could use Jeff Rowberg's library.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

¡Great! You should be fine now.

I'm on the process of filtering data now, as the use of DMP seems quite complex atm, i'm going with the good old raw data.

Krodal, you seem to be quite experienced with MPU-6050, i have a couple of doubts i hope you can help me with.

As far as i have understanded from the datasheet  ¿it auto calibrates to 0g right?, i mean, i don't need to substract any 0g value from my output ¿right?.

And at +-2g scale the sensitivity is 16384 LSB/g so, to get acceleration in g's, at 16bits, (rawAxis - 32767)/16384 ¿am i right?

Thx.
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1015
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...As far as i have understanded from the datasheet  ¿it auto calibrates to 0g right?, i mean, i don't need to substract any 0g value from my output ¿right?.

And at +-2g scale the sensitivity is 16384 LSB/g so, to get acceleration in g's, at 16bits, (rawAxis - 32767)/16384 ¿am i right?

My sketch is ment to give a quick start. On this page http://arduino.cc/playground/Main/MPU-6050 I mention to use the library by Jeff Rowberg for serious use.

The MEMS sensors are a great invention, but they have a large offset and the sensitivity is inaccurate.
Lucky for us, the sensitivity of the MPU-6050 is calibrated. The gyro sensitivity scale and the accelerometer scale are factory calibrated.
But not the offset (I can't read it in the datasheet).
So when using the raw values, the offset could be taken care of in software. Or by using the undocumented offset registers in the MPU-6050.
When using DMP firmware, the offset compensation could be in the firmware.

Accelerator with +/-2g range = 16384 LSB/g. So a value of 16384 is 1 g.
Get the accelerator value during setup(), and save it in rawOffset.
In the loop() get the value and calculate the 'g' value:
Code:
double g;
g = (double) (rawAxis - rawOffset ) / 16384.0;
And that for all 6 values (3 gyro, 3 accel).

During setup() the z-axis gets 1 'g' of the earth gravity. So you have to add 1 'g' after the offset compensation.
Logged

Pages: 1 [2] 3   Go Up
Jump to: