Skip to content Skip to sidebar Skip to footer

MPU6050 Arduino Accelerometer Gyroscope Code and Tutorial

MPU6050 Arduino Tutorial

A. Explanation of the Arduino MPU6050

MPU6050 Arduino Tutorial & Code -The MPU050 is a chip that includes an Accelerometer, Gyroscope, and Temperature Sensor. In the market, the MPU6050 Arduino module is available which can be easy to use using Arduino board. The price of this MPU module is quite cheap, you can see it in e commerce

MPU6050 Arduino is included in the IMU (Inertial Measurement Unit). IMU is also known as a Magnetometer, which is a combination of devices capable of measuring and obtaining values ​​for velocity, orientation and gravitational force.

Common examples of Uses / Applications of this IMU device are like UAV, QuadCopter, Smart Robot, Smartphones, Console games, Gimbal for Video & Image Stabilization, Gaming Headset, Virtual Reality, etc.

Before we look in the MPU6050 Arduino program, let's understand the basics of the Accelerometer, Gyroscope and Temperature Sensor.

 

a. MPU6050 Accelerometer

Accelerometer is a device that functions to determine the orientation (direction) based on the acceleration being measured. An example of using the Accelerometer is on a smartphone, such as changing the wallpaper only by movement.

Now, acceleration is the rate of change in velocity of an object. When an object is moved at a constant velocity, it is said to have no acceleration. However, when an object is moved at a non-constant speed, it is said to have acceleration.

Accelerometer does not use earth's gravity in its operation, but it needs to take into account static acceleration such as gravitational force during the process.

Accelerometer MPU6050 Features:

  • 3 Axis
  • Programmable full scale range of ± 2g, ± 4g, ± 8g and ± 16g
  • 16-bit ADC
  • Current Low power mode: 10µA at 1.25Hz, 20µA at 5Hz, 60µA at 20Hz, 110µA at 40Hz
  • Normal current mode: 500µA
  • For details, please see the datasheet on page 10.

 

2. MPU6050 Gyroscope

A gyroscope is a device that functions to determine rotation (rotation). An example of use on a smartphone is a compass. The gyroscope uses Earth's gravity in its operation.

Gyroscope MPU6050 Features :

  • 3 Axis
  • Digital-output X-, Y-, and Z-Axis angular rate sensors (gyroscopes) with a user-programmable fullscale range of ± 250, ± 500, ± 1000, and ± 2000 ° / sec for precise tracking of fast and slow motion .
  • 16-bit ADC
  • Standby current: 5µA
  • Operating current: 3.6mA
  • For details, please see the datasheet on page 10.

 

3.Temperature Sensore MPU6050

In this chip there is also a temperature sensor that we can use with specifications:

  • - 40 to + 85 ° C
  • Sensitivity 340 LSB / ° C (without trim)
  • Accuracy + 1 ° C

 

B. How Does the MPU6070 Work?

As explained on the theoretical basis above, this MPU chip has an accelerometer, gyroscope and temperature sensor. When this MPU performs an operation, the value data from each sensor will enter the Digital Motion Processor (DPM). Not only that, DMP can also be used to retrieve external sensor data (if any) connected to this MPU module.

After that, DMP will send the data using I2C communication (SDA & SCL) and will be received by Arduino.

 

C. MPU6050 Arduino Connection

To be able to connect the MPU6050 Arduino sensor module, we need to know the pinout on the module. You can see the pinout in the following image:

MPU6050 Arduino Accelerometer Gyroscope Code and Tutorial


PinOut explanation:

  • Basically, this chip works with a 3.3V voltage. Using a regulator ic like "4B2X" with an output of 3.3V, we can use VCC with a value of + 5V.
  • I2C Clock and Data are used for communication with microcontrollers such as Arduno Uno.
  • External I2C Data and Clock are used to communicate to external sensors.
  • The I2C Address Bit is used to change the internal I2C address of the module if the default address of this module conflicts with another I2C device.
    If the pin is AD0 LOW, then the address is 0x68 (default).
    If pin AD0 is HIGH, the address is 0x69.
  • An interrupt is an interrupt output.

The mpu6050 to Arduino Wiring are as follows:

MPU6050 Arduino Accelerometer Gyroscope Code and Tutorial

 

