Simplest test code for MPU6050

I'm trying to ride Occam's razor in diagnostics today. Not asking for homework help.

Without a long winded post of what I've done and what I haven't .. Playing with the MPU6050 gyroscope and the UNO board, displaying the results on a 2x20 VFD display scavenged from a cash register.

The display output is working. I've been using exclusively other peoples code to talk to the sensor for a proof of concept before I play about with writing my own and continuing my code journey.

Three different authors and the same results .. there is no change in the data displayed. As if the sensor mute.

Having awesome confidence in my hardware and 4 connections, first thought first try was I had crap code .. but three different sketches makes that doubtful.

Then went down the bunny hole seeing if the Wire.h protocol is included in the IDE .. seems, according to the Internet, to be. So I assume from the sketch side that the UNO and sensor are able to talk, if not actually doing so? Maybe?

So what I'm lookin for is the most basic and minimum of code lines as a diagnostic tool that will definitively prove that the sensor (if wired correctly) is in fact alive, kicking, and speaking I2C, or dead as a dog doo and get another sensor.

I've seen bad silicon out of the box in my days, so it's possible - but again, Occam .. and Taylor Swift .. It's likely me .. me .. I'm the problem, it's me.

Thanks in advance for any help.

Jim.

, and

which MPU6050 library are you using?

try this

// Basic demo for accelerometer readings from Adafruit MPU6050

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {
  Serial.begin(115200);
  while (!Serial)
    delay(10); // will pause Zero, Leonardo, etc until serial console opens

  Serial.println("Adafruit MPU6050 test!");

  // Try to initialize!
  if (!mpu.begin()) {
    Serial.println("Failed to find MPU6050 chip");
    while (1) {
      delay(10);
    }
  }
  Serial.println("MPU6050 Found!");

  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }

  Serial.println("");
  delay(100);
}

void loop() {

  /* Get new sensor events with the readings */
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  /* Print out the values */
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");

  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");

  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");

  Serial.println("");
  delay(500);
}

some results

Adafruit MPU6050 test!
MPU6050 Found!
Accelerometer range set to: +-8G
Gyro range set to: +- 500 deg/s
Filter bandwidth set to: 21 Hz

Acceleration X: -0.61, Y: 0.20, Z: 8.28 m/s^2
Rotation X: 0.15, Y: 0.03, Z: 0.00 rad/s
Temperature: 22.78 degC

Acceleration X: -0.62, Y: 0.18, Z: 8.30 m/s^2
Rotation X: 0.15, Y: 0.03, Z: 0.00 rad/s
Temperature: 22.85 degC

Acceleration X: 2.87, Y: -5.93, Z: 5.10 m/s^2
Rotation X: 0.11, Y: 1.06, Z: 0.57 rad/s
Temperature: 22.90 degC

Acceleration X: -2.90, Y: 2.44, Z: 8.86 m/s^2
Rotation X: -0.01, Y: 0.22, Z: 0.16 rad/s
Temperature: 22.93 degC

Acceleration X: -1.31, Y: 0.59, Z: 8.24 m/s^2
Rotation X: 0.13, Y: -0.12, Z: -0.05 rad/s
Temperature: 22.94 degC

Hope it's ok for an Arduino beginner to offer a suggestion. Use File->Examples->Wire->i2c_scanner. All it does is check every 5 seconds and lists any I2C gadgets it finds as - "I2C device found at address 0x68 !" or whatever other address.

Absolute awesome sauce! Exactly what I was looking for I think. God hat for the day and a pint on me, mate.

After bopping the serial baud rate down to 9600, Returned 'no I2C device found'.

Steers me in the direction of either my hookup leads .. about a foot of loose bell wire on all four connections .. are heavy on inductance and forking the signal, or the sensor itself has pooped the proverbial bed.

The former easy enough to test, and if discounted .. it's by default the latter.

Edit: It appears to be the latter.

Thanks much for unforking me. :slight_smile:

Jim.

Keep in your toolbox a handful of the most raw stripped down code that tests the hookup of only one type of thingumybob.

1 Like

Have you put pull-up resistor on I2C lines?

Ciao, Ale.

My first reaction to your reply was "Am I just out of gas?" I'm actually unfamiliar with the protocol intimately, but yes and no. Didn't know they were required, but I checked the sensor board and there are 2.2K pullups .. Still, this is something to keep in mind for future unforkery.

Jim.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.