2014-06-12

Arduino + MPU-6050 (Gyroscope + Acelerometer) - GY-521 + 2 Servo motors (Simple Code)

As a novice I am trying to put thing together. I am happy that I just made it work.

Arduino Uno as processor
GY-521 / MPU-6050 as accelerometer and Gyroscope.
Breadboard
2 9G Servos Bracket Sensor Mount Pan / Tilt Kit for Gyro - Translucent Blue http://www.dx.com/p/214081

http://playground.arduino.cc/Main/MPU-6050 has starting info on MPU-6050

I used two libraries from I2CDevLib: I2Cdev.h and MPU6050.h
https://github.com/jrowberg/i2cdevlib/

Wire and Servo libraries are already included with Arduino

http://playground.arduino.cc/Learning/I2C
http://www.arduino.cc/en/Reference/Wire
http://www.arduino.cc/en/Reference/Servo

MPU-6050 chip communicates with Arduino using I2C or (read as I square C) communication protocol. I2C bus one of the most common method of communication between varies microcontrollers, sensors etc. All I2C communication is handled by Arduino wire library.

Servo library is needed to control the servo motors.



The code below only uses values from accelerometer. The axis paralel to gravity will have additional 1g force. But shakings from hand will also effect accelerometer. You can see the effect as a shaky servo. I will try to correct it in next version. I am trying to be patient when looking for ways to fix it.
//MPU 6050 2 axis Servo kontrol 

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

MPU6050 mpu;

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

Servo myservoY;
Servo myservoX;

int valY;
int prevValY;

int valX;
int prevValX;

void setup() 
{
  Wire.begin();
  Serial.begin(38400);
  Serial.println("Initialize MPU");
  mpu.initialize();
  Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
  myservoY.attach(9);
  myservoX.attach(10);
}
void loop() 
{
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  valY = map(ay, -17000, 17000, 0, 179);
  valX = map(ax, -17000, 17000, 0, 179);
  if (valY != prevValY)
  {
    myservoY.write(valY);
    prevValY = valY;
  }

  if (valX != prevValX)
  {
    myservoX.write(valX);
    prevValX = valX;
  }
  delay(50);
}


The connection scheme is
  • Arduino’s +5V to Vcc of MPU6050
  • Arduino’s GND to GND of MPU6050
  • Arduino’s SCL (AN5 in UNO board) to SCL of MPU6050
  • Arduino’s SDA (AN4 in UNO board) to SCK of MPU6050
  • Arduino’s digital pin 2 (INT0) to INT of MPU6050
I improved the design a month later:
Improvements: 
  • Faster time response
  • Fewer unwanted vibrations.
  • No change needed in cable connetctions


Hiç yorum yok: