MMA8491Q Accel Tilt Sensor

This documentation includes installation guidelines and sample code for your hardware.

Windows 10 IoT Core Library

MMA8491QSensor library is responsible for communicating with the MMA8491Q accel and tilt sensor.

To use the library, add the following using statement to the top of your C# code.

using TurtaIoTHAT;

Then, create an instance of the MMA8491QSensor class.

MMA8491QSensor accel;

Now you're ready to access the library by calling the accel instance.

Initialization

MMA8491QSensor

Initiates the MMA8491Q sensor to get 3D acceleration data and tilt interrupt.

MMA8491QSensor(Modes sensorMode)
  • Parameters

    • Enum Modes sensorMode:

      • Accelerometer: Initiates the library with accelerometer settings.

      • TiltSensor: Initiates the library with the tilt sensor settings.

Basic Members

Read X Axis

Reads the X-axis G value.

double ReadXAxis()
  • Parameters

    • None

  • Returns

    • Double: X-axis G value.

Read Y Axis

Reads the Y-axis G value.

double ReadYAxis()
  • Parameters

    • None

  • Returns

    • Double: Y-axis G value.

Read Z Axis

Reads the Z-axis G value.

double ReadZAxis()
  • Parameters

    • None

  • Returns

    • Double: Z-axis G value.

Read XYZ Axis

Reads the X, Y and Z-Axis G values respectively.

double[] ReadXYZAxis()
  • Parameters

    • None

  • Returns

    • Double[]: X, Y and Z-Axis G values respectively.

Read Tilt State

Reads the tilt state.

bool ReadTiltState()
  • Parameters

    • None

  • Returns

    • Bool: Tilt state. Returns True if acceleration is > 0.688g or X/Y axis > 43.5. False if not.

Interrupts

Tilt Changed

Notifies on sensor tilt change.

event MMA8491QTiltEventHandler TiltChanged
  • Event Args

    • Bool TiltDetected: Returns True if acceleration is > 0.688g or X/Y axis > 43.5. False if not.

Disposal

To clean up the resources used by the class, call the Dispose() method it when the application is closing.

void Dispose()

This method stops the timers and cleans the resources used by the class.

Samples

You can copy the example codes from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Windows10IoTCore address. There are two examples of this library.

Accel Sample App

This application demonstrates reading 3D acceleration data from the sensor.

To run the example:

  1. Open the sample project you downloaded.

  2. Make sure "ARM" is selected from the solution platforms selection.

  3. Deploy the sample to the Raspberry Pi using the "F5" key.

Sample Code

MainPage.xaml.cs
using System.Diagnostics;
using System.Threading;
using Windows.UI.Xaml.Controls;
using TurtaIoTHAT;

namespace MMA8491QAccelSampleApp
{
    public sealed partial class MainPage : Page
    {
        // MMA8491Q Sensor
        static MMA8491QSensor accel;

        // Sensor timer
        Timer sensorTimer;

        public MainPage()
        {
            this.InitializeComponent();

            // Initialize sensor and timer
            Initialize();
        }

        private void Initialize()
        {
            // Create sensor instance
            accel = new MMA8491QSensor(MMA8491QSensor.Modes.Accelerometer);

            // Configure timer to 2000ms delayed start and 1000ms interval
            sensorTimer = new Timer(new TimerCallback(SensorTimerTick), null, 2000, 1000);
        }

        private static void SensorTimerTick(object state)
        {
            // Write sensor data to output / immediate window
            double[] g = accel.ReadXYZAxis();

            Debug.WriteLine("Accel X: " + g[0].ToString("0.00") + "G");
            Debug.WriteLine("Accel Y: " + g[1].ToString("0.00") + "G");
            Debug.WriteLine("Accel Z: " + g[2].ToString("0.00") + "G");
            Debug.WriteLine("-----");
        }
    }
}

Result

When you run the sample, it prints the 3D acceleration reading to the Visual Studio Output / Immediate Window.

Accel X 0.10G
Accel Y 0.20G
Accel Z 0.30G
-----

To stop the application, use the "Stop" button on the Visual Studio.

Tilt Detect Sample App

This application demonstrates reading tilt interrupts from the sensor.

To run the example:

  1. Open the sample project you downloaded.

  2. Make sure "ARM" is selected from the solution platforms selection.

  3. Deploy the sample to the Raspberry Pi using the "F5" key.

Sample Code

MainPage.xaml.cs
using System.Diagnostics;
using Windows.UI.Xaml.Controls;
using TurtaIoTHAT;

namespace MMA8491QTiltDetectSampleApp
{
    public sealed partial class MainPage : Page
    {
        // MMA8491Q Sensor
        static MMA8491QSensor accel;

        public MainPage()
        {
            this.InitializeComponent();

            // Initialize sensor and timer
            Initialize();
        }

        private void Initialize()
        {
            // Create sensor instance
            accel = new MMA8491QSensor(MMA8491QSensor.Modes.TiltSensor);

            // Subscribe to Tilt Changed event
            accel.TiltChanged += TiltChanged;
        }

        private void TiltChanged(object sender, MMA8491QTiltEventArgs e)
        {
            // Write tilt state to output / immediate window
            Debug.WriteLine(e.TiltDetected ? "Tilt detected." : "No tilt.");
        }
    }
}

Result

When you run the sample, it prints tilt change interrupts to the Visual Studio Output / Immediate Window.

Tilt deteccted.
No tilt.

To stop the application, use the "Stop" button on the Visual Studio.

Last updated