D. MPU6050 Arduino Library

Now we enter the main part of this tutorial, which is how we can use the MPU6050 using Arduno. We can retrieve the data from this module easily, assisted by a library that has been created by ElectronicCats.

For library installation, please go to the Library Manager by:

  • Click the Sketch menu
  • Click Include Library -> Manage Libraries
  • Search "MPU6050" -> select by Electronic Cats
  • Then click Install

After the library is installed, then we will then program Arduino with the program that I provide below. There are 6 program modes, please upload them to your Arduino board,

 

E. MPU6050 Arduino Program Code

1.Yaw Pitch Roll Mode (3D)

Yaw Pitch Roll mode is a technique of movement in 3 dimensions. This mode follows the principle of an airplane, where rotation / displacement occurs in 3 axes or 3 axes, Yaw, Pitch and Roll.
  • Yaw is the rotation of the object on the vertical axis.
  • Pitch is the rotation of an object on a lateral axis.
  • Roll is the rotation of an object on its longitudinal axis. 
to give an understanding of this, look at the following image

Cara Menggunakan MPU6050 Arduino Accelerometer & Gyroscope
Yaw Pitch Roll Source : wikipedia
 
Please upload the program below, then open the Arduino Serial monitor.

#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

Quaternion q;
VectorFloat gravity;
uint8_t fifoBuffer[64];

uint16_t packetSize;
uint16_t fifoCount;
float ypr[3];

void setup() {
  Serial.begin(115200);
  Wire.begin();
  TWBR = 24;
  mpu.initialize();
  mpu.dmpInitialize();

  // This offset value can be changed as needed
  mpu.setXAccelOffset(-1343);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(1033);
  mpu.setXGyroOffset(19);
  mpu.setYGyroOffset(-27);
  mpu.setZGyroOffset(16);
  mpu.setDMPEnabled(true);
  packetSize = mpu.dmpGetFIFOPacketSize();
  fifoCount = mpu.getFIFOCount();

  Serial.println("Stabilizing process ... Please wait ...");
  delay(18000);
  mpu.resetFIFO();
}

void loop() {
  while (fifoCount < packetSize) {
    fifoCount = mpu.getFIFOCount();
  }

  if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));
  }
  else {

    if (fifoCount % packetSize != 0) {

      mpu.resetFIFO();
    }
    else {

      while (fifoCount >= packetSize) {
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        fifoCount -= packetSize;
      }
      mpu.dmpGetQuaternion(&q, fifoBuffer);
      mpu.dmpGetGravity(&gravity, &q);
      mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);

      Serial.print("ypr\t");
      Serial.print(ypr[0] * 180 / PI);
      Serial.print("\t");
      Serial.print(ypr[1] * 180 / PI);
      Serial.print("\t");
      Serial.print(ypr[2] * 180 / PI);
      Serial.println();
    }
  }
}
 
The result is:
Cara Menggunakan MPU6050 Arduino Accelerometer & Gyroscope

 
 

2. Quaternion Mode (4D)

Quaternion mode is a technique of moving objects into 4 dimensions. Now, we will try the following program to display values in the Quartenion technique:

#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

Quaternion q;
uint8_t fifoBuffer[64];
uint16_t packetSize;
uint16_t fifoCount;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  TWBR = 24;
  mpu.initialize();
  mpu.dmpInitialize();

  // This offset value can be changed as needed
  mpu.setXAccelOffset(-1343);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(1033);
  mpu.setXGyroOffset(19);
  mpu.setYGyroOffset(-27);
  mpu.setZGyroOffset(16);
  mpu.setDMPEnabled(true);
  packetSize = mpu.dmpGetFIFOPacketSize();
  fifoCount = mpu.getFIFOCount();

  Serial.println("Stabilizing process ... Please wait ...");
  delay(18000);
  mpu.resetFIFO();
}

void loop() {
  while (fifoCount < packetSize) {
    fifoCount = mpu.getFIFOCount();
  }

  if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));
  }
  else {

    if (fifoCount % packetSize != 0) {

      mpu.resetFIFO();
    }
    else {

      while (fifoCount >= packetSize) {
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        fifoCount -= packetSize;
      }
      mpu.dmpGetQuaternion(&q, fifoBuffer);
      Serial.print("quat\t");
      Serial.print(q.w);
      Serial.print("\t");
      Serial.print(q.x);
      Serial.print("\t");
      Serial.print(q.y);
      Serial.print("\t");
      Serial.println(q.z);
    }
  }
}
 
