Things used in this project

Hardware components:
11028 01
SparkFun Triple Axis Accelerometer and Gyro Breakout - MPU-6050
×1
11026 02
Jumper wires (generic)
×1
12002 04
Breadboard (generic)
If you just want to solder it together, you don't need this, but we suggest using pin headers for the gyroscope and wires.
×1
A000066 iso both
Arduino UNO & Genuino UNO
Nearly any Arduino will work for this, though the Uno is the standard Arduino.
×1

Schematics

Gyroscope wiring
This is a diagram of the wiring for this project. all 3 modes are filled in, so if you want you can use the breadboard, circuit or schematic view to wire your version!
gyroscope_wiring_dhq9thabFT.fzz

Code

WIP stablized gyroscopeArduino
Simply set up your gyroscope, plug the code in, and watch the fun on the serial monitor or serial plotter. Your numbers will be in degrees, but this is still WIP and may not work completely or at all.
/**
        _
   _  _ \_\  _
  |_|  /_\_\ | |_
          \_\
version 1.0.0 . this work is free to use by anyone, but please credit it if you are using it.
**/


#include<Wire.h>
#include<Serial.h>
/**
 * the #include 's are to tell the program to import the two libraries instead of writing them all out.
 * wire lets the gyroscope and the arduino communicate over the ic^2 connection.
 */
#define gyro_address 0x68 
/* change this number to the ic^2 adress of your gyroscope (in Hexidecimal, so find that column and ad a 0x if there isn't one), 
 *  though if you don't have one already, an Invenesense MPU-6050 will make the code work without edits.
*/
void setup() {
  int16_t accel_x, accel_y, accel_z, temp, gyro_x, gyro_y, gyro_z; 
  /**the int16_t defines 16 bits per variable. 15 can be used, but computers prefer 8, 16, 32, etc. change the number
   * to change the bits. these are left blank because the first thing to happen in the loop is them being filled with
   * the gyroscope's value. the math used has to be altered though, so some gyroscopes may use more bits. at this point,
   * you really need the documentation for the gyroscope.
   */
  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); //set this to false, and it doesn't end. simple enough, right?
  
  Wire.beginTransmission(gyro_address);
  Wire.write(0x6a);
  Wire.requestFrom(gyro_adress, 1, true);
  int usercontrol = Wire.read();
  Wire.endTransmission(true);
  Serial.begin(9600); 
  /**initiate 'Serial' library. most arduinos use 9600 baud (bits per second) but some use different amounts. 
   * again, check the documentation.*/
   double x_rotation = 0, y_rotation = 0, z_rotation = 0; 
   //these double variables have double the bits as others, so they can be longer and therefor more specific.
}

void loop() {
  // put your main code here, to run repeatedly:

}
Code version 1.1.0Arduino
This is an updated version of the code, with a few improvements but no filter or math yet..
/**
        _
   _  _ \_\  _
  |_|  /_\_\ | |_
          \_\
version 1.1.0 . this work is free to use by anyone, but please credit it if you are using it.
**/


#include<Wire.h>
/**
 * the #include 's are to tell the program to import the two libraries instead of writing them all out.
 * wire lets the gyroscope and the arduino communicate over the ic^2 connection.
 */
#define gyro_address 0x68 
/* change this number to the ic^2 adress of your gyroscope (in Hexidecimal, so find that column and add a 0x if there isn't one), 
 *  though if you don't have one already, an Invenesense MPU-6050 will make the code work without edits.
*/
int16_t accel_x, accel_y, accel_z, temp, gyro_x, gyro_y, gyro_z; 
/**the int16_t defines 16 bits per variable. 15 can be used, but computers prefer 8, 16, 32, etc. change the number
   * to change the bits. these are left blank because the first thing to happen in the loop is them being filled with
   * the gyroscope's value. the math used has to be altered though, so some gyroscopes may use more bits. at this point,
   * you really need the documentation for the gyroscope.
   */
  
void setup() {
  Wire.begin();
  Wire.beginTransmission(gyro_address);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true); //set this to false, and it doesn't end. simple enough, right?
  
  Wire.beginTransmission(gyro_address);
  Wire.write(0x6a);
  Wire.requestFrom(gyro_address, 1, true);
  int usercontrol = Wire.read();
  Wire.endTransmission(true);
  Serial.begin(9600); 
  /**initiate 'Serial' library. most arduinos use 9600 baud (bits per second) but some use different amounts. 
   * again, check the documentation.*/
   double x_rotation = 0, y_rotation = 0, z_rotation = 0; 
   //these double variables have double the bits as others, so they can be longer and therefor more specific.
}

void loop() {
  Wire.beginTransmission(gyro_address);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H).this number may change depending on how the gyroscope transferrs data.
  Wire.endTransmission(false);
  Wire.requestFrom(gyro_address, 14, true); // request a total of 14 registers (again, it may be different for your gyroscope)
  accel_x = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  accel_y = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  accel_z = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  temp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  /*
   * these 7 variables look familiar? we defined them as nothing in setup(). these store the raw values of the gyroscope.
   * once again, all these numbers may change. if you can, refer to a program someone else made with the same gyro to
   * save a couple headaches.
   */
   Serial.print(" | Accelerometer X:");
   Serial.print(accel_x);
   Serial.print(" | Accelerometer Y:");
   Serial.print(accel_y);
   Serial.print(" | Accelerometer Z:");
   Serial.print(accel_z);
   Serial.print(" | Temperature:");
   Serial.print(temp / 340.00 + 36.53);
   Serial.print(" | Gyro X:");
   Serial.print(gyro_x);
   Serial.print(" | Gyro Y:");
   Serial.print(gyro_y);
   Serial.print(" | Gyro Z:");
   Serial.println(gyro_z);
   delay(100);//this is important to make sure the board and arduino both aren't overloading. slower-operating gyros may need a bigger number.
}
Code version 1.2.1Arduino
The code now adds data to the gyroscope and accelerometer saved data arrays. This will allow the Kalman filter to make its predictions based on past data. very important! There are also some other small fixes. the filter will probably be added soon, but when is unknown. We're doing our best to implement the math.
/**
        _
   _  _ \_\  _
  |_|  /_\_\ | |_
          \_\
version 1.2.1 (filter still in progress, but data gets saved) . this work is free to use by anyone, but please credit it if you are using it.
**/


