Jump to content
I2Cdevlib Forums
Sign in to follow this  
Kim D

newbie No experience at all !!! GY-521 sensor

Recommended Posts

I try to restore a friends machine, that drives over un-level ground and need to hold a vertical device always perfectly vertical using 1x 12VDC motor.
Stabilization is done on 1 axis. Not 2 I believe.
His former old friend build a Arduino Uno board with on top a DUALCHANNEL H-BRIDGE motor shield from ELECROW.COM and
on top of that board a prototype shield v.5 board containing a GY-521 sensor ( MPU 6050 I believe).

We managed to get his old code back via relatives of his former friend who look like this :

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

MPU6050 mpu;

// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorFloat gravity;    // [x, y, z]            gravity vector
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

// PID
double kp = 20, ki = 0, kd = 0;
double Setpoint = 0; // leveled
double Input = 0, Output = 0;
PID PID1(&Input, &Output, &Setpoint, kp, ki, kd, DIRECT);

// ================================================================
// ===               INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
  mpuInterrupt = true;

void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
  Fastwire::setup(400, true);

  // initialize serial communication

  // initialize DMP
  devStatus = mpu.dmpInitialize();

  // Offsets for calibrating
  mpu.setZAccelOffset(1788); // 1688 factory default

  // make sure it worked (returns 0 if so)
  if (devStatus == 0) {
    // turn on the DMP, now that it's ready
    Serial.println(F("Enabling DMP..."));

    // enable Arduino interrupt detection
    Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
    attachInterrupt(0, dmpDataReady, RISING);
    mpuIntStatus = mpu.getIntStatus();

    // set our DMP Ready flag so the main loop() function knows it's okay to use it
    Serial.println(F("DMP ready! Waiting for first interrupt..."));
    dmpReady = true;

    // get expected DMP packet size for later comparison
    packetSize = mpu.dmpGetFIFOPacketSize();
  } else {
    // ERROR!
    // 1 = initial memory load failed
    // 2 = DMP configuration updates failed
    // (if it's going to break, usually the code will be 1)
    Serial.print(F("DMP Initialization failed (code "));

  // Motor_1 controll pin initiate;
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT); // Speed control

  //Enable the Motor Shield output;
  pinMode(6, OUTPUT);
  digitalWrite(6, HIGH);

  //PID initiate
  PID1.SetOutputLimits(-255, 255);

void loop() {
  //If programming failed, don't try to do anything
  if (!dmpReady) return;

  // wait for MPU interrupt or extra packet(s) available
  while (!mpuInterrupt && fifoCount < packetSize) {
    // other program behavior stuff here
    // .
    // .
    // .
    // if you are really paranoid you can frequently test in between other
    // stuff to see if mpuInterrupt is true, and if so, "break;" from the
    // while() loop to immediately process the MPU data
    // .
    // .
    // .
  // reset interrupt flag and get INT_STATUS byte
  mpuInterrupt = false;
  mpuIntStatus = mpu.getIntStatus();

  // get current FIFO count
  fifoCount = mpu.getFIFOCount();

  // check for overflow (this should never happen unless our code is too inefficient)
  if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
    // reset so we can continue cleanly
    Serial.println(F("FIFO overflow!"));

    // otherwise, check for DMP data ready interrupt (this should happen frequently)
  } else if (mpuIntStatus & 0x02) {
    // wait for correct available data length, should be a VERY short wait
    while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

    // read a packet from FIFO
    mpu.getFIFOBytes(fifoBuffer, packetSize);

    // track FIFO count here in case there is > 1 packet available
    // (this lets us immediately read more without waiting for an interrupt)
    fifoCount -= packetSize;


    mpu.dmpGetQuaternion(&q, fifoBuffer);
    mpu.dmpGetGravity(&gravity, &q);
    mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

    Input = ypr[2] * 180 / M_PI;

    //Output to motor shield

    //Display Euler angles in degrees
    Serial.print(ypr[0] * 180 / M_PI);
    Serial.print(ypr[1] * 180 / M_PI);
    Serial.print(ypr[2] * 180 / M_PI);

void control_motor(double pwm_value) {
  int pwm = round(pwm_value);
  if (pwm < -255) pwm = -255;
  if (pwm > 255) pwm = 255;

  if (pwm > 0) {
    //Set the motor direction CW
    digitalWrite(4, LOW);
    digitalWrite(5, HIGH);
    analogWrite(9, pwm);                                               //Set the motor speed
  else {
    //Set the motor direction CCW
    digitalWrite(4, HIGH);
    digitalWrite(5, LOW);
    analogWrite(9, pwm);

But once I connect my arduino uno board and launch the program I get this error message:

Arduino: 1.8.13 (Windows 7), Board:"Arduino Uno"

V2:1:10: fatal error: I2Cdev.h: No such file or directory

 #include "I2Cdev.h"


compilation terminated.

exit status 1

I2Cdev.h: No such file or directory

ZIP file does not contain a map or valid library


What do I need to do?

Thanks for any help. Very welcome when you don't have any arduino experiences !!!

Kind regards,


20200730_110514_resized (Medium).jpg

20200730_110724_resized (Medium).jpg

20200730_110519_resized (Medium).jpg

20200730_110705_resized (Medium).jpg

20200730_110531_resized (Medium).jpg

20200730_110539_resized (Medium).jpg

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Create New...