Language: EN


How to use an accelerometer in our Arduino projects

In this post we are going to introduce the use of the accelerometer as a fundamental component in our electronics and Arduino projects. In the next post we will expand to the use of gyroscopes to finally end up presenting the use of IMU’s to calculate orientation, for example, of a vehicle or robot.

In previous posts we have seen different options available to give movement to our projects with Arduino through 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 complicated, we will need to determine the position, speed, and orientation. We have many sensors that will help us determine the position, speed, and orientation, such as magnetic compasses, GPS, barometers, optical and magnetic encoders, or limit switch buttons.

But among this type of sensors, accelerometers, gyroscopes, and IMUs stand out for their importance. In recent years, they have experienced great development and have become small, compact, durable, and inexpensive devices.

This is due in large part to the appearance and improvement of microelectromechanical systems (MEMS), devices that combine mechanical and electronic behaviors on microscopic scales, typically in 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 use it frequently in our home 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 forward direction of a vehicle, or to detect that our robot has fallen.

What is an accelerometer?

As its name suggests, an accelerometer is a device that allows you to measure acceleration.

We briefly remember that acceleration is the variation of speed with respect to time or, expressed mathematically,

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

Likewise, we remember that by Newton’s first law

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

Imagine that we want to manufacture a device that measures acceleration. We could build a sensor consisting of a solid body, inside which we suspend a mass attached by springs to the outer body.


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

This displacement of the free interior mass can be measured to determine the magnitude of the acceleration. The displacement will be proportional to the supported acceleration, and will remain constant while the acceleration is constant.

To build this accelerometer in a MEMS, a micromechanized polysilicon structure built on a silicon wafer is used. The polysilicon springs suspend the structure on 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 part deforms and moves relative to the fixed part, deforming in a similar way to our imaginary device based on springs. The displacement is detected by the variation of the system’s capacitance.

The accelerometers available are usually 3-axis, that is, they are capable of measuring the acceleration to which the sensor is subjected in X, Y, and Z independently, which allows simultaneously knowing the magnitude and direction of the measured acceleration.

The ability to measure the acceleration of a system provides in itself 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 that we can obtain from an accelerometer. The sensor is affected by Earth’s gravity, which represents an acceleration of approximately 9.81 m/s^2 on the surface of the Earth, which is of course constantly recorded by the sensor.

A first consequence is that we can detect conditions of free fall, for example, when the sensor falls off a table, or a robot loses 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 interestingly, the measurement of gravity can be used to determine the orientation of the sensor. Since we can record the acceleration in three axes, in the absence of other accelerations, we can determine the orientation of the sensor with a little trigonometry.

In the case of 2D, assuming that 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 3D case, the equations are,


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

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

As we can see, an accelerometer is a very versatile device, which allows us to obtain measurements of acceleration and orientation. In addition, they are absolute sensors, meaning that there is 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 medium or long-term measurement.

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

In the next post, we will address the use of gyroscopes, devices that allow us to measure the angular velocity of a sensor, and that complement very well the limitations in the calculation of orientation, something that we will see in the last post about IMUs.

Download the code

All the code from this post is available for download on Github. github-full