MPU-6050 + Servo + arduino Uno - fail when uploading

So far I have the gy-521 connected to my arduino and can see it is working great through the serial monitor.

Now I want to get the gy-521 to control a parallel motion through a servo-motor, just like this:

So I took the code from Luka's webpage:
http://lukagabric.com/arduino-mpu6050/

I've installed all libraries... as far as I know..(I can see the codes in arduino 1.05 in the library) but when I try to upload the code to my arduino board I get this message:

sketch_jun08c.ino:2:20: error: I2Cdev.h: No such file or directory
sketch_jun08c.ino:3:21: error: MPU6050.h: No such file or directory
sketch_jun08c.ino:4:11: error: #include expects "FILENAME" or
sketch_jun08c:6: error: 'MPU6050' does not name a type
sketch_jun08c:11: error: 'Servo' does not name a type
sketch_jun08c.ino: In function 'void setup()':
sketch_jun08c:22: error: 'mpu' was not declared in this scope
sketch_jun08c:24: error: 'myservo' was not declared in this scope
sketch_jun08c.ino: In function 'void loop()':
sketch_jun08c:29: error: 'mpu' was not declared in this scope
sketch_jun08c:29: error: 'amp' was not declared in this scope
sketch_jun08c:29: error: expected `;' before ')' token
sketch_jun08c:34: error: 'myservo' was not declared in this scope

I've being sitting with this for so long that I have lost the big picture of all this.. so I hope someone can see where the problem might could be. What I've read through google is that the problem is the files in my library. but I've also followed this guide to make sure no problem would come.. http://arduino.cc/de/Guide/Libraries

Sorry for my bad english! im from Denmark.

Best regards.

Anyone?

Best regards.

You have a problem with wrong folder/libraries/versions/include files.

Since Luka is using the I2Cdevlib library, you should install the library for i2cdevlib and the library for the MPU-6050 from this site: http://www.i2cdevlib.com/

Your compiler is telling you, that you have neither of those libraries.

Thanks for your reply! (:

As you can see I've installed the library from http://www.i2cdevlib.com/

and I've also installed the freeimu library, and both gives that error output when trying to uploading it when using Jeff rowbergs code. I don't get it..??

see attached picture..

The Luka code has an empty include at line 4.
Could you remove that line?
Could you also change the other includes to use '<' and '>'

#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>

And try to compile again.

If that doesn't work, I suggest to make a fresh start.
Download the newest Arduino 1.0.5.
Unzip it in a new folder and select a new project folder.
Download the I2Cdevlib and the MPU-6050 library in put it in the library folder.
Try both example sketches for the MPU-6050.

Thanks alot, now the errors got minimize a bit :slight_smile:

and ended up with:
luca_sketch_MPU6050:6: error: 'MPU6050' does not name a type
luca_sketch_MPU6050:11: error: 'Servo' does not name a type
luca_sketch_MPU6050.ino: In function 'void setup()':
luca_sketch_MPU6050:22: error: 'mpu' was not declared in this scope
luca_sketch_MPU6050:24: error: 'myservo' was not declared in this scope
luca_sketch_MPU6050.ino: In function 'void loop()':
luca_sketch_MPU6050:29: error: 'mpu' was not declared in this scope
luca_sketch_MPU6050:29: error: 'amp' was not declared in this scope
luca_sketch_MPU6050:29: error: expected `;' before ')' token
luca_sketch_MPU6050:34: error: 'myservo' was not declared in this scope

Code:
#include <Wire.h>
#include <I2Cdev.h>
#include <MPU6050.h>

MPU6050 mpu; // SO IT SEEMS THAT THIS STATEMENT IS ONE OF THE PROBLEMS RIGHT?

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

Servo myservo;

int val;
int prevVal;

void setup()
{
Wire.begin();
Serial.begin(38400);

Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
myservo.attach(9);
}

void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

val = map(ay, -17000, 17000, 0, 179);
if (val != prevVal)
{
myservo.write(val);
prevVal = val;
}

delay(50);
}

Best regards, and thanks alot again for your time, I really appreciate it. if there aint any problem you can see then Im going to reinstall it all, but I hope we can figure it out so that wont be the solution.

This is one of the two examples for the MPU-6050 library, https://github.com/jrowberg/i2cdevlib/blob/master/Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino

The "MPU6050 [i]something[/i];" declares the class for the MPU6050.
That is only possible if you have installed the MPU6050 library. So I guess you still have to install it.

Did you install the i2cdev library with a zip file ?
I only can find the sources, i2cdevlib/Arduino/I2Cdev at master · jrowberg/i2cdevlib · GitHub
You can install the MPU6050 library the same way, i2cdevlib/Arduino/MPU6050 at master · jrowberg/i2cdevlib · GitHub

If you post your sketch, please copy-paste it between [code] ... [/code] tags.

Hello Erdin and Dynamitetalks
i am a beginner in Arduino world. and am working on the same thing facing the same issues Dynamitetalks has.

Erdin your question about a zip file library, should it be zipped?
yes the errors narrowed down to the same as follow.

GYRO_Servo:6: error: 'MPU6050' does not name a type
GYRO_Servo:11: error: 'Servo' does not name a type
GYRO_Servo.ino: In function 'void setup()':
GYRO_Servo:22: error: 'mpu' was not declared in this scope
GYRO_Servo:24: error: 'myservo' was not declared in this scope
GYRO_Servo.ino: In function 'void loop()':
GYRO_Servo:29: error: 'mpu' was not declared in this scope
GYRO_Servo:29: error: 'amp' was not declared in this scope
GYRO_Servo:29: error: expected `;' before ')' token
GYRO_Servo:34: error: 'myservo' was not declared in this scope

any suggestions?

If you are a beginner, start at the begin.
Run the i2c scanner:
http://playground.arduino.cc/Main/I2cScanner

Is your sensor detected ?

There is a test sketch that doesn't need libraries:
http://playground.arduino.cc/Main/MPU-6050

Do the values change if you move and tilt and rotate the sensor ?

You have servo motors connected ?
Can you run a simple test sketch to test the servo motors ?
How are they powered ?

Next step will be the i2cdevlib library.
Did you install that library ? how ? from what website ? do you use the newest arduino ? which arduino board ?

Hi Erdin,
Thank you so so much for taking the time for me, appreciate it much.

Yes my sensor is detected 0x68.
Yes the test was a success and values change with every movement.
I did a test and the servo motor is working properly. A sweep motion

The I2Cdevlib as I named the folder in the library folder of the program and pasted the files I downloaded from the site provided here.

My arduino is the latese UNO board

Different errors now they are as follow

In file included from GYRO_Servo.ino:3:
E:\ARDUINO\libraries\MPU6050/MPU6050.h:9: error: stray '\302' in program
E:\ARDUINO\libraries\MPU6050/MPU6050.h:9: error: stray '\267' in program
E:\ARDUINO\libraries\MPU6050/MPU6050.h:9: error: stray '\302' in program
E:\ARDUINO\libraries\MPU6050/MPU6050.h:9: error: stray '\267' in program
In file included from GYRO_Servo.ino:3:
E:\ARDUINO\libraries\MPU6050/MPU6050.h:315:46: error: invalid suffix "a8ae95ca13abf004028130f61cdbc88" on integer constant
E:\ARDUINO\libraries\MPU6050/MPU6050.h:316:62: error: invalid suffix "a8ae95ca13abf004028130f61cdbc88" on integer constant
E:\ARDUINO\libraries\MPU6050/MPU6050.h:4: error: expected unqualified-id before '<' token
E:\ARDUINO\libraries\MPU6050/MPU6050.h:1480: error: expected unqualified-id before numeric constant

Once again thank you Erdin
Mishal

I have seen this problem a week earlier. I don't know if it was solved.
It might have to do with a conflict between: character coding / UTF-8 / Windows / Linux.

Either the source code has wrong characters (Jeff Rowberg should fix that) or perhaps you did a copy-paste with the html code, or opened it in an editor that chanched the character coding.

I downloaded the "Raw" if this file: i2cdevlib/MPU6050.h at master · jrowberg/i2cdevlib · GitHub
And I could not find any weird characters.

Hi Erdin,
i re did all the steps with the libraries, and now i am getting different errors.

GYRO_Servo:13: error: 'Servo' does not name a type
GYRO_Servo.ino: In function 'void setup()':
GYRO_Servo:26: error: 'myservo' was not declared in this scope
GYRO_Servo.ino: In function 'void loop()':
GYRO_Servo:31: error: 'amp' was not declared in this scope
GYRO_Servo:31: error: expected `;' before ')' token
GYRO_Servo:36: error: 'myservo' was not declared in this scope

