Arduino Code

You can easily wire this breakout to any microcontroller, we'll be using an Arduino. For another kind of microcontroller, just make sure it has I2C with repeated-start support, then port the code - its pretty simple stuff!
  • Connect Vin to the power supply, 3-5V is fine. Use the same voltage that the microcontroller logic is based off of. For most Arduinos, that is 5V
  • Connect GND to common power/data ground
  • Connect the SCL pin to the I2C clock SCL pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A5, on a Mega it is also known as digital 21 and on a Leonardo/Micro, digital 3
  • Connect the SDA pin to the I2C data SDA pin on your Arduino. On an UNO & '328 based Arduino, this is also known as A4, on a Mega it is also known as digital 20 and on a Leonardo/Micro, digital 2
The MMA8451 has a default I2C address of 0x1D and can be changed to 0x1C by tying the A pin to GND

Download Adafruit_MMA8451

To begin reading sensor data, you will need to download the Adafruit_MMA8451_Library from our github repository. You can do that by visiting the github repo and manually downloading or, easier, just click this button to download the zip
Rename the uncompressed folder Adafruit_MMA8451 and check that the Adafruit_MMA8451 folder contains Adafruit_MMA8451.cpp and Adafruit_MMA8451.h

Place the Adafruit_MMA8451 library folder your arduinosketchfolder/libraries/ folder.
You may need to create the libraries subfolder if its your first library. Restart the IDE.

We also have a great tutorial on Arduino library installation at:

Download Adafruit_Sensor

The MMA8451 library uses the Adafruit_Sensor support backend so that readings can be normalized between sensors. You can grab Adafruit_Sensor from the github repo or just click the button below.
Install like you did with Adafruit_MMA8451

Load Demo

Open up File->Examples->Adafruit_MMA8451->MMA8451demo and upload to your Arduino wired up to the sensor
Thats it! Now open up the serial terminal window at 9600 speed to begin the test.

There's three lines of output from the sensor.

Example for line 1:

X: 45 Y: -672 Z: 734

This is the "raw count" data from the sensor, its a number from -8192 to 8191 (14 bits) that measures over the set range. The range can be set to 2G, 4G or 8G

Example for line 2:

X: -0.07 Y: 0.09 Z: 9.8 m/s^2

This is the Adafruit_Sensor'ified nice output which is in m/s*s, the SI units for measuring acceleration. No matter what the range is set to, it will give you the same units, so its nice to use this instead of mucking with the raw counts. (Note that the screenshot above has the m/s^2 divided by 10, you can ignore that typo :)

Example for line 3:

Portrait Up Front

This is the output of the orientation detection inside the chip. Since inexpensive accelerometers are often used to detect orientation and tilt, this sensor has it built in. The orientation can be Portrait or Landscape, then Up/Down or Left/Right and finally tilted forward or tilted back. Note that if the sensor is tilted less than 30 degrees it cannot determine the forward/back orientation. If you play with twisting the board around you'll get the hang of it.

Library Reference

The library we have is simple and easy to use

You can create the Adafruit_MMA8451 object with:
Download: file
Adafruit_MMA8451 mma = Adafruit_MMA8451();
There are no pins to set since you must use the I2C bus!

Then initialize the sensor with:
Download: file
this function returns True if the sensor was found and responded correctly and False if it was not found. We suggest something like this:
Download: file
  if (! mma.begin()) {
    Serial.println("Couldnt start")
    while (1);
  Serial.println("MMA8451 found!");

Set & Get Range

You can set the accelerometer max range to ±2g, ±4g or ±8g with
Download: file
And read what the current range is with
Download: file
Which returns 1 for ±2g, 2 for ±4g and 3 for ±8g

Read Raw Count Data

You can read the raw counts data with
Download: file;
The x, y and z data is then available in mma.x, mma.y and mma.z
All three are read in one transaction.

Reading Normalized Adafruit_Sensor data

We recommend using the Adafruit_Sensor interface which allows reading into an event structure. First create a new event structure
Download: file
sensors_event_t event; 
Then read the event whenever you want
Download: file
The normalized SI unit data is available in event.acceleration.x, event.acceleration.y and event.acceleration.z

Read Orientation

The sensor has built in tilt/orientation detection. You can read the current orientation with
Download: file
The return value ranges from 0 to 7
  • 0: Portrait Up Front
  • 1: Portrait Up Back
  • 2: Portrait Down Front
  • 3: Portrait Down Back
  • 4: Landscape Right Front
  • 5: Landscape Right Back
  • 6: Landscape Left Front
  • 7: Landscape Left Back
This guide was first published on Jul 30, 2014. It was last updated on Jul 30, 2014. This page (Arduino Code) was last updated on Nov 11, 2019.