I2C problem

Hello !
I have this MPU6050 to connect with my arduino Uno R3
http://www.ebay.fr/itm/MPU-6050-3-Axis-Gyro-Gyroscope-And-Accelerometer-Sensor-Module-Arduino-4028-/200948650867?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item2ec9791373
So I connect it like this :
VCC to 3,3V
Gnd to Gnd
SCL to A5
SDA to A4
I have try this code but it makes "MPU6050 connection failed" and show me a lot of zero...

// 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();

    // initialize serial communication
    // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
    // it's really up to you depending on your project)
    Serial.begin(38400);

    // 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);
}

So, I have try a I2C scanner to found the adresse of my device.
But the problem is that all of scanners I have try bug when they "Scanning..." The program freeze...

/**
 * 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);
    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 = 100;

// 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);
}

I have try to pullup SCL and SDA with 4,7k to 5V but nothing change...
I think the problem comes of my program or my libraries ??
Please, help me ! And sorry for my bad english :confused:

Don't use the pullup resistors to +5V. If the SDA and SCL are pulled up to 5V it might damage the sensor. Your sensor might be broken by now.

That sensor board has an onboard voltage regulator.
You better use the +5V pin of the Arduino to power it.
When you use the 3.3V, the sensor onboard voltage regulator makes 3.2V, that is too low for the Arduino.

The sensor board has pullup resistors. You don't have to add extra pullup resistors.

Sensor Vcc to Arduino 5V.

When you use the i2c_scanner, it should be detected.
http://playground.arduino.cc/Main/I2cScanner

Thks for the reply,
I have delete pullup resistors, and connect 5V to VCC.
But when I use the i2c_scanner, the serial monitor show me :
"I2C Scanner
Scanning..."
Nothing else...

Maybe a problem with the wire library ? :confused:

1 Like

No, the i2c_scanner should work.
Could you check your wiring once more ?
If you use a breadboard, they have sometimes bad contacts.
Perhaps you have to buy a new sensor board.

1 Like

My wiring is good, I really don't understand, I don't use breadboard.
I've already have a problem with I2C when I've try to connect wii motion plus to my arduino UNO in the past. It didn't work.
So, it's for the reason I have bought this sensor. It's possible to have problem with my arduino card for the I2C pins ?
Can I use a gyroscope without I2C ? Analogique ?
Thk's for the help !

Can you post a clear photo of your setup please?

Do you happen to have a second Arduino lying around? If so you could check if I2C is working OK.



I don't have another arduino so I can't test if the I2C working OK...

Can I use a gyroscope without I2C ?

Maybe with this ?
http://todbot.com/blog/2010/09/25/softi2cmaster-add-i2c-to-any-arduino-pins/

I read things about another mode call "SPI" Can someone help me please ? :confused:

The wiring is okay. You do have a nasty problem for sure.
But I think it is better to solve this problem, than to try other (non-standard) things that could cause troubles on their own.

Do you have a multimeter ?
Can you check pins of the sensor board ? The SDA and SCL should be near 3.3V.

I assume you have the newest Arduino software ( 1.0.5 ) and didn't change the libraries or installed conflicting libraries.

If you have trouble with this Uno board, perhaps you could test the A4 and A5 pin.
Could you write a sketch that toggles A4 and A5 and measure the output (or use a resistor with a led at the output, preferrable to the 5V since the I2C is active low).

In a situation like this, it is best to try the complete setup on different hardware. You would need another Arduino board and another I2C device and other wires and so on.

Accelerator sensors are available with analog outputs. I know there is an (expensive) gyro with analog output. But I2C should not be this hard. Perhaps something is broken.

SPI is another interface. Some sensors can do both I2C and SPI. But a 3.3V device can not be connected to a 5V Arduino board. The I2C bus is more flexible in use.

Could you buy another Arduino board, and perhaps also another sensor ? I think that is the best thing to do at this moment.

I have check the SDA and SCL pins from my sensor board and they both are at 1,2V. You think that's the problem ?

I have the newest Arduino software ( 1.0.5 ).

Can you help me to write the sketch for toggles A4 and A5, I will measure the output too.

Take the "blink" example and change the pin number if you want to toggle a pin.

I've toggles A4 and A5 and measure the output, they both at 5V...

byte start_address = 1;

byte end_address = 100;

...

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

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

The I2C-address depends on the AD0 pin of the sensor. If it is connected to ground, the address is 0x68. If it is connected to VLOGIC (+3.3V) it is 0x69.

0x68 is 104 and 0x69 is 105 (decimal). Thus the scanner isn't scanning far enough.

  for(address = 1; address < 127; address++ )

The i2c_scanner code scanning 1 to 127 no ? But it doesn't found the device :confused:

Not in the code you posted in your original post it didn't.

I have an error when I compile now :confused:

C:\Program Files (x86)\Arduino\libraries\Wire/utility/twi.h: In function 'void scanI2CBus(byte, byte, void (*)(byte, byte))':
C:\Program Files (x86)\Arduino\libraries\Wire/utility/twi.h:44: error: too few arguments to function 'uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t)'
sketch_aug14a:24: error: at this point in file

Sadly we have the error message but not the code.

Read this before posting a programming question