I recently bought a combination 3 axis accelerometer and 2 axis gyroscope from Sparkfun and I wanted to post some of the code I used to get it up and running.  I'm planning on using it in an Arduino based MIDI controller that I'll be posting soon (video below, code at very bottom of page), I want to use the analog controls from the tilt and rotational movement to control the outgoing MIDI.  Accelerometers and gyroscopes are also used frequently on robots for balancing and other types of feedback.

First a bit of basic information:
Gyroscopes measure rotational movement in degrees per second.  They will not directly tell you information about tilt, only movement about an axis.  Accelerometers measure acceleration, you can easily use this information to calculate the tilt of an object by subtracting the current accelerometer data from a value that you know to be zero tilt.

For this project I used the 5 Degrees of Freedom IDG500/ADXL335 board from Sparkfun.  I like this board because it outputs the accelerometer and gyroscope data as 5 analog voltages which can easily be read using five of the Arduino's analog input pins.  Some accelerometers and gyroscopes output data digitally by encoding it in a pulse-width modulated (PWM) signal.  If you are short on analog input pins on your Arduino, it may be a good idea to use one of these digital chips, but the analog chips are nice because they require minimal effort to get working.

Parts list:
IMU Analog Combo Board - 5 Degrees of Freedom IDG500/ADXL335 Sparkfun SEN-11072
Arduino Uno Sparkfun DEV-11021
male header pins Sparkfun PRT-00116
22 gauge wire


Solder header pins to the accelerometer/gyro board.  On a breadboard connect Arduino 5V to "RAW" pin, Arduino ground to ground, then connect the pin labeled X-Acc to A0, Y-Acc to A1, and Z-Acc to A2

The code below will display the X,Y, and Z acceleration fro your board int he serial monitor (ctrl/command + shift + M).  I tried to center the values around a zero point by introducing the variable "zero" in my code and then subtracting all my incoming data from this number.  This way when the board is held parallel to the ground, the X and Y acceleration both read 0.  When the board is tipped to positive X it will display a positive number, and when it is tipped to negative X it will display a negative number (same goes for Y).  This accelerometer code allows you to determine the orientation of your board in 3D space.

Sometimes you may want to use a gyroscope to get rotational information about your board that cannot be calculated with an accelerometer.  On a breadboard connect the pin labeled Vref to A0, yRate to A1, xRate to A2, y4.5 to A3, and x4.5 to A4.
You may be wondering about the difference between the X/YRate and X/Y4.5 outputs.  Gyroscopes measure degrees per second, this gyroscope outputs a voltage based on the degrees of movement per second so we can say that it is outputting volts per degrees per second (V/°/s).  From the datasheet of the gyroscope we can see that the X/YRate is outputting data with 2.0mV/°/s sensitivity and the X/Y4.5 is outputting data with about 4.5 times the sensitivity, or 9.1mV/°/s.
Upload the following code to display the output from each of these (fig 6).

