MMA8491Q Accel Tilt Sensor

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

Python Library

Turta_MMA8491Q library is responsible for communicating with the MMA8491Q sensor.

To install the library from the (PyPI) Python Package Index, enter the following command to the console.

pip install turta-iothat

Add the following statement to the top of your Python code.

from turta_iothat import Turta_MMA8491Q

Then, create an instance of the Turta_MMA8491Q class.

mma8491q = Turta_MMA8491Q.MMA8491QSensor()

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

Initialization

The library automatically initializes the required components when its instance is being created, so there is no need to do a manual initialization.

Basic Members

Read X Axis

Reads the X-axis G value.

float read_x_axis()
  • Parameters

    • None

  • Returns

    • Float: X-axis G value.

Read Y Axis

Reads the Y-axis G value.

float read_y_axis()
  • Parameters

    • None

  • Returns

    • Float: Y-axis G value.

Read Z Axis

Reads the Z-axis G value.

float read_z_axis()
  • Parameters

    • None

  • Returns

    • Float: Z-axis G value.

Read XYZ Axis

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

float[] read_xyz_axis()
  • Parameters

    • None

  • Returns

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

Read Tilt State

Reads the tilt state.

bool read_tilt_state()
  • Parameters

    • None

  • Returns

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

Samples

You can copy the example code from https://github.com/Turta-io/IoTHAT/tree/master/Samples/Raspbian/Python address, and then copy it to the Raspberry Pi. There is one example of this library.

MMA8491Q Sample App

This application demonstrates printing accel and tilt readings from the sensor.

To run the example:

  1. Copy the library and sample code to a folder on the Raspberry Pi.

  2. Open terminal, and go to the folder you copied the codes.

  3. Run the sample with the following command:

python MMA8491QSampleApp.py

Sample Code

MMA8491QSampleApp.py
#! /usr/bin/python

import time
from turta_iothat import Turta_MMA8491Q
#Install IoT HAT library with "pip install turta-iothat"

#Initialize
mma8491q = Turta_MMA8491Q.MMA8491QSensor()

try:
    while True:
        #Read & print X, Y and Z-Axis G values in one shot
        xyz = mma8491q.read_xyz_axis()
        print("X-Axis..........: " + str(round(xyz[0], 2)) + "G")
        print("Y-Axis..........: " + str(round(xyz[1], 2)) + "G")
        print("Z-Axis..........: " + str(round(xyz[2], 2)) + "G")

        #Read & print tilt state
        print("Tilt............: " + ("Tilt detected." if mma8491q.read_tilt_state() else "No tilt."))

        #Rest a bit
        print("-----")
        time.sleep(0.5)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints X, Y, and Z axis G readings and tilt state to the terminal. The example output should be like this:

X-Axis..........: 1.00G
Y-Axis..........: 0.33G
Z-Axis..........: 0.66G
Tilt............: No tilt.
-----

To stop the Python script, just press CTRL + C.

Last updated