The results is:
Cara Menggunakan MPU6050 Arduino Accelerometer & Gyroscope

 

3. Euler Mode (Degree)

Euler's mode is to display the result of angular movement from 3 dimensions into degrees. Here is the program:

#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

Quaternion q;
uint8_t fifoBuffer[64];
uint16_t packetSize;
uint16_t fifoCount;

float euler[3];

void setup() {
  Serial.begin(115200);
  Wire.begin();
  TWBR = 24;
  mpu.initialize();
  mpu.dmpInitialize();

  // This offset value can be changed as needed
  mpu.setXAccelOffset(-1343);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(1033);
  mpu.setXGyroOffset(19);
  mpu.setYGyroOffset(-27);
  mpu.setZGyroOffset(16);
  mpu.setDMPEnabled(true);
  packetSize = mpu.dmpGetFIFOPacketSize();
  fifoCount = mpu.getFIFOCount();

  Serial.println("Stabilizing process ... Please wait ...");
  delay(18000);
  mpu.resetFIFO();
}

void loop() {
  while (fifoCount < packetSize) {
    fifoCount = mpu.getFIFOCount();
  }

  if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));
  }
  else {

    if (fifoCount % packetSize != 0) {

      mpu.resetFIFO();
    }
    else {

      while (fifoCount >= packetSize) {
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        fifoCount -= packetSize;
      }

      mpu.dmpGetQuaternion(&q, fifoBuffer);
      mpu.dmpGetEuler(euler, &q);
      Serial.print("euler\t");
      Serial.print(euler[0] * 180 / M_PI);
      Serial.print("\t");
      Serial.print(euler[1] * 180 / M_PI);
      Serial.print("\t");
      Serial.println(euler[2] * 180 / M_PI);
    }
  }
}
 
The results is:
Cara Menggunakan MPU6050 Arduino Accelerometer & Gyroscope

 

4. RealAccel Mode (Real Acceleration)

RealAccel mode is to display real data from acceleration without considering gravity. The programs are as follows:

#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

Quaternion q;
uint8_t fifoBuffer[64];
uint16_t packetSize;
uint16_t fifoCount;
VectorInt16 aa;
VectorInt16 aaReal;
VectorFloat gravity;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  TWBR = 24;
  mpu.initialize();
  mpu.dmpInitialize();

  // This offset value can be changed as needed
  mpu.setXAccelOffset(-1343);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(1033);
  mpu.setXGyroOffset(19);
  mpu.setYGyroOffset(-27);
  mpu.setZGyroOffset(16);
  mpu.setDMPEnabled(true);
  packetSize = mpu.dmpGetFIFOPacketSize();
  fifoCount = mpu.getFIFOCount();

  Serial.println("Stabilizing process ... Please wait ...");
  delay(18000);
  mpu.resetFIFO();
}

void loop() {
  while (fifoCount < packetSize) {
    fifoCount = mpu.getFIFOCount();
  }

  if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));
  }
  else {

    if (fifoCount % packetSize != 0) {

      mpu.resetFIFO();
    }
    else {

      while (fifoCount >= packetSize) {
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        fifoCount -= packetSize;
      }

      mpu.dmpGetQuaternion(&q, fifoBuffer);
      mpu.dmpGetAccel(&aa, fifoBuffer);
      mpu.dmpGetGravity(&gravity, &q);
      mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
      Serial.print("areal\t");
      Serial.print(aaReal.x);
      Serial.print("\t");
      Serial.print(aaReal.y);
      Serial.print("\t");
      Serial.println(aaReal.z);
    }
  }
}
 
The results is:
Cara Menggunakan MPU6050 Arduino Accelerometer & Gyroscope

 

5. WorldAccel Mode (World-Frame Acceleration)

World Accel mode is a model in which rotation based on the known orientation of quaternion and gravity is not used. The programs are:

#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

Quaternion q;
uint8_t fifoBuffer[64];
uint16_t packetSize;
uint16_t fifoCount;
VectorInt16 aa;
VectorInt16 aaReal;
VectorFloat gravity;
VectorInt16 aaWorld;

