# How to use an accelerometer in our Arduino projects

In this entry, we will introduce the use of the accelerometer as a fundamental component in our electronics and Arduino projects. In the next entry, we will expand on the use of gyroscopes and finally present the use of IMUs to calculate orientation, for example, of a vehicle or robot.

In previous entries, we have seen different options available to give movement to our Arduino projects using rotary motors or linear actuators. We have also analyzed the factors to consider when choosing one or the other for our project.

If we are going to give movement to our project, and as they become more complex, we will need to determine position, speed, and orientation. We have many sensors that will help us determine position, speed, and orientation, such as magnetic compasses, GPS, barometers, optical and magnetic encoders, or limit switches.

But among these types of sensors, accelerometers, gyroscopes, and IMUs stand out for their importance. In recent years, they have undergone significant development and have become small, compact, durable, and inexpensive devices.

This is largely due to the emergence and improvement of microelectromechanical systems (MEMS, Microelectromechanical Systems), devices that integrate mechanical and electronic behaviors at microscopic scales (typically on the order of nanometers).

Many everyday devices have one of these sensors. For example, mobile phones and tablets use them to detect the orientation of the screen.

We will frequently use it in our homemade electronics and robotics projects with Arduino, for example, to measure the orientation of a gripper in a robotic arm or a turret with sensors, the direction of movement of a vehicle, or to detect that our robot has fallen.

## What is an accelerometer?

As its name indicates, an accelerometer is a device that allows measuring the acceleration to which it is subjected.

Let’s briefly remember that acceleration is the change in velocity over time or, mathematically expressed,

The units for acceleration in the international system are m/s^2.

Also, we recall that by Newton’s first law

That is, any body with mass m requires a certain force to change its velocity. Equivalently, any body subjected to acceleration will experience a certain force. We will use this fact to understand how the accelerometer works.

Imagine we want to create a device that measures acceleration. We could build a sensor formed by a solid body, inside which we suspend a mass held by springs to the outer body.

When an acceleration is applied to the assembly, the suspended mass will exert a force on the springs, causing one to contract and the other to elongate, meaning the relative position of the mass inside the sensor will vary.

This displacement of the inner free mass can be measured to determine the magnitude of the acceleration. The displacement will be proportional to the supported acceleration and will remain constant as long as the acceleration is constant.

To build this accelerometer in a MEMS, a micromachined polysilicon structure is used, constructed on a silicon wafer. The polysilicon springs suspend the structure above the silicon wafer while providing resistance to withstand the accelerations to which the sensor will be subjected.

When the device is subjected to accelerations, the interior deforms and moves relative to the fixed part, deforming similarly to our imaginary device based on springs. The displacement is detected by the variation in the capacitance of the system.

Available accelerometers are typically 3-axis, meaning they can measure the acceleration to which the sensor is subjected in X, Y, and Z independently, allowing simultaneous knowledge of the magnitude and direction of the measured acceleration.

The ability to measure the acceleration of a system provides interesting functionalities, such as recording vibrations or impacts. For example, pedometers use an accelerometer to detect the steps we take.

But it is not the only function we can obtain from an accelerometer. The sensor is affected by Earth’s gravity, which implies an acceleration of approximately 9.81 m/s^2 at the Earth’s surface, which is, of course, constantly recorded by the sensor.

A first consequence is that we can detect free fall conditions, for example, when the sensor falls from a table, or a robot loses its balance and falls to the ground. We simply need to check that the resulting measured acceleration is less than 9.81 m/s^2.

The object could be falling while being subjected to another acceleration, in which case it would be much more difficult to determine the fall.

Even more interesting, the measurement of gravity can be used to determine the orientation of the sensor. Since we can record acceleration in three axes, in the absence of other accelerations, we can determine the orientation of the sensor with a bit of trigonometry.

In the case of 2D, assuming we place the sensor horizontally in its X-Y plane, with Z pointing upwards, and only rotate around the Y axis, the equation for the angle is,

Similarly, in the case of 3D, the resulting equations are,

However, an accelerometer is not a suitable sensor for trying to determine the velocity of a system, much less its position. We could be tempted to estimate the velocity by simply multiplying acceleration by time.

As we often mention, these types of measurements integrated over time accumulate measurement errors and noise, which leads to drift effects. It should always be a last resort and never considered beyond a rough estimate.

As we can see, an accelerometer is a very versatile device that allows us to obtain measurements of acceleration and orientation. Additionally, they are absolute sensors, meaning there exists a 0,0,0 measurement point, which would correspond to the sensor falling freely in a vacuum. Finally, the measurement is not integrated, so they do not present drift in the measurement over the medium or long term.

As negative aspects, they are very sensitive devices to vibrations, so the measurement will present high-frequency noise. In general, we will need to filter the signal before we can use it. In the simplest case, simply averaging several measurements will be sufficient.

In the next entry, we will address the use of gyroscopes, devices that allow measuring the angular velocity of a sensor and complement the limitations in the calculation of orientation, something we will see in the final entry about IMUs.