Inertial Measurement Unit (IMU) Use
How an IMU works
An IMU works based on MEMS techonology, at the minimum it collects both angular velocity and acceleration for 6 degrees of freedom(DOF), although there are more advanced ones that include a barometer(if knowing height relative to ground is important) and magnetometers(very useful for accurate gyroscope values). The accelerometer works on the principle of piezo electric effect, the ability of certain materials to generate an electric charge in response to applied mechanical stress.
Here, imagine a cuboidal box, having a small ball inside it, like in the picture above. The walls of this box are made with piezo electric crystals. Whenever you tilt the box, the ball is forced to move in the direction of the inclination, due to gravity. The wall with which the ball collides, creates tiny piezo electric currents. There are totally, three pairs of opposite walls in a cuboid. Each pair corresponds to an axis in 3D space: X, Y and Z axes. Depending on the current produced from the piezo electric walls, we can determine the direction of inclination and its magnitude(citation).
Gyroscopes work on the principle of Coriolis acceleration. Imagine that there is a fork like structure, that is in constant back and forth motion. It is held in place using piezo electric crystals. Whenever, you try to tilt this arrangement, the crystals experience a force in the direction of inclination. This is caused as a result of the inertia of the moving fork. The crystals thus produce a current in consensus with the piezo electric effect, and this current is amplified(citation). The pictures below give an example, but here's a helpful (video) also.
Choosing the right IMU
Selecting the right IMU for your needs is crucial depending on what you plan on putting it through. For example, the IMU used in the Wall-E project was the MPU6050. This IMU was incredibly cheap and had the potential to be powerful, but there are some setbacks which I will go over later. The first decision to be made is how many DOF you will require, for example the MPU6050 only has 6 DOF, which is misleading because you will only be able to truly rely on 5 DOF as the yaw gyroscope angle is unreliable, I will explain why later. If your application requires knowing height, you will definitely want to get an IMU with a barometer. If that yaw gyroscope matters, you are going to have to invest in at least a 9 DOF IMU with a magnetometer to correct the yaw drift. The next decision to make is how fast and accurate do you need your IMU to be? Make sure to take a look at the datasheet of the IMU, for example here is a section from the MPU6050.
The Wall-E project is using the +- 500 degrees/s max range for angular velocity(not that fast), but the benefit is the smaller the max range the more accurate the gyroscope reading will be. This is because the MPU6050 uses a 16 bit Analog to Digital Converter (ADC) so naturally more bits can be used to describe this smaller range. So at +-500 degrees/s range, if the IMU is moving at 1 degree/s the actual value given to you is 65.5. More intuitively look at the acceleration values, knowing the range with 16 bits is +-32768, a 1g value is read as 16384 and 2g value maxes out at 32768. So just realize there is this trade off that must be made e.g. if you are using a rocket it will need a higher max range of angular velocity and will give less accurate readings. Pick the IMU that matches your needs.
The final decision you will need to make on choosing an IMU will be how advanced it is and what kind algorithms are at its disposal to give you sensible values to integrate that 65.5 value and output a meaningful gyroscope angle. The MPU6050 comes with a Digital motion processor(DMP), a chip to handle this process instead of on the Arduino freeing up processing power, which uses interrupts to send the data from the chip periodically to the Arduino when data is ready. Just be aware you know how to access these complex algorithms(sometimes called fancy names such as "sensor/data fusion") otherwise you will have to handle this process yourself, which is what we did for the Wall-E project.
Using the MPU6050
The MPU6050 is not expensive and has the potential to be a great IMU, however we ran into trouble actually obtaining meaningful data from the IMU. So first off, if you wish to try and use the DMP here is the right direction to start(tutorial). Watch the video, it is easy to hook up and get the necessary code from the video or right (here). You will definitely want to refer to the Arduino documentation in case you get stuck (documentation). First, hook up the sensor and upload the I2C scanner sketch to make sure your Arduino is communicating properly with the IMU, the address should be 0x68. After that, the IMU must be calibrated. Upload and run the calibration sketch to find your offset values, this sketch can be found under File, examples, MPU6050 and select the calibration sketch. Write down these values and move into the MPU6050 DMP sketch(You may test and run the raw example sketch if you wish, the values are not turned into meaningful data yet though). Now inside the DMP sketch at the very beginning you are given the option to uncomment different possible sets of data you may wish to use/print to the screen such as quaternion, euler, acceleration, etc. The only output defined should just be the yaw pitch and roll at the beginning. Up to this point everything is straightforward, now make sure your values make sense! Turn the IMU around, read different angles, let the IMU rest and make sure the yaw angle isn't drifting. Note, if your serial monitor is displaying characters and not your pitch, roll and yaw values, make sure to set the baud rate in your monitor to the value the sketch uses which is 115200. Uncomment and test out the teapot demo if you wish, you will need more software to display it which can be found (here). If everything is working, you will want to run additional code(e.g. turn a LED on/off when the IMU is past a certain angle) inside the void loop, inside the while loop where the Arduino is waiting for the MPU interrupt. Note: if you don't this will result in interrupt or fifo errors!
What we ended up doing for the Wall-E project was processing the values on the Arduino instead of using the DMP. Even if you wish to use the DMP, to get a better understanding on what the DMP is actually doing watch this [(video)].