void setup() {
  Serial.begin(115200);
  Wire.begin();
  TWBR = 24;
  mpu.initialize();
  mpu.dmpInitialize();

  // This offset value can be changed as needed
  mpu.setXAccelOffset(-1343);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(1033);
  mpu.setXGyroOffset(19);
  mpu.setYGyroOffset(-27);
  mpu.setZGyroOffset(16);
  mpu.setDMPEnabled(true);
  packetSize = mpu.dmpGetFIFOPacketSize();
  fifoCount = mpu.getFIFOCount();

  Serial.println("Stabilizing process ... Please wait ...");
  delay(18000);
  mpu.resetFIFO();
}

void loop() {
  while (fifoCount < packetSize) {
    fifoCount = mpu.getFIFOCount();
  }

  if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));
  }
  else {

    if (fifoCount % packetSize != 0) {

      mpu.resetFIFO();
    }
    else {

      while (fifoCount >= packetSize) {
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        fifoCount -= packetSize;
      }

     mpu.dmpGetQuaternion(&q, fifoBuffer);
      mpu.dmpGetAccel(&aa, fifoBuffer);
      mpu.dmpGetGravity(&gravity, &q);
      mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity);
      mpu.dmpGetLinearAccelInWorld(&aaWorld, &aaReal, &q);
      Serial.print("aworld\t");
      Serial.print(aaWorld.x);
      Serial.print("\t");
      Serial.print(aaWorld.y);
      Serial.print("\t");
      Serial.println(aaWorld.z);
    }
  }
}

The results is:
Cara Menggunakan MPU6050 Arduino Accelerometer & Gyroscope

 

6. Teapot Mode (Visualisasi Processing)

Teapot mode is displaying the quaternion value which is used to visualize the mpu6050 module in processing. The Arduino program is as follows:

#include "MPU6050_6Axis_MotionApps20.h"
#include "Wire.h"

MPU6050 mpu;

Quaternion q;
uint8_t fifoBuffer[64];
uint16_t packetSize;
uint16_t fifoCount;
uint8_t teapotPacket[14] = { '$', 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0x00, 0x00, '\r', '\n' };


void setup() {
  Serial.begin(115200);
  Wire.begin();
  TWBR = 24;
  mpu.initialize();
  mpu.dmpInitialize();

  // This offset value can be changed as needed
  mpu.setXAccelOffset(-1343);
  mpu.setYAccelOffset(-1155);
  mpu.setZAccelOffset(1033);
  mpu.setXGyroOffset(19);
  mpu.setYGyroOffset(-27);
  mpu.setZGyroOffset(16);
  mpu.setDMPEnabled(true);
  packetSize = mpu.dmpGetFIFOPacketSize();
  fifoCount = mpu.getFIFOCount();

  Serial.println("Stabilizing process ... Please wait ...");
  delay(18000);
  mpu.resetFIFO();
}

void loop() {
  while (fifoCount < packetSize) {
    fifoCount = mpu.getFIFOCount();
  }

  if (fifoCount == 1024) {

    mpu.resetFIFO();
    Serial.println(F("FIFO overflow!"));
  }
  else {

    if (fifoCount % packetSize != 0) {

      mpu.resetFIFO();
    }
    else {

      while (fifoCount >= packetSize) {
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        fifoCount -= packetSize;
      }

      teapotPacket[2] = fifoBuffer[0];
      teapotPacket[3] = fifoBuffer[1];
      teapotPacket[4] = fifoBuffer[4];
      teapotPacket[5] = fifoBuffer[5];
      teapotPacket[6] = fifoBuffer[8];
      teapotPacket[7] = fifoBuffer[9];
      teapotPacket[8] = fifoBuffer[12];
      teapotPacket[9] = fifoBuffer[13];
      Serial.write(teapotPacket, 14);
      teapotPacket[11]++; // packetCount, loops at 0xFF on purpose
    }
  }
}
 
The Processing program is as follows::
import processing.serial.*;
import processing.opengl.*;
import toxi.geom.*;
import toxi.processing.*;

// NOTE: requires ToxicLibs to be installed in order to run properly.
// 1. Download from http://toxiclibs.org/downloads
// 2. Extract into [userdir]/Processing/libraries
//    (location may be different on Mac/Linux)
// 3. Run and bask in awesomeness