i hope i am not bothering you or any one els with my bigenners quaries :slight_smile:

Thanks, Mishal

Something with the class "Servo" is missing.
Look at the examples (on the right side, click on a few examples):

Do you have: #include <Servo.h>

Please read this:
http://forum.arduino.cc/index.php?topic=97455.0
After reading that, you can ask as many questions as you like. We like to see the whole sketch (between code tags), and you have to tell what you are talking about and how you solved it, so others can see the solution.

How did you solve the problem with the weird characters ?

Where did the file GYRO_servo.ino come from ? I have no idea what it is.

Dear Erdin
Thank you for the heads up, and mostly thank you for pointing me to the solution, its working now.

As you suggested i had to add the [#include <Servo.h>] in my sketch, and changed the getMotion6 argument. the sketch is as follow,

[#include <Wire.h>
#include <I2Cdev.h>
#include <Servo.h>
#include <MPU6050.h>

MPU6050 mpu; // SO IT SEEMS THAT THIS STATEMENT IS ONE OF THE PROBLEMS RIGHT?

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

Servo myservo;

int val;
int prevVal;

void setup()
{
Wire.begin();
Serial.begin(38400);

Serial.println("Initialize MPU");
mpu.initialize();
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
myservo.attach(9);
}

void loop()
{
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // this was mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

val = map(ay, -17000, 17000, 0, 179);
if (val != prevVal)
{
myservo.write(val);
prevVal = val;
}

delay(50);
}]

Once again thank you Erdin.

Very cool indeed. Well done.
This is a good guide for balancing and filtering: Guide to gyro and accelerometer with Arduino including Kalman filtering - Sensors - Arduino Forum

#include <Wire.h>

#include <AFMotor.h>
#include <Servo.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
Servo radar_servo;
int servo_position=0;
AF_DCMotor motor1(1,MOTOR12_8KHZ); // declare 1st motor name (say motor1)and set its frequency
AF_DCMotor motor2(2,MOTOR12_8KHZ); // declare
const int MPU_addr=0x68;
MPU6050 mpu;

int16_t gyroX,gyroRate;

unsigned long currTime, prevTime=0, loopTime;
int d=0;
void setup(){
pinMode(A1, OUTPUT);
pinMode(A0, INPUT);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
mpu.initialize();
Serial.begin(9600);
radar_servo.attach(10);
motor2.setSpeed(200); // set motor2 speed as 250(max.) untill changed
motor1.setSpeed(200); // set motor1 speed as 250(max.) untill changed
motor1.run(RELEASE); // Release is an argument passed to function motor1.run which tells the motor 1 to stop.
motor2.run(RELEASE); // Release is an argument passed to function motor2.run which tells the motor 2 to stop.
}

void loop(){

for(servo_position=30; servo_position<=130;servo_position+=1)
{
radar_servo.write(servo_position);

float gyroAngle=0, gyroAngle1=0;

double time=0;
digitalWrite(A1,HIGH);
delayMicroseconds(10);
digitalWrite(A1,LOW);
time=pulseIn(A0,HIGH,5000);
d = (((time*330)/2000));
if(d>=80 && d<=500)
{

int rotA=servo_position-80;
int rotAabs = 0.85*abs(rotA);
while(gyroAngle1<rotAabs)
{

if(rotA>0){
motor2.setSpeed(150); // set motor speed to 250
motor1.setSpeed(150);
motor1.run(BACKWARD); // use differential drive logic to turn robot
motor2.run(BACKWARD);
currTime=millis();
loopTime=currTime-prevTime;
prevTime=currTime;
gyroX=mpu.getRotationX();
gyroRate=(map(gyroX, -32768, 32768, -250, 250));
gyroAngle= gyroAngle+((float)gyroRate*loopTime/1000);
gyroAngle1= abs(gyroAngle);}

else if(rotA<0){
motor2.setSpeed(150); // set motor speed to 250
motor1.setSpeed(150);
motor1.run(FORWARD); // use differential drive logic to turn robot
motor2.run(FORWARD);
currTime=millis();
loopTime=currTime-prevTime;
prevTime=currTime;
gyroX=mpu.getRotationX();
gyroRate=(map(gyroX, -32768, 32768, -250, 250));
gyroAngle= gyroAngle+((float)gyroRate*loopTime/1000);
gyroAngle1= abs(gyroAngle);}

else if (rotA==0){
motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE); }

}

motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE);
radar_servo.write(80);

while(d>=80 && d<=500){

if (d>250 && d<=500){
motor2.setSpeed(250); // set motor speed to 250
motor1.setSpeed(250);
motor1.run(FORWARD); // use differential drive logic to turn robot
motor2.run(BACKWARD);
delay(700); }

else if (d<=250 && d>=200){
motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE); }

else if (d<200 && d >= 80){
motor2.setSpeed(200); // set motor speed to 250
motor1.setSpeed(200);
motor1.run(BACKWARD); // use differential drive logic to turn robot
motor2.run(FORWARD); }

double time=0;
digitalWrite(A1,HIGH);
delayMicroseconds(10);
digitalWrite(A1,LOW);
time=pulseIn(A0,HIGH,5000);
d = (((time*330)/2000)); }}

motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE);

}

for(servo_position=130; servo_position>=30;servo_position-=1)
{
radar_servo.write(servo_position);

float gyroAngle=0, gyroAngle1=0;

double time=0;
digitalWrite(A1,HIGH);
delayMicroseconds(10);
digitalWrite(A1,LOW);
time=pulseIn(A0,HIGH,5000);
d = (((time*330)/2000));
if(d>=80 && d<=500)
{

int rotA=servo_position-80;
int rotAabs = 0.85*abs(rotA);
while(gyroAngle1<rotAabs)
{

if(rotA>0){
motor2.setSpeed(150); // set motor speed to 250
motor1.setSpeed(150);
motor1.run(BACKWARD); // use differential drive logic to turn robot
motor2.run(BACKWARD);
currTime=millis();
loopTime=currTime-prevTime;
prevTime=currTime;
gyroX=mpu.getRotationX();
gyroRate=(map(gyroX, -32768, 32768, -250, 250));
gyroAngle= gyroAngle+((float)gyroRate*loopTime/1000);
gyroAngle1= abs(gyroAngle);}

else if(rotA<0){
motor2.setSpeed(150); // set motor speed to 250
motor1.setSpeed(150);
motor1.run(FORWARD); // use differential drive logic to turn robot
motor2.run(FORWARD);
currTime=millis();
loopTime=currTime-prevTime;
prevTime=currTime;
gyroX=mpu.getRotationX();
gyroRate=(map(gyroX, -32768, 32768, -250, 250));
gyroAngle= gyroAngle+((float)gyroRate*loopTime/1000);
gyroAngle1= abs(gyroAngle);}

else if (rotA==0){
motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE); }

}

motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE);
radar_servo.write(80);

while(d>=80 && d<=500){

if (d>250 && d<=500){
motor2.setSpeed(250); // set motor speed to 250
motor1.setSpeed(250);
motor1.run(FORWARD); // use differential drive logic to turn robot
motor2.run(BACKWARD); }

else if (d<=250 && d>=200){
motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE);
delay(700); }

else if (d<200 && d >= 80){
motor2.setSpeed(200); // set motor speed to 250
motor1.setSpeed(200);
motor1.run(BACKWARD); // use differential drive logic to turn robot
motor2.run(FORWARD); }

double time=0;
digitalWrite(A1,HIGH);
delayMicroseconds(10);
digitalWrite(A1,LOW);
time=pulseIn(A0,HIGH,5000);
d = (((time*330)/2000)); }}

motor1.run(RELEASE); // use differential drive logic to turn robot
motor2.run(RELEASE);

}

}
can anyone fix the error

sketch_oct09a.ino:10:21: error: MPU6050.h: No such file or directory
sketch_oct09a:16: error: 'MPU6050' does not name a type
sketch_oct09a.ino: In function 'void setup()':
sketch_oct09a:30: error: 'mpu' was not declared in this scope
sketch_oct09a.ino: In function 'void loop()':
sketch_oct09a:69: error: 'mpu' was not declared in this scope
sketch_oct09a:82: error: 'mpu' was not declared in this scope
sketch_oct09a:161: error: 'mpu' was not declared in this scope
sketch_oct09a:174: error: 'mpu' was not declared in this scope

can anyone solve this error