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.