Recently I bought one tiny arduino-based robot called Ringo by PlumGeek. It is a "fun-bot" because it has many sensor on board. You can built your own behaviour and PlumGeek has made many many function to simplify your life or you could build it your own from scratch.
It has many inputs & outputs such as
RGB NeoPixel LED, IR LED Transmitter, Piezo Sound Element, Ambient Light Sensor, and many more. Each sensors already connected to ATmega 328P (Arduino UNO), some of them connected to I2C port. It also packed with 3 axis Accelerometer, and 3 axis Gyroscope.
Using Freescale MMA8451Q Accelerometer on board, I could play with a simple code to access the accelerometer.
To understand this "Magic Thing", we can easily think about a box in shape of cube with ball inside it.
Each side of a cube box is like pressure sensitive sensor labeled with negative and positive axis. Okay, Let's leave the earth for a moment and bring this box to the outer-space. Now we can eliminate gravity effect and the ball is perfectly center, not touching any side. Imagine that the gap between outer side of the ball and inside the cube is realy-realy small. In this situation the accelerometer output will be zero.
If we move the box suddenly to the left, let's say we move the box with acceleration 1g. The ball will hit the X- side because of the action-reaction term. So the result will be -1g for X-axis.
Accelerometer measured the force that caused by acceleration in opposite direction. Enough time in outer-space, we have to go to our home on earth when Sir Isaac Newton found the gravity effect while observing an apple fell off the tree.
In our home, ball inside the box is accelerated by gravity so that ball sit on Z- side even if the box is not moving.
Now if we can throw the box to the sky so it moves perfectly aline with vertical axis, with acceleration of g, you can make the result zero like in outer-space but for just a moment.
For the real life, this model is not exactly how a accelerometer sensors work. But you can think and solve the accelerometer problems with this kind of model.
Acccelerometer in Ringo board
The sensitivity of this sensor is depand on which mode we are using. In 2g mode the sensitivity is 4096counts/g, 2048 counts/g for 4g mode and 1024 counts/g for 8g mode.
All accelerometer has zero-g level. This is the value that corresponds to deviation of an actual output signal while the sensor is stationary.
as measured, the accelerometer data stored safely and can be calculated as g-factor with below equation
AccelerometerAxis = AcceleroData x Sensitivity
The result will be in g , Let's called it Rx for X-axis accelerometer data, Ry and Rz with corresponding.
If the device is not moving and the force that apply to the sensor is just gravitation, you can easily define the inclination of device relative to the ground each axis with simple calculation.
You can calculated angle between X,Y, and Z axes :