#include<Wire.h>
/**
 * the #include 's are to tell the program to import the two libraries instead of writing them all out.
 * wire lets the gyroscope and the arduino communicate over the ic^2 connection.
 */
#define gyro_address 0x68 
/* change this number to the ic^2 adress of your gyroscope (in Hexidecimal, so find that column and add a 0x if there isn't one), 
 *  though if you don't have one already, an Invenesense MPU-6050 will make the code work without edits.
*/
int16_t accel_x, accel_y, accel_z, temp, gyro_x, gyro_y, gyro_z; 
/**the int16_t defines 16 bits per variable. 15 can be used, but computers prefer 8, 16, 32, etc. change the number
   * to change the bits. these are left blank because the first thing to happen in the loop is them being filled with
   * the gyroscope's value. the math used has to be altered though, so some gyroscopes may use more bits. at this point,
   * you really need the documentation for the gyroscope.
   */
array gyro_data = []; //this stores the past data from the sensor, giving the kalman filter some of its neccasary data.
array accel_data = []; //this stores the past data from the sensor, giving the kalman filter some of its neccasary data.
array gyro_current = []; //this feeds into the main variable, but it's here just to better orginize the X, Y. & Z values.
array accel_current = []; //this feeds into the main variable, but it's here just to better orginize the X, Y. & Z values.

void setup() {
  Wire.begin();
  Wire.beginTransmission(gyro_address);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true); //set this to false, and it doesn't end. simple enough, right?
  
  Wire.beginTransmission(gyro_address);
  Wire.write(0x6a);
  Wire.requestFrom(gyro_address, 1, true);
  int usercontrol = Wire.read();
  Wire.endTransmission(true);
  Serial.begin(9600); 
  /**initiate 'Serial' library. most arduinos use 9600 baud (bits per second) but some use different amounts. 
   * again, check the documentation.*/
   double x_rotation = 0, y_rotation = 0, z_rotation = 0; 
   //these double variables have double the bits as others, so they can be longer and therefor more specific.
}

void loop() {
  Wire.beginTransmission(gyro_address);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H).this number may change depending on how the gyroscope transferrs data.
  Wire.endTransmission(false);
  Wire.requestFrom(gyro_address, 14, true); // request a total of 14 registers (again, it may be different for your gyroscope)
  accel_x = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  accel_y = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  accel_z = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  temp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  gyro_x = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  gyro_y = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  gyro_z = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  /*
   * these 7 variables look familiar? we defined them as nothing in setup(). these store the raw values of the gyroscope.
   * once again, all these numbers may change. if you can, refer to a program someone else made with the same gyro to
   * save a couple headaches.
   */
   accel_current = [accel_x,accel_y,accel_z];
   gyro_current = [gyro_x,gyro_y,gyro_z];
   Serial.print(" | Accelerometer X:");
   Serial.print(accel_x);
   Serial.print(" | Accelerometer Y:");
   Serial.print(accel_y);
   Serial.print(" | Accelerometer Z:");
   Serial.print(accel_z);
   Serial.print(" | Temperature:");
   Serial.print(temp / 340.00 + 36.53);
   Serial.print(" | Gyro X:");
   Serial.print(gyro_x);
   Serial.print(" | Gyro Y:");
   Serial.print(gyro_y);
   Serial.print(" | Gyro Z:");
   Serial.println(gyro_z);
   gyro_data[gyro_data.length()] = gyro_current;
   accel_data[accel_data.length()] = accel_current;
   /**
    * these two top statements add the current data from the gyro and accel to the data arrays, using a neat trick: every array counts 
    * starting at 0, but the .length() function starts at one. this allows us to create new data in the arrays constantly! be warned:
    * using a constantly adding array can be dangerous, especially with large amounts of data.
    */
   delay(100);//this is important to make sure the board and arduino both aren't overloading. slower-operating gyros may need a bigger number.
}

Credits

Thanks to Arduino team and Unknown.

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

3D-Printed Prank Vibrating Cup
Easy
  • 1,271
  • 14

Full instructions

The instant your prankee lifts up this 3D-printed cup it starts violently vibrating! The electronics separate from the cup for easy washing.

The Magic Display With Arduino - The Mind Reader
Easy
  • 1,495
  • 6

Full instructions

The Magic Display With Arduino can figure out with a few clues what is your choose on a card with 64 options of numbers, letters or symbols.

Very First Hands On Arduino - Voice Activated LED
Easy
  • 770
  • 9

Full instructions

Use voice command to turn LED on or off.

Get Connected! WiFi Registration Using AP Mode!
Easy
  • 1,447
  • 16

Full instructions

Stop reflashing your devices everytime the WiFi password changes! Learn how to use anduinoWiFi and AP mode to initiate WiFi registration!

Temperature Sensor
Easy
  • 401
  • 5

Full instructions

A photon that measures the temperature and humididty with a DHT 22 sensor and communicates the temperature data to another photon.

Car Blinker
Easy
  • 238
  • 4

Protip

Blink for Vehicles, such as bikes and skates. Easy.

ProjectsCommunitiesContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login
Respect project
Feedback