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.