I bought this $30 triple axis accelerometer from sparkfun... The first thing we have to do is get it working!
- Wire it up. The MPU6050 uses a method of communication called IC2. I'm not an expert on this, but this is what you'll need to connect to an arduino uno to get it working:
MPU6050 Arduino Uno VDD 3.3V GND GND SCL A5 SDA A4 VIO 3.3V
That last one (VIO->3.3V) I'm not 100% on: some forums have it while others don't, but was necessary for me to get mine working. Other forums suggest INT to Digital pin 2 and CLK to GND but mine works without these.
- Download some sample code. Head over to github.com/jrowberg/i2cdevlib and click the button on the right to download the zip file of the entire collection (If you want, github has a great app to download to keep your code synced with the latest updates of this). Navigate to Arduino/MPU6050/Examples/MPU6050_raw/MPU6050_raw.ino and open that in your desktop Arduino software. But wait to upload, if you try it, you'll get a bunch of errors.
Install Libraries. click on the "Sketch" menu and click on "Import Library..." and then "Add Library" Navigate to where you downloaded the files from step 2 and click on the folder MPU6050 to install that library. Do the same thing with the folder called IC2Dev. Both libraries should show up at the bottom of the list.
- Upload to the Arduino. Hopefully you don't have any errors!
- Open the Serial Monitor. Go to Tools->Serial Monitor. Be sure your Baud rate (bottom right) is set to 38400. You also might want to turn off the autoscroll for a second. If you see something that says
Initializing I2C devices... Testing device connections... MPU6050 connection successful
then you are successful!!
Interpret the output. This tiny chip provides a wealth of useful information. You should be seeing something like the image to the right. There are 6 columns of data being output by the device. The first 3 are the Acceleration in the X,Y,and Z directions (note: these are non-zero even when my device is sitting on the table because we have acceleration due to gravity acting on the sensor). The units seem to be in yard / square second (wtf?) so to get to m/s^2 just divide by 1760. The next three values are the rotations around each axis. I believe these values are in degrees per second. This is the roll, pitch, and yaw of the craft.
More expensive quadcopters use something called a magnetometer (included in a 9-axis IMU) to give the absolute orientation of the sensor based on the magnetic field of the earth. We can calculate this with our data if the craft is sitting still, but as soon as it starts flying we loose enough information to know the orientation of the craft. This would be super useful to conduct autonomous flight or provide additional stabilization; however,t for a remote-controlled craft it isn't necessary and adds complexity. For an additional $10, sparkfun has a chip that offers 9-axis output.