In: Electrical Engineering
need a code for mpu 6050 gyro module for arduino
it needs to read the angle of the pendulum and feed information
back into stepper motor to try to attain balance
Actually this my code..and you need to modify some parts from this code
#include<wire.h> //wire library
#include<i2Cdev.h>//i2C Library
#include<MPU6050.h> //mpu6050 library
MPU6050 mpu;
int16_t aa,ab,ac;
int16_t ba,bb,bc;
#define in1 5
#define in2 6
void setup(){
Serial.begin(9600);
Serial.println("Initialize MPU");
mpu.initialize();
//Serial.println(mpu.testConnection() ? "Connected" : "Connection
failed"); pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
}
void loop(){
mpu.getMotion6(&aa, &ab, &ac, &ba, &bb,
&bc);
aa = map(aa, -17000, 17000, -1500, 1500);
//Serial.println(aa);
if(aa > 0){
if(aa<255){
Serial.println(aa);
analogWrite(in2,aa);
}
else{
Serial.println("+255");
analogWrite(pin2,255);
}
}
if(aa<0){
if(aa>-255){
Serial.println(aa);
analogWrite(in1, aa-aa-aa);
}
.........................................................................................................
else{
Serial.println("-255");
analogWrite(in1, 255);
}
}
delay(1000);
}