como-usar-un-acelerometro-arduino

How to use an accelerometer in our Arduino projects

  • 6 min

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 on using gyroscopes to finally end by presenting the use of IMUs to calculate orientation, for example, of a vehicle or robot.

In previous posts, we have seen different options available to provide 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 provide movement to our project, and as these become more complex, we will need to determine the position, velocity, and orientation. We have many sensors that will help us determine position, velocity, and orientation, such as magnetic compasses, GPS, barometers, optical and magnetic encoders, or limit switches.

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 cheap devices.

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

arduino-mens

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

We will frequently use it in our homemade electronics and robotics projects with Arduino, for example, to measure the orientation of a gripper on a robotic arm or a sensor turret, the direction of travel 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 recall that acceleration is the change in velocity with respect to time or, expressed mathematically,

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

Likewise, we recall that by Newton’s first law

That is, any body with a mass m requires a certain force to change its velocity. 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 we want to build a device to measure acceleration. We could build a sensor consisting of a solid body, inside which we suspend a mass attached by springs to the outer body.

arduino-acelerometro-funcionamiento

When applying an 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 acceleration sustained and will remain constant as long as the acceleration is constant.

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

arduino-acelerometro

When subjecting the device to accelerations, the interior part deforms and displaces relative to the fixed part, in a way similar to our imaginary spring-based device. The displacement is detected by the variation in the system’s capacitance.

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

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

But it’s not the only function we can get from an accelerometer. The sensor is affected by Earth’s gravity, which represents an acceleration of approximately 9.81 m/s^2 on the Earth’s surface, which of course is 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 interestingly, 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 sensor’s orientation with a bit of trigonometry.

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

arduino-acelerometro-orientacion-inclinacion

Similarly, in the 3D case the equations become,

arduino-acelerometro-orientacion

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

As we frequently mention, this type of time-integrated measurements accumulate measurement errors and noise, which leads to drift effects. It should always be the last resort and never be considered beyond a rough estimate.

As we see, an accelerometer is a very versatile device that allows us to obtain measurements of accelerations and orientation. Furthermore, they are absolute sensors, meaning 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 measurement in the medium or long term.

As negative aspects, they are devices very sensitive to vibrations, so the measurement will have 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 enough.

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