Qwiic Accelerometer (MMA8452Q) Hookup Guide
Example Code
Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.
We've written an Arduino library to make interfacing with the MMA8452Q as easy as can be. Click on the button below to download the library. Or you can grab the latest, greatest version over on the library's GitHub repository.
Load Up the Example Sketch
Once you've installed the SFE_MMA8452Q library, restart Arduino. Then go to File > Examples > SFE_MMA8452Q > MMA8452Q_Basic to open the example sketch.
Once you've set your Board and Serial Port, upload the sketch to your Arduino. Then open the serial monitor. You'll begin to see acceleration values stream by, in addition to some information about the sensor's orientation.
Try moving the sensor around to change those values. If it is motionless, flat on the desk, then an acceleration of 1g should be felt on the z-axis, while the others feel around 0. Test the other axes by rotating the board and making them feel the pull of gravity.
Using the SFE_MMA8452Q Library
Here are some tips on using the MMA8452Q Arduino library so you can embed it into an Arduino sketch of your own.
Include the Library (Global)
To begin, you need to "include" the library in your sketch:
language:c
#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library
The library also requires that you include Wire.h
in your sketch. Make sure you include that before you include the SFE_MMA8452Q.h file.
Create an MMA8452Q Object (Global)
Once the library is included, you can create an MMA8452Q object. This line of code will do it for you:
language:c
MMA8452Q accel; // Default MMA8452Q object create. (Address = 0x1D)
Optionally, you can define the 7-bit I2C address of your MMA8452Q in this parameter, using one of these lines of code:
language:c
MMA8452Q accel(0x1C); // Initialize the MMA8452Q with an I2C address of 0x1C (SA0=0)
MMA8452Q accel(0x1D); // Initialize the MMA8452Q with an I2C address of 0x1D (SA0=1)
But if you've left the address jumper untouched (meaning the "SA0" pin is connected to VCC), you can call the default (no parameter) constructor shown earlier.
Initialize the MMA8452Q (Setup)
Finally, in the setup()
function of your sketch, you can initialize the accelerometer using the init()
function. The init()
function verifies communication with the accelerometer, and sets up the full-scale range and output data rate.
Again, you have a few options here. You can use a simple declaration like below. This will initialize the accelerometer with range of ±2g and an output data rate of 800 Hz (turns the accelerometer up to the max!):
language:c
accel.init(); // Default init: +/-2g and 800Hz ODR
If you want to specify the acceleration and output data rate, you can instead use an init()
function like this:
language:c
accel.init([scale], [odr]); // Init and customize the FSR and ODR
Scale can be either SCALE_2G
, SCALE_4G
, or SCALE_8G
. The "odr" variable can be either ODR_800
, ODR_400
, ODR_200
, ODR_100
, ODR_50
, ODR_12
, ODR_6
, or ODR_1
, respectively setting the data rate to 800, 400, 200, 100, 50, 12.5, 6.25, or 1.56 Hz.
Reading and Using Values
Once you've set the accelerometer up, you can immediately start reading the data coming out of the chip. Reading and using the values is a two-step process. First, call the read()
function to pull in the values.
language:c
accel.read(); // Update acceleromter data
After you've called the read()
function, you can use either of two sets of values to use the data. Reading from the x
, y
, and z
class variables will return a signed 12-bit integer read straight out of the accelerometer.
language:c
xAcceleration = accel.x; // Read in raw x-axis acceleration data
Serial.print("Acceleration on the x-axis is ");
Serial.println(xAcceleration);
Or, if you want a value with physical units, you can use the cx
, cy
, and cz
class variables. These are the calculated acceleration values read out of the accelerometer; they'll be in units of g's.
language:c
zAcceleration = accel.cz; // Read in calculated z-axis acceleration
Serial.print("Acceleration on the z-axis is: ");
Serial.print(zAcceleration);
Serial.println(" g's");
Remember! Those variables are only updated after the read()
function is called. Make sure that happens before you start using acceleration values.
Reading Portrait/Landscape
The MMA8452Q has all sorts of nifty, extra features, one of which is orientation detection -- it can estimate if it's being held in landscape mode, portrait mode, or flat.
To read the portrait/landscape data from the accelerometer, use the readPL()
function. This function returns a byte, which will either be equal to PORTRAIT_U
, PORTRAIT_D
, LANDSCAPE_R
, LANDSCAPE_L
, or LOCKOUT
.
language:c
byte pl = accel.readPL();
switch (pl)
{
case PORTRAIT_U:
Serial.print("Portrait Up");
break;
case PORTRAIT_D:
Serial.print("Portrait Down");
break;
case LANDSCAPE_R:
Serial.print("Landscape Right");
break;
case LANDSCAPE_L:
Serial.print("Landscape Left");
break;
case LOCKOUT:
Serial.print("Flat");
break;
}
As in the example above, you can use if
or switch
statements to check which orientation your accelerometer is in.