Thursday, June 9, 2016

MPU6050 Controlling LEDs

Hi guys!I started to build a quadcopter and today I will show you how to use a MPU6050 gyro.



First list of parts.
-Arduino
-MPU6050
-Jumper Cables
-LEDs

I am not going to share a schematic because the connections are easy.I put an image of the connections of gyro.Leds connections written in the code.
We will only use the 4 pin of sensor.
I used a library(MPU6050) and I strongly recommend to read comments.
Arduino Code

//#include <SoftwareSerial.h>
//SoftwareSerial mySerial(10, 11);  //This part is not necessary for this mini project.
#define FRONT_LED 6
#define BOTTOM_LED 9      //Defining Led pins
#define RIGHT_LED 3
#define LEFT_LED 5
char ch;
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
    #include "Wire.h"
#endif
char blu;
MPU6050 mpu;
const int buttonPin = 2; 
int buttonState = 0; 

// 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
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector
volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
void dmpDataReady() 
{
    mpuInterrupt = true;
}


void setup() 
{
  pinMode(buttonPin, INPUT);
    

    #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
        Wire.begin();
        TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
    #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
        Fastwire::setup(400, true);
    #endif

    Serial.begin(115200);

    while (!Serial); // wait for Leonardo enumeration, others continue immediately

    Serial.println(F("Initializing I2C devices..."));
    mpu.initialize();

    Serial.println(F("Testing device connections..."));
    Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
    Serial.println(F("Initializing DMP..."));
    devStatus = mpu.dmpInitialize();

    mpu.setXAccelOffset(-100);//These are my offset values you should calibrate your sensor with another code.Google it!!
    mpu.setYAccelOffset(199);
    mpu.setZAccelOffset(1978);
    mpu.setXGyroOffset(133);
    mpu.setYGyroOffset(-14);
    mpu.setZGyroOffset(82); 
    if (devStatus == 0) {
        // turn on the DMP, now that it's ready
        Serial.println(F("Enabling DMP..."));
        mpu.setDMPEnabled(true);
        Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();
        Serial.println(F("DMP ready! Waiting for first interrupt..."));
        dmpReady = true;
        packetSize = mpu.dmpGetFIFOPacketSize();
    } else {
        Serial.print(F("DMP Initialization failed (code "));
        Serial.print(devStatus);
        Serial.println(F(")"));
    }
    //init LEDs
    pinMode(FRONT_LED, OUTPUT);
    pinMode(BOTTOM_LED, OUTPUT);    
    pinMode(RIGHT_LED, OUTPUT);
    pinMode(LEFT_LED, OUTPUT);     
}


void flashLEDs(int x, int y, int z)
{


  buttonState = digitalRead(buttonPin);
          if(buttonState == LOW){ //I added a button to check a "thing" but this won't make you a trouble.
    if(y==0 && z==0)
  {
     analogWrite(FRONT_LED,0);
     analogWrite(BOTTOM_LED,0);  
      analogWrite(LEFT_LED,0);
     analogWrite(RIGHT_LED,0); 
  }
    if (y > 0) {
     analogWrite(FRONT_LED, y*(5.4));
        analogWrite(BOTTOM_LED, 0);  
                 
    } else {
        analogWrite(BOTTOM_LED, (y*(-1)*5.4));      
        analogWrite(FRONT_LED, 0);      
    }
    if (z > 0) {
        analogWrite(LEFT_LED, z*(5.4));
        analogWrite(RIGHT_LED,0);          
    } else {
        analogWrite(RIGHT_LED, z*(5.4)*-1);      
        analogWrite(LEFT_LED,0);            
    }    
    Serial.print(x);Serial.print("\t");  
    Serial.print(y);Serial.print("\t");  
    Serial.println(z);
          }
    else if(buttonState== HIGH)
    {      analogWrite(FRONT_LED,255);
     analogWrite(BOTTOM_LED,0);  
      analogWrite(LEFT_LED,0);
     analogWrite(RIGHT_LED,255); }    
}

void loop() 
{
    int ch = mySerial.read();
    if (!dmpReady) return;
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();
    fifoCount = mpu.getFIFOCount();
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        mpu.resetFIFO();
        Serial.println(F("FIFO overflow!"));

    } else if (mpuIntStatus & 0x02) {
        while (fifoCount < packetSize) {
          fifoCount = mpu.getFIFOCount();
        }

        mpu.getFIFOBytes(fifoBuffer, packetSize);        
        fifoCount -= packetSize;
        mpu.dmpGetQuaternion(&q, fifoBuffer);
        mpu.dmpGetGravity(&gravity, &q);
        mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
        int t=2;


        flashLEDs(ypr[0] * 180/M_PI, ypr[1] * 180/M_PI, ypr[2] * 180/M_PI);

     
 
    }
}




--------------------------------------------------------------

Video of my application


You can ask anything you want in the comment section.Keep working!!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...