ToxiclibsSupport gfx;

Serial port;                         // The serial port
char[] teapotPacket = new char[14];  // InvenSense Teapot packet
int serialCount = 0;                 // current packet byte position
int synced = 0;
int interval = 0;

float[] q = new float[4];
Quaternion quat = new Quaternion(1, 0, 0, 0);

float[] gravity = new float[3];
float[] euler = new float[3];
float[] ypr = new float[3];

void setup() {
    // 300px square viewport using OpenGL rendering
    size(300, 300, OPENGL);
    gfx = new ToxiclibsSupport(this);

    // setup lights and antialiasing
    lights();
    smooth();
  
    // display serial port list for debugging/clarity
    println(Serial.list());

    // get the first available port (use EITHER this OR the specific port code below)
    String portName = Serial.list()[0];
    
    // get a specific serial port (use EITHER this OR the first-available code above)
    //String portName = "COM4";
    
    // open the serial port
    port = new Serial(this, portName, 115200);
    
    // send single character to trigger DMP init/start
    // (expected by MPU6050_DMP6 example Arduino sketch)
    port.write('r');
}

void draw() {
    if (millis() - interval > 1000) {
        // resend single character to trigger DMP init/start
        // in case the MPU is halted/reset while applet is running
        port.write('r');
        interval = millis();
    }
    
    // black background
    background(0);
    
    // translate everything to the middle of the viewport
    pushMatrix();
    translate(width / 2, height / 2);

    // 3-step rotation from yaw/pitch/roll angles (gimbal lock!)
    // ...and other weirdness I haven't figured out yet
    //rotateY(-ypr[0]);
    //rotateZ(-ypr[1]);
    //rotateX(-ypr[2]);

    // toxiclibs direct angle/axis rotation from quaternion (NO gimbal lock!)
    // (axis order [1, 3, 2] and inversion [-1, +1, +1] is a consequence of
    // different coordinate system orientation assumptions between Processing
    // and InvenSense DMP)
    float[] axis = quat.toAxisAngle();
    rotate(axis[0], -axis[1], axis[3], axis[2]);

    // draw main body in red
    fill(255, 0, 0, 200);
    box(10, 10, 200);
    
    // draw front-facing tip in blue
    fill(0, 0, 255, 200);
    pushMatrix();
    translate(0, 0, -120);
    rotateX(PI/2);
    drawCylinder(0, 20, 20, 8);
    popMatrix();
    
    // draw wings and tail fin in green
    fill(0, 255, 0, 200);
    beginShape(TRIANGLES);
    vertex(-100,  2, 30); vertex(0,  2, -80); vertex(100,  2, 30);  // wing top layer
    vertex(-100, -2, 30); vertex(0, -2, -80); vertex(100, -2, 30);  // wing bottom layer
    vertex(-2, 0, 98); vertex(-2, -30, 98); vertex(-2, 0, 70);  // tail left layer
    vertex( 2, 0, 98); vertex( 2, -30, 98); vertex( 2, 0, 70);  // tail right layer
    endShape();
    beginShape(QUADS);
    vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(  0, -2, -80); vertex(  0, 2, -80);
    vertex( 100, 2, 30); vertex( 100, -2, 30); vertex(  0, -2, -80); vertex(  0, 2, -80);
    vertex(-100, 2, 30); vertex(-100, -2, 30); vertex(100, -2,  30); vertex(100, 2,  30);
    vertex(-2,   0, 98); vertex(2,   0, 98); vertex(2, -30, 98); vertex(-2, -30, 98);
    vertex(-2,   0, 98); vertex(2,   0, 98); vertex(2,   0, 70); vertex(-2,   0, 70);
    vertex(-2, -30, 98); vertex(2, -30, 98); vertex(2,   0, 70); vertex(-2,   0, 70);
    endShape();
    
    popMatrix();
}

