How to use an Accelerometer with Arduino

  • The MPU6050 is a sensor that combines an accelerometer and gyroscope, with a DMP processor that optimizes performance.
  • The MPU6050 can measure motion on 3 axes and communicates with Arduino using I2C or SPI, making it ideal for measuring acceleration and rotation.
  • The ADXL345 is another popular accelerometer that features interrupt pins and is highly accurate with its 13-bit resolution.

accelerometer

The use of sensors is fundamental in many advanced electronics projects. If you are into Arduino, you have probably heard of the accelerometer, a device that measures variations in acceleration, and the gyroscope, which allows you to work with angular measurements. Both allow you to capture movement and orientation in three-dimensional axes, which opens up a universe of interesting possibilities.

In this article we will explain in depth what an accelerometer is and how you can integrate it into your Arduino projects. We will describe different modules such as the MPU6050, the ADXL345 and others like them, detailing how to get the most out of them. In addition, we offer you material on how to connect these sensors and code examples that you can use for your projects.

What is an accelerometer?

accelerometer

An accelerometer is a sensor that measures the acceleration of an object in one or more axes. This includes both acceleration due to motion and acceleration due to gravity. Acceleration is measured in meters per second squared (m/s²), and by taking into account its three axes (X, Y, and Z), we can tell in which direction and with what force the object is moving.

Accelerometers are essential for applications that require motion, tilt or vibration detection. For example, they are used in smartphones to detect screen orientation, camera stabilization, fall detection in security devices and video games.

In Arduino projects, the accelerometer offers a wealth of data that you can turn into actions such as controlling the orientation of a robot or triggering alarms when certain acceleration limits are exceeded. In addition, many modules such as the MPU6050 and ADXL345 They also include gyroscopes, allowing for additional data on angular velocity and tilt.

The MPU6050 accelerometer

El MPU6050 It is one of the most widely used modules due to its great versatility. This device combines both a three-axis accelerometer and a three-axis gyroscope in a single chip, providing a total of six degrees of freedom (6DOF). In addition, it includes a Digital Motion Processor (DMP) that allows the readings from both sensors to be combined, optimizing the performance of the Arduino by avoiding complex calculations within the microcontroller.

The accelerometer's measuring range allows for an adjustable range between ±2g, ±4g, ±8g and ±16g. This gives it great precision, as it has 16-bit converters that allow it to capture small variations in each axis. Its internal gyroscope also allows for the measurement of rotations with an adjustable range between ±250°/s and ±2000°/s.

Communication is simple thanks to the I2C or SPI protocol, allowing you to efficiently connect the MPU6050 to an Arduino and get the data quickly through the data bus. Its low power consumption (about 3,5mA on average) makes it ideal for projects where power usage must be optimized.

MPU6050 connection example

The connection between an MPU6050 module and Arduino is quite simple, thanks to the I2C standard that this sensor uses.

MPU6050 Arduino Uno (or similar)
VCC 5V
GND GND
SCL A5
SDA A4

Once these connections are established, you can upload some code to the Arduino IDE that will allow you to read data from the accelerometer and gyroscope in real time.

Reading data with Arduino

To get the readings from the MPU6050, you can use the libraries MPU6050 y Wire, which facilitate communication between the sensor and the Arduino board. Below we show a simple example of how to read accelerations and rotations and display them through the serial port:

#include 
#include 

MPU6050 accelGyro;
int ax, ay, az;
int gx, gy, gz;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  accelGyro.initialize();

  if (accelGyro.testConnection()) {
    Serial.println("Sensor conectado correctamente");
  } else {
    Serial.println("Error al conectar el sensor");
  }
}

void loop() {
  accelGyro.getAcceleration(&ax, &ay, &az);
  accelGyro.getRotation(&gx, &gy, &gz);
  Serial.print("Accel:	");
  Serial.print(ax); Serial.print("	");
  Serial.print(ay); Serial.print("	");
  Serial.print(az); Serial.print("	");
  Serial.print("Gyro:	");
  Serial.print(gx); Serial.print("	");
  Serial.print(gy); Serial.print("	");
  Serial.println(gz);
  delay(100);
}

Accelerometer calibration

Once connected and receiving data, it is important to calibrate the sensor to obtain accurate readings. This involves compensating for any errors due to sensor tilt or small variations in the electronic components.

To calibrate your MPU6050 module, you can adjust the values ​​of offset both the accelerometer and the gyroscope. These offsets will allow you to correct the readings and make them more accurately reflect the real values. Here is an example where the offsets are automatically adjusted:

void calibrateMPU6050() {
  int16_t ax, ay, az, gx, gy, gz;
  int ax_offset = 0, ay_offset = 0, az_offset = 0;
  int gx_offset = 0, gy_offset = 0, gz_offset = 0;
  // Inicia con valores de ejemplo...
  for(int i=0; i<100; i++) {
    mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    ax_offset += ax; ay_offset += ay; az_offset += az;
    gx_offset += gx; gy_offset += gy; gz_offset += gz;
    delay(100);
  }

This code is just a snippet to illustrate how to adjust offsets from multiple repeated reads.

ADXL345 Accelerometer

El ADXL345 is a 3-axis capacitive accelerometer that is also very popular in the Arduino community. Like the MPU6050, this sensor is low-power and features a FIFO memory block to store up to 32 sets of measurements from all three axes.

One of its strengths is that it allows you to choose between several measurement ranges, from ±2g to ±16g, with a resolution of up to 13 bits. It also has two interrupt pins that you can configure to detect specific events such as sudden movements or free falls.

Using the ADXL345 with Arduino

Connecting the ADXL345 to an Arduino is also fairly straightforward. Here's how to connect the sensor using the I2C bus:

ADXL345 Arduino Uno (or similar)
Vcc 5V
GND GND
SDA A4
SCL A5

Once you are ready, you can use the library SparkFun_ADXL345 to launch your accelerometer readings quickly and efficiently. Below is some code on how to perform these readings:

#include 
ADXL345 adxl;

void setup() {
  Serial.begin(9600);
  adxl.powerOn();
  adxl.setRangeSetting(8); // Selecciona el rango de ±8g
}

void loop() {
  int x, y, z;
  adxl.readAccel(&x, &y, &z);
  Serial.print("X:"); Serial.print(x);
  Serial.print(" Y:"); Serial.print(y);
  Serial.print(" Z:"); Serial.println(z);
  delay(500);
}

Accelerometer Applications

Accelerometers have a wide variety of applications. As well as being used in Arduino hobby projects, they are essential in electronic devices such as mobile phones, cameras and navigation systems. Common applications include:

  • Motion detection in video games and controllers.
  • Camera stabilization to avoid vibrations.
  • Safety devices to detect falls or sudden movements.
  • Security alarm in vehicles or access systems.

Whether you want to build your own robot, develop a gesture-based control system, or simply learn more about sensors, the accelerometer is one of the best options to start with. Through practical examples and proper calibration, you can get accurate readings that will allow you to make real-time decisions in your projects.


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.