Stacked from 12 images. Method=B (R=44,S=4)
In today's world of technology and electronics, MEMS (Micro-Electro-Mechanical Systems) sensors have become a key tool for all kinds of projects, from robotics to home automation and wearables. Modules that combine accelerometers and gyroscopes, such as the MPU6050 and the LSM9DS1, are two of the most popular options thanks to their versatility, low cost, and ease of integration with microcontrollers like Arduino and other platforms. A thorough understanding of their operation, unique features, differences, and even the best way to leverage them is essential for designing precise systems that measure movement, orientation and inclination.
In this article we will take you step by step through everything you need to know about sensors MPU6050 y LSM9DS1: how they work, what applications they have, how to integrate them into your project, calibrate them, interpret their readings correctly and make the most of their capabilities, combining the information gathered in the best tutorials and technical articles, under a practical and updated vision with close language, so that achieve professional results in your developments.
What is a MEMS sensor and how does it work?

Before going into the specific models MPU6050 and LSM9DS1, it is important to be clear about the concept of MEMS sensor. These devices, also called Micro Electro Mechanical Systems, integrate microscopic mechanical components and electronic circuits into a single chip, so that they can detect physical variations—such as accelerations, rotations, or vibrations—and convert them into electrical signals that can be interpreted by digital systems.
In the case of MEMS accelerometers and gyroscopes, their operation is based on principles such as:
- Newton's law of acceleration (a = F/m), using internal structures acting as microscopic masses and springs.
- The Coriolis effect is used to detect angular movements, taking advantage of the deflection that small masses experience when rotating within the chip.
- Internal ADC converters to transform physical variations into high-resolution digital values (usually 16 bits).
These capabilities make MEMS extremely useful in applications that require measurement of orientation, inclination or movement in three dimensions, such as navigation systems, camera stabilization, smart watches, drones, robots, and much more.
Main features of the MPU6050
El MPU6050 It is probably the most widely used MEMS motion sensor among makers, engineers and hobbyists looking for an economical and reliable solution for measuring acceleration and rotation in three axes.
Its key technical specifications include:
- 3-axis accelerometer: Capable of detecting accelerations on the X, Y and Z axes, with a programmable range of ±2g, ±4g, ±8g and ±16g.
- 3 axis gyro: Measures angular velocities on all three axes, with adjustable sensitivity at ±250, ±500, ±1000 and ±2000 degrees per second.
- Digital motion processor (DMP): Incorporates an internal microprocessor dedicated to performing complex calculations Motion Fusion (sensor fusion), calculating data such as quaternions, Euler angles and rotation matrices without having to load those calculations onto the main microcontroller.
- Digital output via I2C: Communications via I2C bus with two possible addresses (configurable via pin AD0 to 0x68 or 0x69), allowing operation with most Arduino, ESP and similar boards.
- 16-bit ADC converter: It offers high resolution in data collection.
- Integrated temperature sensor
- Possibility of expanding with an external magnetometer: Via the I2C auxiliary bus, the MPU6050 can read other connected sensors such as the popular HMC5883L (magnetometer), to form a complete 9-axis IMU.
- Flexible operating voltage: It can be powered at 3,3V or even 5V if a motherboard like the GY-521 is used, which incorporates a regulator.
Furthermore, the module's compact size (around 25 x 15 mm) and the fact that it comes ready for integration into a breadboard make it ideal for both testing and final development.
What is the LSM9DS1 and how is it different?
In turn, the LSM9DS1 It is a more advanced and modern option within the MEMS IMU family, although it is less popular than the MPU6050 in beginner projects. It integrates the following on a single chip:
- Un 3-axis accelerometer
- Un Gyroscope of 3 shafts
- Un magnetometer also 3-axis
This means that the LSM9DS1 is a 9 DoF (Degrees of Freedom) IMU, allowing you to measure acceleration, angular velocity and Earth's magnetic field in three dimensions, providing complete and accurate readings of absolute position and orientation with respect to Earth.
Its main advantages over the MPU6050 include:
- Combines the three sensors into a single physical chip, saving space and simplifying connections.
- You can communicate both through I2C as SPI, which gives it greater versatility for different platforms.
- The ranges and sensitivities of each sensor (accelerometer, gyroscope, magnetometer) are more flexibly configurable.
- It has advanced digital filtering and event detection options.
The LSM9DS1 is often chosen for projects where absolute orientation is required (e.g., compasses, navigation systems, or flight stabilization) without the need for additional external sensors.
Operating principles of MEMS accelerometers and gyroscopes
To truly understand how these MEMS modules work, it is important to understand the physical concepts and how they translate into digital data:
Accelerometer
Un MEMS accelerometer measures the acceleration of an object (change in speed over time) relative to the three axes of space. Internally, it is based on the presence of a microscopic suspended mass by flexible anchors or tiny springs. When the sensor accelerates, this mass shifts slightly, and this variation is converted into an electrical signal using variable or piezoelectric capacitors.
- The accelerometer always detects at least one acceleration: the gravity (9,81 m/s2), even if the sensor is still.
This is used to calculate the inclination with respect to the horizontal plane. - By integrating the acceleration with respect to time, the velocity and, in turn, the position traveled can be obtained, although these operations tend to accumulate errors.
Gyroscope
El MEMS gyroscope use the Coriolis effect to detect the speed at which a body rotates around its X, Y and Z axes. When the sensor experiences a rotation, internal vibrating masses suffer a deflection proportional to the angular velocity, and that change is measured electronically.
- The gyroscope measures angular velocity: how quickly the sensor's orientation changes on each axis.
- Integrating the angular velocity with time gives the angle of rotation (angular position), although this operation generates cumulative errors called drift.
Why combine accelerometer and gyroscope?
On their own, both accelerometers and gyroscopes have limitations when determining the orientation of an object:
- Accelerometer: Accurate in detecting inclinations relative to the vertical axis (using gravity), but very sensitive to sudden movement, external accelerations or vibrations.
- Gyroscope: It is ideal for measuring rapid changes in orientation, but suffers from error accumulation if its output is integrated over a long period of time.
Therefore, most applications merge data from both sensors, which greatly improves the accuracy and reliability of readings. angle, inclination or positionTo achieve this, they are used digital processing filters such as the Complementary filter or the Kalman filter, which combine and weigh the advantages of each sensor.
Getting started with the MPU6050: connection and libraries
Typical connection diagram
The MPU6050 It is usually mounted on a plate type GY-521, which greatly facilitates integration with microcontrollers such as Arduino.
The basic connections to use the module in I2C mode are usually:
| MPU6050 | Arduino Uno/Nano/Mini | Arduino Mega/DUE | Arduino Leonardo |
|---|---|---|---|
| VCC | 5V | 5V | 5V |
| GND | GND | GND | GND |
| SCL | A5 | 21 | 3 |
| SDA | A4 | 20 | 2 |
The module has built-in pull-up resistors, so it is generally not necessary to add them externally.
I2C address and AD0 pin
The MPU6050 allows you to configure its I2C address to 0 x 68 (by default, when the AD0 pin is at GND or unconnected) or 0 x 69 (when AD0 is connected to high/5V). This makes it easy to use multiple sensors on the same bus.
Recommended library: I2Cdevlib by Jeff Rowberg
To work comfortably with the MPU6050 on Arduino, the community recommends using the following libraries:
- I2Cdev: Facilitates I2C communication with many sensors.
- MPU6050: Allows you to access all sensor functions, read calibrated values, offsets, and use the DMP.
They are available in: https://github.com/jrowberg/i2cdevlib
Once downloaded, simply unzip them and place them in the folder libraries from the Arduino IDE.
Reading basic data: acceleration and angular velocity
Once the MPU6050 is connected and configured, the next step is to perform readings of accelerations and angular velocities on the three axes. The basic process, using the aforementioned library, includes:
- Initialize the sensor using the function sensor.initialize().
- Check the connection with sensor.testConnection().
- Read RAW (unprocessed) values from the accelerometer and gyroscope into variables like ax, ay, az for acceleration and gx, gy, gz for spin.
- Send the data to the serial port to display the results.
This data appears as 16-bit integers in the range .
Calibrating the MPU6050 sensor
One of the key phases when using the MPU6050 is the calibrationIt is very common for the sensor to return non-zero values, even if it is perfectly horizontal and at rest, due to possible misalignments when soldering the chip onto the module, or even minor manufacturing imperfections.
Calibrating the sensor involves determining the accelerometer and gyroscope offsets on each axis and configure them on the sensor so that the readings are based on the correct information. A typical process might consist of:
- Read the current offsets using functions like getXAccelOffset(), getYAccelOffset(), etc.
- Place the sensor in a horizontal position and completely still.
- Using a program, adjust the offsets until the filtered readings (for example, using a moving average or low-pass filter) converge to the ideal values: ax = 0, ay = 0, az = 16384, gx = 0, gy = 0, gz = 0 in raw mode (RAW).
- Set these values with the functions setXAccelOffset(), setYAccelOffset(), etc.
Once properly calibrated, the sensor will provide much more accurate and stable values, essential for critical applications such as stabilization or navigation.
Scaling and converting readings to physical units
The raw readings from the MPU6050 must be transformed into SI (International System) units in order to be interpreted and used in physical calculations or data visualization:
- Acceleration: The default range is ±2g, which is equivalent to ±19,62 m/s2A RAW value of 16384 corresponds to 1g; therefore, to convert to x am/s2: ax * (9,81/16384.0).
- Angular velocity: By default, ±250°/s, so the conversion would be: gx * (250.0 / 32768.0) to convert from RAW values to degrees per second.
These scale factors change if you configure the sensor to other ranges, so it's essential to always check the factory or custom settings before interpreting the data.
Calculate tilt using only the accelerometer
When the sensor is at rest or only under the effect of gravity, the accelerometer readings can be used to calculate the angle of inclination with respect to the X and Y axesTypical mathematical formulas use trigonometric functions:
- For X-inclination: atan(ax / sqrt(ay² + az²)) × 180/π
- For Y-slant: atan(ay / sqrt(ax² + az²)) × 180/π
This provides the angle of inclination with respect to each axis relative to the plane of gravity, although if the sensor is in motion or receives other accelerations, these values may be altered.
Calculating rotation angles with the gyroscope
The gyroscope allows to calculate the variation of angle by integration of angular velocity over time. Mathematically:
- The angle is equal to the integral of the angular velocity in a given time interval: θ = θ0 + ∫w·dt
In practice, these calculations can be performed in program loops, summing the angular velocity multiplied by the sampling period (dt) to obtain the accumulated angle.
It is important to control the integration error, since small errors accumulate, causing drift.
Sensor fusion filters: Complementary and Kalman
To reduce interpretation errors and make the most of each sensor, data fusion algorithms are used:
Complementary Filter
This filter combines the angle estimated by the gyroscope (which works well in the short term) with the angle calculated by the accelerometer (which is more reliable in the long term but noisy). The typical formula is:
Final_angle = α × (Previous_angle + Angular_velocity×dt) + (1-α) × Accelerometer_angle
Where α is usually between 0,95 and 0,99. It allows to obtain a stable reading and reduce the drift.
Kalman filter
Much more advanced, this filter fuses measurements, taking into account the uncertainty of each measurement and their correlations, achieving accurate estimates in the presence of noise. It is widely used in navigation systems and advanced robotics, although it requires greater computational power.
3D simulation and orientation visualization (Yaw, Pitch, Roll)
An interesting application is the real-time display of 3D orientation of an object, such as a drone or robot, by representing the angles Yaw, Pitch and Roll.
This is achieved by transmitting the processed data to graphics software, using tools like Serial Plotter or specific 3D programs to monitor and analyze movements. This way, you can visually understand how your system is oriented in space.
Extended readings: use of magnetometer and LSM9DS1 sensor
El LSM9DS1 integrates an accelerometer, gyroscope and magnetometer in a single chip, allowing data to be obtained from absolute position and orientationIn addition to measuring acceleration and rotation, it can detect the Earth's magnetic field to:
- Calculate the absolute azimuth, useful in navigation and digital compasses.
- Develop guidance systems without the need for additional external sensors.
- Fuse data from all sensors for highly accurate position and orientation estimation (9-DoF).
Practical tips for effective use of MPU6050 and LSM9DS1
- Always calibrate the sensors before use in critical applications to improve accuracy.
- Avoid mounting modules near sources of electromagnetic interference, such as motors or magnets.
- Use filtering techniques and maintain precise control of sampling times.
- For absolute orientation with respect to the north, it is recommended to use a LSM9DS1 or combine the MPU6050 with an external magnetometer, such as the HMC5883L.
- Implementing real-time visualizations helps to better interpret the collected data.
- Bookstores like i2cdevlib They make work much simpler, so prioritize them to make development easier.