MPU 6050 on a mechanical wave driver.

Hi everyone,

I have attached an MPU 6050 accelerometer to an oscillating wave driver, which is in turn powered by a sine wave generator. I am trying to find the amplitude of the acceleration of the MPU 6050. I am only using a frequency of 1 Hz, so its not too fast. The Z axis is in the direction of displacement. However the serial plotter plots a waveform that jitters between large negative and positive values, while only the amplitude of the wave oscillates at about 1 Hz. I've attached a picture of it.

Why is it so jittery, and can I fix it? I have calibrated the sensor and set the offsets and everything. Here is my code, pieced together from some examples: (Ignore the filtering stuff, I was just messing with that)

#include <Filters.h>

// 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:
//      2013-05-08 - added multiple output formats
//                 - added seamless Fastwire support
//      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.
===============================================
*/

float filterFreq = 10.0;

float windowLength = 20.0/filterFreq;     // how long to average the signal, for statistist

// 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"

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

// 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;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high

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

float accel;

#define OUTPUT_READABLE_ACCELGYRO


#define LED_PIN 13
bool blinkState = false;

void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

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

    // 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");
    Serial.println("Updating internal sensor offsets...");
    // supply your own gyro offsets here, scaled for min sensitivity
    accelgyro.setXGyroOffset(-20);
    accelgyro.setYGyroOffset(-67);
    accelgyro.setZGyroOffset(2);
    accelgyro.setXAccelOffset(-1289);
    accelgyro.setYAccelOffset(2598);
    accelgyro.setZAccelOffset(1347);
    
    // configure Arduino LED pin for output
    pinMode(LED_PIN, OUTPUT);
    
    FilterOnePole lowpassFilter(LOWPASS, filterFreq);
}

void loop() {
    // filters are test with a sine wave input, keep track of those values here for a sanity check
    RunningStatistics inputStats;                 // create statistics to look at the raw test signal
    inputStats.setWindowSecs( windowLength );
    
    FilterOnePole filterOneLowpass( LOWPASS, filterFreq );   // create a one pole (RC) lowpass filter
    RunningStatistics filterOneLowpassStats;                    // create running statistics to smooth these values
    filterOneLowpassStats.setWindowSecs( windowLength );
    
    while(true) {
      // read raw accel/gyro measurements from device
      accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
      accel = 9806.650*((az-16384.000)/16384.000); 
      // 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
      //filterOneLowpass.input(accel);
      //filterOneLowpassStats.input( filterOneLowpass.output() );
      
      Serial.println(accel);
  
      // blink LED to indicate activity
      blinkState = !blinkState;
      digitalWrite(LED_PIN, blinkState);
    }
}

Thanks guys!

Without any indication of units in either time or amplitude axes, there's no reason not to
believe its working fine. There's an oscillation.

Can you explain the meaning of the numbers on the graph?