Get Started with MEMS-Based Motion and Orientation Sensor Design Using Arduino Breakout Boards
Contributed By Digi-Key's North American Editors
Designers increasingly need to provide their systems with orientation and motion capability. Fortunately, sensors based on solid-state (semiconductor) and microelectromechanical systems (MEMS) technologies have become available to help them do so. Their small size and low cost allow motion and orientation sensing to be deployed in a wide range of systems, including drones, robots, and of course handheld products like smartphones and tablet computers. These sensors are also used in predictive maintenance systems for the Industrial Internet of Things (IIoT), providing data for analysis using artificial intelligence (AI) and machine learning (ML) at the edge.
The main types of MEMS sensors used to detect motion and orientation are accelerometers, gyroscopes, magnetometers, and various combinations. While many designers are interested in incorporating motion and orientation sensors into their designs, they are often unsure where to start.
One option is to use evaluation and development kits provided by MEMS sensor vendors to support their solutions. Assuming there’s a good degree of support, this is a perfectly fine approach. However, it does require the designer to either restrict themselves to using only the sensors from a single vendor, or to learn the software tools from multiple sensor vendors.
Alternatively, designers who are not used to working with motion and orientation sensors may benefit from experimenting and prototyping using low-cost open source microcontroller development boards from Arduino, along with its single integrated development environment (IDE), in conjunction with low-cost open source sensor breakout boards (BOBs) that feature sensors from multiple vendors.
To help designers get started, this article provides a glossary of sensor terminology and a brief discussion of the role of motion and orientation sensors. It then introduces a selection of such sensor BOBs from Adafruit and their uses.
Glossary of sensor terminology
Two terms that are commonly used when referring to motion and orientation sensors are “number of axes” and “degrees of freedom” (DOF). Unfortunately, these terms are often used synonymously, which can lead to confusion.
Generally speaking, the term axis (plural axes) can be used to describe the dimensionality of the data being employed by a system. In the context of motion and orientation, there are three axes of interest, X, Y, and Z.
The way in which these axes are visualized depends on the system in question. In the case of a smartphone in a portrait orientation, for example, the X axis is horizontal with regard to the screen and points to the right, the Y axis is vertical with regard to the screen and points upwards, and the Z axis, which is perpendicular to the other two axes, is considered to point out of the screen (Figure 1).
Figure 1: A physical system can only ever have a maximum of six DOF because there are only six ways in which it can move in 3D space: three linear and three angular. (Image source: Max Maxfield)
With regard to a device like a smartphone, there are two types of motion of interest: linear and angular. In the case of linear motion, the system may move from side to side on the X axis, up and down on the Y axis, and forward and backward on the Z axis. In the case of angular motion, the system may rotate around one or more of the three axes.
In the context of motion, the DOF refers to any of the directions in which independent motion can occur. On this basis, a physical system can only ever have a maximum of six DOF (6DOF) because there are only six ways in which it can move in 3D space (three linear and three angular).
The term “orientation” refers to the physical position or direction of something relative to something else. In the case of a smartphone, the orientation determines whether the phone is lying flat on its back, standing up on one edge (either in portrait or landscape mode), or somewhere in between.
One way to look at this is that the orientation of a device can be specified by the values of all possible DOFs at some time tX. By comparison, the motion of a device is determined by the differences between the values of all possible DOFs between times t0 and t1.
Sensors like accelerometers, gyroscopes, and magnetometers are available with one, two, or three axes. A 1-axis accelerometer, for example, will only detect changes along whichever of the three axes to which it is aligned; a 2-axis sensor will detect changes on two of the three axes; and a 3-axis sensor will detect changes on all three axes.
If a sensor platform is described as tracking more than six axes, this indicates that it provides a higher degree of accuracy by tracking multiple data points along (or around) the X, Y, and Z axes. An example is a 12-axis accelerometer suite that utilizes linear acceleration measurements from four, 3-axis accelerometers.
Unfortunately, it is common to confuse DOF with the numbers of axes. For example, the combination of a 3-axis accelerometer, 3-axis gyroscope, and 3-axis magnetometer may be described by some vendors as a 9DOF sensor, even though it should more correctly be described as a 6DOF 9-axis sensor.
In addition to measuring acceleration, an accelerometer also measures gravity. For example, in the case of a smartphone, a 3-axis accelerometer can determine which direction is down, even if the user is standing still and the device is motionless.
A 3-axis accelerometer can also be used to determine the vertical and horizontal orientation of the device, which can use this information to present its display in portrait or landscape modes. On its own, however, the accelerometer cannot be used to determine the smartphone’s orientation with regard to the Earth’s magnetic field. This capability is necessary for tasks like planetarium apps that allow the user to identify and locate stars, planets, and constellations in the night sky by simply pointing the device toward the area of interest. In this case, a magnetometer is required. If the smartphone were always placed flat on a table, then a 1-axis magnetometer would suffice. Since a smartphone may be used in any orientation, however, it is necessary to employ a 3-axis magnetometer.
Accelerometers aren’t affected by the surrounding magnetic field, but they are affected by movement and vibration. By comparison, magnetometers aren’t affected by movement and vibration per se, but they can be influenced by magnetic materials and electromagnetic fields in the vicinity.
Although a 3-axis accelerometer can also be used to derive rotational data, a 3-axis gyroscope provides more accurate data regarding angular momentum. Gyroscopes work well when it comes to measuring rotational speed, and they are not affected by acceleration in a linear direction or by magnetic fields. However, gyroscopes do have a tendency to generate a small “residual” rotation speed, even when they become stationary. This is known as “zero-drift-offset.” The problem arises if the user is attempting to determine an absolute angle using the gyroscope, in which case it is necessary to integrate the rotational speed to obtain the angular position. The issue with integration in this scenario is that errors can accumulate. A small error of only 0.01 degrees in the first measurement can grow to a full degree after 100 measurements, for example. This is known as “gyro drift.”
The term “sensor fusion” refers to the combining of sensory data derived from disparate sources such that the resulting information has less uncertainty than would be possible if the data from those sources was used individually.
In the case of a sensor array comprising a 3-axis accelerometer, 3-axis gyroscope, and 3-axis magnetometer, for example, the data from the accelerometer and the magnetometer can be used to cancel out gyro drift. Meanwhile, the data from the gyro can be used to compensate for any vibration-induced noise from the accelerometer and magnetic material/field-induced noise from the magnetometer.
The result of using sensor fusion is that the accuracy of the output exceeds the accuracy of individual sensors.
Introducing some representative sensors
Depending on the application, the designer may decide to employ only a single type of motion/orientation sensor in the form of an accelerometer, gyroscope, or magnetometer.
A good introductory accelerometer is the 2019 BOB from Adafruit, which features a 3-axis accelerometer with a 14-bit analog-to-digital converted (ADC) (Figure 2).
Figure 2: The 2019 BOB from Adafruit features a 3-axis accelerometer that can be used to detect motion, tilt, and basic orientation. (Image source: Adafruit)
The high-precision 3-axis sensor has a wide range from ±2 g to ±8 g and can be used to detect motion, tilt, and basic orientation. The sensor requires a 3.3 volt supply, but the BOB includes a low-dropout 3.3 volt regulator and level shifting circuitry, thereby making it safe for use with 3 volt or 5 volt power and logic. Communication between the BOB and the Arduino (or another microcontroller) is performed using I2C.
For applications requiring only a gyroscopic sensor to detect twisting and turning motions, a good introduction board is Adafruit’s 1032 BOB with the L3GD20H 3-axis gyroscope from STMicroelectronics. Supporting both I2C and SPI interfaces to the Arduino (or another microcontroller), the L3GD20H can be set to ±250, ±500, or ±2000 degree-per-second scale for a large range of sensitivity. Once again, the sensor requires a 3.3 volt supply, but the BOB includes a 3.3 volt regulator and level shifting circuitry allowing it to be used with either 3 volt or 5 volt power and logic.
Similarly, for applications requiring only a magnetic sensor, a good evaluation option is the 4479 BOB from Adafruit, which features the LIS3MDL 3-axis magnetometer from STMicroelectronics. The LIS3MDL can sense ranges from ±4 gauss (±400 microtesla (µT)) up to ±16 gauss (±1600 µT or 1.6 millitesla (mT)). Communication between the BOB and the Arduino (or another microcontroller) is performed using I2C. Again, the BOB includes 3.3 volt regulator and level shifting circuitry making it safe for use with 3 volt or 5 volt power and logic.
It's very common for multiple sensors to be used in conjunction with each other. For example, an accelerometer may be used in conjunction with a gyroscope to perform tasks like 3D motion capture and inertial measurement; that is, to allow the user to determine how an object is moving in a 3D space. One example of such a combo is Adafruit’s 4480 BOB (Figure 3), which features the LSM6DS33 sensor chip from STMicroelectronics.
Figure 3: The 4480 BOB from Adafruit features the LSM6DS33TR 3-axis accelerometer and a 3-axis gyroscope that can be used to perform tasks like 3D motion capture and inertial measurement. (Image source: Adafruit)
The 3-axis accelerometer can provide data as to which direction is towards the Earth by measuring gravity, and how fast the board is accelerating in 3D space. Meanwhile, the 3-axis gyroscope can measure spin and twist. As with the other sensor BOBs presented earlier, the 4480 BOB includes a 3.3 volt regulator and level shifting circuitry, making it safe for use with 3 volt or 5 volt power and logic. Also, the sensor data can be accessed using both I2C or SPI interfaces, allowing it to be used with an Arduino (or other microcontroller) without any complicated hardware setup.
Another example of a dual-sensor BOB is the 1120 from Adafruit, which features the combination of a 3-axis accelerometer and a 3-axis magnetometer in the form of an LSM303 sensor chip from STMicroelectronics. Communications between the microcontroller and the 1120 are via an I2C interface, and the BOB includes a 3.3 volt regulator and level shifting circuitry, making it safe for use with 3 volt or 5 volt power and logic.
Some applications require the use of accelerometers, gyroscopes, and magnetometers. In this case, a useful introductory BOB is Adafruit’s 3463, which features two sensor chips: a 3-axis gyroscope, and a 3-axis accelerometer with a 3-axis magnetometer. Communications between the BOB and microcontroller are implemented via an SPI interface. Also, a 3.3 volt regulator and level shifting circuitry are included, making it safe for use with 3 volt or 5 volt power and logic.
One advantage of the 3463 BOB is that the designer is provided with raw access to the data from the three sensors. A corresponding disadvantage is that using this sensor (manipulating and processing its data) will require about 15 kilobytes (Kbytes) of the microcontroller’s flash memory and will consume a lot of clock cycles.
As an alternative, Adafruit’s 2472 BOB features a BNO055 sensor chip from Bosch. The BNO055 includes a 3-axis accelerometer, a 3-axis-gyroscope, and a 3-axis magnetometer, all presented in a single package (Figure 4).
Figure 4: In addition to a 3-axis accelerometer, 3-axis gyroscope, and 3-axis magnetometer, the BNO055 sensor on Adafruit’s 2472 BOB also includes an Arm Cortex-M0 processor that performs sensor fusion. (Image source: Adafruit)
Furthermore, the BNO055 also includes a 32-bit Arm Cortex-M0 processor, which takes the raw data from the three sensors, performs sophisticated sensor fusion, and provides designers with processed information in forms they can use: quaternions, Euler angles, and vectors. More specifically, via the 2472 BOB’s I2C interface, designers can quickly and easily access the following:
- Absolute orientation (Euler Vector, 100 Hertz (Hz)): Three-axis orientation data based on a 360° sphere.
- Absolute orientation (Quatenrion, 100 Hz): Four-point quaternion output for more accurate data manipulation.
- Angular velocity vector (100 Hz): Three-axis of “rotation speed” in rad/s.
- Acceleration vector (100 Hz): Three-axis of acceleration (gravity + linear motion) in meters per second squared (m/s2).
- Magnetic field strength vector (20 Hz): Three-axis of magnetic field sensing (in µT).
- Linear acceleration vector (100 Hz): Three-axis of linear acceleration data (acceleration minus gravity) in m/s2.
- Gravity vector (100 Hz): Three-axis of gravitational acceleration (minus any movement) in m/s2.
- Temperature (1 Hz): Ambient temperature in degrees Celsius.
Having the sensor fusion performed on-chip frees up the main microcontroller’s memory and compute cycles, which is ideal for designers creating low-cost, real-time systems. Furthermore, sensor fusion algorithms can be difficult and time-consuming to master. Having the sensor fusion performed on-chip allows system developers to be up and running in minutes, as opposed to days or weeks if they are implementing algorithms from the ground up.
Many designers are interested in incorporating motion and orientation sensors into their designs but are unsure where to start. In the case of designers who are not used to working with these devices, getting familiar with sensors from various manufacturers can be challenging. One way to start experimenting and prototyping is to use low-cost, open-source microcontroller development boards like the Arduino, along with low-cost open-source sensor BOBs featuring sensors from multiple vendors.
Disclaimer: The opinions, beliefs, and viewpoints expressed by the various authors and/or forum participants on this website do not necessarily reflect the opinions, beliefs, and viewpoints of Digi-Key Electronics or official policies of Digi-Key Electronics.