I wanted to zero this code as I did with the accelerometer code above. I was expecting the outputs from the x and y gyro to equal the output from Vref while the device is held stationary, but as you can see in fig 6, this is not the case (if anyone has an explanation for this I'd love to hear it!). Since Vref was not a god reference for me to zero my output to, I used some preset values based on the data from fig 6, you can find them in my print statements in the code below. I also removed the monitoring of Vref since it appeared to be stable no matter how much I moved the device, this also saved me an analog input pin on the Arduino.

The results from this code are shown in figure 7.  Rotation around the x and y axes will change the values of xRate, x4.5, yRate, and y4.5.
I used this accelerometer/gyro in a Arduino-based MIDI controller I've been working on. Below is the code for the two applications I demoed in the video at the top of this instructable.  I connected all three axes of the accelerometer and the two 4.5gyro outputs for this project.

single pixel moving around, triggering MIDI (only uses x and y accelerometer):

four pixels bouncing (only uses x accelerometer, uses x gyro to clear pixels):

<p>Thanks for sharing!</p><p>However that's still kinda large for my project</p><p>I just purchased an integrated Bluetooth/Gyro/ACC board from here:<a href="http://www.aliexpress.com/item/nrf51822-MPU6050-6pixs/32456095618.html?ws_ab_test=201407_1%2C201444_6_3_2_1_5_4%2C201409_1&spm=2114.01020208.3.2.MJWube" rel="nofollow">http://www.aliexpress.com/item/nrf51822-MPU6050-6pixs/32456095618.html?ws_ab_test=201407_1%2C201444_6_3_2_1_5_4%2C201409_1&amp;spm=2114.01020208.3.2.MJWube</a></p><p>Anyone ever used this before?</p>
<p>Amanda , </p><p>I am trying to learn more about accelerometers and your code should help .</p><p>Thanks again for sharing!</p><p>Build_it_Bob</p>
<p>Hi, very nice project!!</p><p>I`m trying to build it to use it with contemporany dance...but I`m really new on this and having troubles...</p><p>Also, i`m not using the same ACC/GYRO you`re using. I`m using the &quot;GY-61 - Analog acceleration sensor module- ADXL335&quot;</p><p>Could help me? Im trying to send the midi info but can`t make it work. Mine doesn`t have X, Y, Z Acc...(it did work with the test code)</p><p>Thanks for posting here your project, very inspiring!!</p>
<p>Thank you for sharing your project.</p><p>Is there a download link (project file) ?</p>
<p>there is a download pdf option for pro users, otherwise you can try printing out the page, or copying the code into a text editor.</p>
<p>OK. Thanks</p>
<p>Hello. Very nice and impressive project!</p><p>I would like to ask you a question about accelerators.</p><p>Suppose <br> everything is connected (accelerometer to arduino). I just turned on <br>the system but did not move at all the accelerometer. In that case do i <br>have a reading? Can I calculate initial unknown tilt without moving the <br>accelerometer?</p><p>This is important to me because i am thinking to <br>make a controller for adjusting a hospital bed in predifined tilts. So <br>in case of power down not to have to move the patient just to find out <br>in which position the bed is. </p><p>Could you help me out?</p>
<p>yes, the accelerometer reads the acceleration due to gravity, so you will get a tilt reading if you do not move the device. Gyroscopes, by contrast, must be moved to generate a reading.</p>
You made my day a lot better.<br>Thank you for the reply it's been awesomely helpful!!
<p>no problem! post a pic of your project when you're done, sounds cool!</p>
Hello Amanda,<br> <br> Awesome project! I've been hacking at this to make it compatible with a MPU-6050 combo acc/gyro chip over i2c. I've got it working but sometimes there is a lag on startup (or it doesn't start at all) and if you let it sit without activity it can freeze up. Sometimes it will come back to life spontaneously. I'm new to interrupts, ports and the like on Arduino, so if anyone sees anything that would tighten up this code I'd love to hear it! I hope this will also make the project more accessible for those who don't have the discontinued chip in the example.<br> <br> I've also been hacking at this to try and work with a 4x8 grid. I think I have the shift registers daisy chained properly, but the code to get this working has eluded me so far. If there is an easy solution, please post!<br> <br> You will need the i2cdev library to make this work. Download here: https://github.com/jrowberg/i2cdevlib<br> <br> The library supports other devices as well, so if you're not using the MPU-6050 you might find what you are using there.<br> <br> You can see my revised sketch here: https://gist.github.com/4422201<br> <br> Cheers!
<p>been a long time since i looked back at this project, sounds great! have you made any more progress?</p>
That's so cool! Reminds me of the old Lights Out game by tiger toys.

About This Instructable


111 favorites


Bio: I'm a grad student at the Center for Bits and Atoms at MIT Media Lab. Before that I worked at Instructables, writing code for ... More »
More by amandaghassaei: OTCA Metapixel - Conway's Game of Life "9 Degrees of Freedom" IMU Twitter Controlled Pet Feeder
Add instructable to: