Tilt and Acceleration with the MX2125

The Memsic MX2125 Dual-axis Accelerometer is great for measuring tilt, vibration, rotation, and let’s not forget its namesake, acceleration.  A few MX2125 project ideas might include a tilt video game controller, tilt robot controller, robot hill climbing sensor, and radio vehicle acceleration, speed, and position data logger.

 

Circuit

The MX2125 sends two acceleration signals, one for its x-axis, and a separate one for its y-axis.  In this test, we’ll use P11 to monitor the y-axis signal and P10 to monitor the x-axis signal.

Parts

(1) Memsic2125 Accelerometer (#28017)
(2) 220 ohm resistors (red-red-brown, #150-02210)

  • Build the circuit shown here.

 

 

 

 

This lesson will work on any Propeller development board with a prototyping area.

 

Tilt Test

The Test 2125 Tilt application reports tilt in two directions: y-axis (left/right) and x-axis (toward/away from you).   

  • Click SimpleIDE’s Open Project button.
  • Open Test 2125 Tilt from ... Documents\SimpleIDE\Learn\Examples\Devices\Sensor\Accelerometer MX2125. 
  • Click the Run with Terminal button.
  • Hold the board level, and verify that both x and y tilts are close to zero.

  • Tilt the board to the right by about 45 degrees, and verify that the y-axis reports about 45. 
  • Try tilting to the left instead and check the sign of the angle measurement.

  • Tilt the board toward you by about 45 degrees, and verify that the x-axis reports about 45.
  • Tilt the board away from you, did the sign of the measurement change?

 

How it Works

The MX2125 sends out two streams of pulses, one for the X axis and one for the Y axis.  These high pulses last about 5000 µs (microseconds) when the board is held level. 

As the board is tilted, the high pulses will vary across a range of 3175 to 6125 µs.  Functions inside the mx2125 library scales these measurements to a range of  +/- 1250.  After that, it  converts the measurements to degrees with a maximum range of +/- 90. (The output range of your particular MX2125 unit may vary.)

You can add the mx2125 library to any project with the Add Simple Library button. Just click it,  navigate to …Documents\SimpleIDE\Learn\Simple Libraries\Sensor\libmx2125, and click OK.  This incorporates all of the library's files into your project and adds #include "mx2125.h" to your code. 

This library puts functions named mx_accel, mx_tilt, and mx_rotate at your service. 

Here, P10 is connected to the MX2125’s Xout pin, so  int x = mx_tilt(10) gets the x-axis tilt measurement and stores it in a variable named x

Similarly, int y = mx_tilt(11) gets the y-axis tilt and stores it in a variable named y.

In the example below, these measurements are taken and displayed with a print statement, every 200 ms in an endless loop.

/*
  Test 2125 Tilt.c
 
  Measure tilt.  Assumes MX2125 is being held horizontally and then tilted.   
*/

#include "simpletools.h"                          // Include simpletools header
#include "mx2125.h"                               // Include mx2125 header

int main()                                        // Main function
{
  while(1)                                        // Repeat indefinitely
  {
    int x = mx_tilt(10);                          // X-axis tilt
    int y = mx_tilt(11);                          // Y-axis tilt
    print("%ctilt x = %d, tilt y = %d, %c\n",     // Display tilts
            HOME,       x,           y, CLREOL);
    pause(200);                                   // 1/5th second before repeat
  }  
}

 


Did You Know?

The MX2125 has a tiny chamber inside that’s filled with nitrogen gas.  There’s a heating element in the middle, and temperature sensors around chamber's sides.

As you tilt the MX2125, the hotter gas rises.  The temperature sensor(s) closest to the hot gas detect these higher values.  Circuits inside the sensor use those temperature measurements to control the duration of the high output pulses that a microcontroller can read. 

How does this work for measuring acceleration?  First, think about having a partially filled water bottle in a car seat.  As the car accelerates, the heavier water sloshes away from the direction the car is accelerating, pushing the lighter air forward.  The cooler nitrogen gas behaves like the water in your water bottle, sloshing away from the direction the sensor is accelerating.  Also like the water bottle, it displaces the hotter nitrogen gas toward the direction of acceleration.   The temperature sensors pick this changing temperature pattern, and update the output pulse widths accordingly.


 

Try This

You can hold your board vertically in front of you and turn it like it is a steering wheel.  The mx2125 library’s mx_rotate function will return the board's rotation angle in degrees, 0 to 359. 

  • Click SimpleIDE’s Open Project button.
  • Open Test 2125 Rotation from ... Documents\SimpleIDE\Learn\Examples\Devices\Sensor\Accelerometer MX2125. 
  • Click the Run with Terminal button.
  • Hold the board vertically and rotate it like it’s a steering wheel while you watch the SimpleIDE Terminal.

 

Your Turn

  • Try using the tilt angle returned by mx_rotate to control something.  It could be speaker tone, LED brightness, servo position, or even the position of a character in the SimpleIDE terminal. 

Hint: for character position, you’ll need to use some math on the x and y values to scale them into a range 0 to 20. Then, use print(“%c%c%c*”, CRSRXY, x, y) to place an asterisk in the SimpleIDE Terminal, or print(“%c%c%c ”, CRSRXY, xOld, yOld) to blot out the previously positioned asterisk with a space.