Accel & Tilt Module

Last updated 12 days ago

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

Overall Info

Turta Accel & Tilt Module carries 3-axis accelerometer with tilt sensing and digital accelerometer functions.

Illustration

Top
Bottom

Module Connections

  • I2C: SCL and SDA.

  • ENABLE: PWM.

  • INT X: INT.

  • INT Y: GPIO.

  • INT Z: AN.

  • POWER: +3.3V and GND.

Specifications

MMA8491Q's technical specifications are as follows:

  • Range: ±8 g full-scale range.

  • Sensitivity: 14-bit digital output, 1 mg/LSB sensitivity

  • Tilt Detection: 3-Axis, 43.5° / 0.688g.

  • Output Data Rate: Up to 800Hz.

Prerequisites

To get maximum performance from your hardware, please make sure you'll meet the following requirements.

Hardware

Accel & Tilt Module is an add-in board for Turta Modular System. You'll need a compatible host device for your module. The following devices support Accel & Tilt Module:

  • Turta IoT Node - ESP32

Software

For ESP32 based systems, we're currently supporting Arduino core. Please make sure you've installed the following:

  • Arduino IDE.

  • ESP32 Hardware libraries for the Arduino IDE.

  • Python for the compilation of Arduino projects.

  • Drivers for your host device, e.g., USB Virtual Serial Port driver.

Installation

You'll need to install a library to make Arduino IDE support the module. This library includes drivers for the MMA8491Q and sample code for you to jumpstart your project.

Libraries

Please download the library archive from https://github.com/Turta-io/Modular

Then, copy its contents to your Arduino Libraries folder.

You can find the Arduino Libraries folder inside your Arduino sketch directory. It is generally located inside the My Documents folder.

Samples

The library comes with included samples. There is no need to download additional files for the examples.

Source Code

The hardware, its drivers and the sample code are open source. You can use them under the terms of the MIT license. Please feel free to customize and prototype them for your projects.

Arduino Library

Turta_AccelTilt_Module library is responsible for reading the photocoupler inputs.

To use the library on Arduino IDE, add the following #include statement to the top of your sketch.

#include <Turta_AccelTilt_Module.h>

Then, create an instance of the Turta_AccelTilt_Module class.

Turta_AccelTilt_Module accel

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

Initialization

To initialize the module, call the begin method.

begin()

This method configures the I2C bus and GPIO pins to read sensor data.

Basic Members

Read X-Axis

Returns the G value of the X axis.

double readXAxis()
  • Parameters

    • None.

  • Returns

    • Double: G Value of the X axis.

Read Y-Axis

Returns the G value of the Y axis.

double readYAxis()
  • Parameters

    • None.

  • Returns

    • Double: G Value of the Y axis.

Read Z-Axis

Returns the G value of the Z axis.

double readZAxis()
  • Parameters

    • None.

  • Returns

    • Double: G Value of the Z axis.

Read XYZ Axis

Returns the values of all axes in a single shot.

void readXYZAxis(double x, double y, double z)
  • Parameters

    • Double: x out

    • Double: y out

    • Double: z out

  • Returns

    • None

Read Tilt State

Returns the tilt state of all axes.

void readTiltState(bool xTilt, bool yTilt, bool zTilt)
  • Parameters

    • Bool: xTilt out

    • Bool: yTilt out

    • Bool: zTilt out

  • Returns

    • None

Examples

You can open the example from Arduino IDE > File > Examples > Examples from Custom Libraries > Turta Accel & Tilt Module. There are two examples of this sensor.

Accel

This application demonstrates reading G values applied to X, Y and Z axes.

To run the example:

  1. Open the IoT_Node_AccelTilt_Accel sketch from the examples menu.

  2. Select Turta IoT Node from the Tools > Board menu.

  3. Select your device's COM port from Tools > Port menu.

  4. Open Serial Monitor from Tools > Serial Monitor.

  5. Select 115.200 baud from the Serial Monitor's status bar.

  6. Upload the code to your device.

Sample Code

IoT_Node_AccelTilt_Accel.ino
#include <Turta_AccelTilt_Module.h>
// Create Accel & Tilt Sensor instance.
Turta_AccelTilt_Module accel;
void setup() {
// Initialize Accel & Tilt Sensor.
accel.begin();
// Configure serial port.
Serial.begin(115200);
delay(200);
}
void loop() {
// Read X-Axis.
double xAxisG = accel.readXAxis();
Serial.print("X: ");
Serial.println(xAxisG);
// Read Y-Axis.
double yAxisG = accel.readYAxis();
Serial.print("Y: ");
Serial.println(yAxisG);
// Read Z-Axis.
double zAxisG = accel.readZAxis();
Serial.print("Z: ");
Serial.println(zAxisG);
// Read X, Y, Z-Axis at one shot.
double x, y, z;
accel.readXYZAxis(x, y, z);
Serial.print("X: ");
Serial.print(x);
Serial.print(", Y: ");
Serial.print(y);
Serial.print(", Z: ");
Serial.println(z);
// Print an empty line.
Serial.println("");
// Delay 100ms.
delay(100);
}

Result

After the application is uploaded to the device, it writes G forces applied to all axes to the Serial Monitor. The example output should be like this:

X: 0.12
Y: 0.34
Z: 0.92
X: 0.12, Y: 0.34, Z: 0.92

The application runs forever until you clear it from memory.

Tilt

This application demonstrates reading tilt states of X, Y, and Z axes.

To run the example:

  1. Open the IoT_Node_AccelTilt_Tilt sketch from the examples menu.

  2. Select Turta IoT Node from the Tools > Board menu.

  3. Select your device's COM port from Tools > Port menu.

  4. Open Serial Monitor from Tools > Serial Monitor.

  5. Select 115.200 baud from the Serial Monitor's status bar.

  6. Upload the code to your device.

Sample Code

IoT_Node_AccelTilt_Tilt.ino
#include <Turta_AccelTilt_Module.h>
// Create Accel & Tilt Sensor instance.
Turta_AccelTilt_Module accel;
void setup() {
// Initialize Accel & Tilt Sensor.
accel.begin();
// Configure serial port.
Serial.begin(115200);
delay(200);
}
void loop() {
// Read Tilt State.
bool xTilt, yTilt, zTilt;
accel.readTiltState(xTilt, yTilt, zTilt);
Serial.print("X: ");
Serial.print(xTilt ? "Tilt" : "Flat");
Serial.print(", Y: ");
Serial.print(yTilt ? "Tilt" : "Flat");
Serial.print(", Z: ");
Serial.println(zTilt ? "Tilt" : "Flat");
// Delay 100ms.
delay(100);
}

Result

After the application is uploaded to the device, it writes tilt states to the Serial Monitor. The example output should be like this:

X: Flat, Y: Flat, Z: Tilt
X: Flat, Y: Flat, Z: Tilt
X: Flat, Y: Tilt, Z: Flat
X: Flat, Y: Tilt, Z: Flat

The application runs forever until you clear it from memory.

Troubleshooting

If you're experiencing difficulties while working with your device, please try the following steps.

Problem: When using the single shot reading function, Y-axis returns 0 G. Cause: There is a software communication error on the I2C bus. Solution: It's a known issue, and we're working on to fix this bug. Until then, please use the single reading functions. It's not a malfunction.