void serialEvent(Serial port) {
    interval = millis();
    while (port.available() > 0) {
        int ch = port.read();

        if (synced == 0 && ch != '$') return;   // initial synchronization - also used to resync/realign if needed
        synced = 1;
        print ((char)ch);

        if ((serialCount == 1 && ch != 2)
            || (serialCount == 12 && ch != '\r')
            || (serialCount == 13 && ch != '\n'))  {
            serialCount = 0;
            synced = 0;
            return;
        }

        if (serialCount > 0 || ch == '$') {
            teapotPacket[serialCount++] = (char)ch;
            if (serialCount == 14) {
                serialCount = 0; // restart packet byte position
                
                // get quaternion from data packet
                q[0] = ((teapotPacket[2] << 8) | teapotPacket[3]) / 16384.0f;
                q[1] = ((teapotPacket[4] << 8) | teapotPacket[5]) / 16384.0f;
                q[2] = ((teapotPacket[6] << 8) | teapotPacket[7]) / 16384.0f;
                q[3] = ((teapotPacket[8] << 8) | teapotPacket[9]) / 16384.0f;
                for (int i = 0; i < 4; i++) if (q[i] >= 2) q[i] = -4 + q[i];
                
                // set our toxilibs quaternion to new data
                quat.set(q[0], q[1], q[2], q[3]);

                /*
                // below calculations unnecessary for orientation only using toxilibs
                
                // calculate gravity vector
                gravity[0] = 2 * (q[1]*q[3] - q[0]*q[2]);
                gravity[1] = 2 * (q[0]*q[1] + q[2]*q[3]);
                gravity[2] = q[0]*q[0] - q[1]*q[1] - q[2]*q[2] + q[3]*q[3];
    
                // calculate Euler angles
                euler[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
                euler[1] = -asin(2*q[1]*q[3] + 2*q[0]*q[2]);
                euler[2] = atan2(2*q[2]*q[3] - 2*q[0]*q[1], 2*q[0]*q[0] + 2*q[3]*q[3] - 1);
    
                // calculate yaw/pitch/roll angles
                ypr[0] = atan2(2*q[1]*q[2] - 2*q[0]*q[3], 2*q[0]*q[0] + 2*q[1]*q[1] - 1);
                ypr[1] = atan(gravity[0] / sqrt(gravity[1]*gravity[1] + gravity[2]*gravity[2]));
                ypr[2] = atan(gravity[1] / sqrt(gravity[0]*gravity[0] + gravity[2]*gravity[2]));
    
                // output various components for debugging
                //println("q:\t" + round(q[0]*100.0f)/100.0f + "\t" + round(q[1]*100.0f)/100.0f + "\t" + round(q[2]*100.0f)/100.0f + "\t" + round(q[3]*100.0f)/100.0f);
                //println("euler:\t" + euler[0]*180.0f/PI + "\t" + euler[1]*180.0f/PI + "\t" + euler[2]*180.0f/PI);
                //println("ypr:\t" + ypr[0]*180.0f/PI + "\t" + ypr[1]*180.0f/PI + "\t" + ypr[2]*180.0f/PI);
                */
            }
        }
    }
}

void drawCylinder(float topRadius, float bottomRadius, float tall, int sides) {
    float angle = 0;
    float angleIncrement = TWO_PI / sides;
    beginShape(QUAD_STRIP);
    for (int i = 0; i < sides + 1; ++i) {
        vertex(topRadius*cos(angle), 0, topRadius*sin(angle));
        vertex(bottomRadius*cos(angle), tall, bottomRadius*sin(angle));
        angle += angleIncrement;
    }
    endShape();
    
    // If it is not a cone, draw the circular top cap
    if (topRadius != 0) {
        angle = 0;
        beginShape(TRIANGLE_FAN);
        
        // Center point
        vertex(0, 0, 0);
        for (int i = 0; i < sides + 1; i++) {
            vertex(topRadius * cos(angle), 0, topRadius * sin(angle));
            angle += angleIncrement;
        }
        endShape();
    }
  
    // If it is not a cone, draw the circular bottom cap
    if (bottomRadius != 0) {
        angle = 0;
        beginShape(TRIANGLE_FAN);
    
        // Center point
        vertex(0, tall, 0);
        for (int i = 0; i < sides + 1; i++) {
            vertex(bottomRadius * cos(angle), tall, bottomRadius * sin(angle));
            angle += angleIncrement;
        }
        endShape();
    }
}
 
The results is:
 

Incoming search terms:

Source: Arduino32 

Post a Comment for "MPU6050 Arduino Accelerometer Gyroscope Code and Tutorial"