# Adafruit MMA8451 Accelerometer Breakout

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/018/452/medium800/sensors_2019iso_ORIG.jpg?1406585851)

You can detect motion, tilt and basic orientation with a digital accelerometer - and the MMA8451 is a great accelerometer to start with. It's low cost, but high precision with 14-bit ADC. It has a wide usage range, from +-2g up to +-8g yet is easy to use with Arduino or another microcontroller

The MMA8451 is a miniature little accelerometer from Freescale, who are (by this point) masters at the accelerometer-design game. It's designed for use in phones, tablets, smart watches, and more, but works just as well in your Arduino project. Of the MMA8451/MMA8452/MMA8453 family, the MMA8451 is the most precise with a built in 14-bit ADC. The accelerometer also has built in tilt/orientation detection so i can tell you whether your project is being held in landscape or portrait mode, and whether it is tilted forward or back

![](https://cdn-learn.adafruit.com/assets/assets/000/018/471/medium800/sensors_2019kit_ORIG_.jpg?1406662829)

This sensor communicates over I2C so you can share it with a bunch of other sensors on the same two I2C pins. There's an address selection pin so you can have accelerometers share an I2C bus. Please note this chip requires repeated-start I2C support (in case you are looking to port this to another processor)

![](https://cdn-learn.adafruit.com/assets/assets/000/018/473/medium800/sensors_2019_quarter_ORIG.jpg?1406662945)

To get you going fast, we spun up a breakout board for this little guy. Since it's a 3V sensor, we add a low-dropout 3.3V regulator and level shifting circuitry on board. That means its perfectly safe for use with 3V or 5V power and logic.

# Adafruit MMA8451 Accelerometer Breakout

## Pinouts

![](https://cdn-learn.adafruit.com/assets/assets/000/018/474/medium800/sensors_pinouts.jpg?1406662993)

The little chip in the middle of the PCB is the actual MMA8451 sensor that does all the motion sensing. We add all the extra components you need to get started, and 'break out' all the other pins you may want to connect to onto the PCB. For more details you can check out the schematics in the Downloads page.

# Power Pins
The sensor on the breakout requires 3V power. Since many customers have 5V microcontrollers like Arduino, we tossed a 3.3V regulator on the board. Its ultra-low dropout so you can power it from 3.3V-5V just fine.  

- **Vin** - this is the power pin. Since the chip uses 3 VDC, we have included a voltage regulator on board that will take 3-5VDC and safely convert it down. To power the board, give it the same power as the logic level of your microcontroller - e.g. for a 5V micro like Arduino, use 5V
- **3Vo** - this is the 3.3V output from the voltage regulator, you can grab up to 100mA from this if you like  
- **GND** - common ground for power and logic

# I2C Pins

- **SCL** - I2C clock pin, connect to your microcontrollers I2C clock line.
- **SDA** - I2C data pin, connect to your microcontrollers I2C data line.

# INT and ADDR Pins

- **A** is the I2C **A** ddress select pin. By default this is pulled up to 3.3V with a 10K resistor, for an I2C address of 0x1D. You can also connect it to the GND pin for an address of 0x1C
- **I1** and **I2** are the Interrupt #1 and #2 signal pins. These pins are for more advanced usage, where you want to be alerted by the chip say when data is ready to read, or if it detects a large motion. We don't have direct support in the example Arduino library for these pins, so please check the datasheet for the I2C commands

# Adafruit MMA8451 Accelerometer Breakout

## Assembly

![](https://cdn-learn.adafruit.com/assets/assets/000/018/488/medium800/sensors_2019kit_ORIG_.jpg?1406670293)

 
## Prepare the header strip:
Cut the strip to length if necessary. It will be easier to solder if you insert it into a breadboard - **long pins down** ![sensors_header.jpg](https://cdn-learn.adafruit.com/assets/assets/000/018/481/medium640/sensors_header.jpg?1406670112)

## Add the breakout board:
Place the breakout board over the pins so that the short pins poke through the breakout pads![sensors_place.jpg](https://cdn-learn.adafruit.com/assets/assets/000/018/482/medium640/sensors_place.jpg?1406670123)

 
## And Solder!
 Be sure to solder all pins for reliable electrical contact.  
  
_(For tips on soldering, be sure to check out our_ [_Guide to Excellent Soldering_](http://learn.adafruit.com/adafruit-guide-excellent-soldering)_)._![sensors_solder.jpg](https://cdn-learn.adafruit.com/assets/assets/000/018/483/medium640/sensors_solder.jpg?1406670153)

![sensors_solder1.jpg](https://cdn-learn.adafruit.com/assets/assets/000/018/484/medium640/sensors_solder1.jpg?1406670159)

![sensors_solder2.jpg](https://cdn-learn.adafruit.com/assets/assets/000/018/485/medium640/sensors_solder2.jpg?1406670163)

You're done! Check your solder joints visually and continue onto the next steps

![sensors_done.jpg](https://cdn-learn.adafruit.com/assets/assets/000/018/487/medium640/sensors_done.jpg?1406670278)

# Adafruit MMA8451 Accelerometer Breakout

## 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!

![](https://cdn-learn.adafruit.com/assets/assets/000/018/508/medium800/sensors_wiring.jpg?1406740454)

- 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 Libraries

To begin reading sensor data, you will need to download the Adafruit\_MMA8451 library and the Adafruit\_Sensor library from the Arduino library manager.

Open up the Arduino library manager:

![](https://cdn-learn.adafruit.com/assets/assets/000/084/230/medium800/sensors_library_manager_menu.png?1573777110)

Search for the&nbsp; **Adafruit MMA8451&nbsp;** library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/084/231/medium800/sensors_mma8451.png?1573777235)

Search for the&nbsp; **Adafruit Sensor&nbsp;** library and install it

![](https://cdn-learn.adafruit.com/assets/assets/000/084/232/medium800/sensors_adafruitsensor.png?1573777301)

We also have a great tutorial on Arduino library installation at:  
[http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use](http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use "Link: http://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use")

# Load Demo
Open up **File-\>Examples-\>Adafruit\_MMA8451**** -\>MMA8451demo** and upload to your Arduino wired up to the sensor ![](https://cdn-learn.adafruit.com/assets/assets/000/018/509/medium800/sensors_mmademo.png?1406744421)

[](https://learn.adafruit.com/assets/17117)
Thats it! Now open up the serial terminal window at 9600 speed to begin the test.
![](https://cdn-learn.adafruit.com/assets/assets/000/018/510/medium800/sensors_output.png?1406744633)

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:  
```
Adafruit_MMA8451 mma = Adafruit_MMA8451();
```

There are no pins to set since you must use the I2C bus!  
  
Then initialize the sensor with:

```
mma.begin()
```

this function returns **True** if the sensor was found and responded correctly and **False** if it was not found. We suggest something like this:

```
  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  
```
mma.setRange(MMA8451_RANGE_2_G);
mma.setRange(MMA8451_RANGE_4_G);
mma.setRange(MMA8451_RANGE_8_G);
```

And read what the current range is with

```
mma.getRange()
```

Which returns 1 for ±2g, 2 for ±4g and 3 for ±8g

## Read Raw Count Data
You can read the raw counts data with  
```
mma.read();
```

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  
```
sensors_event_t event; 
```

Then read the event whenever you want

```
mma.getEvent(&amp;event);
```

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   
```
mma.getOrientation();
```

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

![](https://cdn-learn.adafruit.com/assets/assets/000/084/232/medium800/sensors_adafruitsensor.png?1573777301)

# Adafruit MMA8451 Accelerometer Breakout

## Python & CircuitPython

It's easy to use the MMA8451 sensor with Python or CircuitPython, and the&nbsp;[Adafruit CircuitPython MMA8451](https://github.com/adafruit/Adafruit_CircuitPython_MMA8451)&nbsp;module.&nbsp; This module allows you to easily write Python code that reads the acceleration and more from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python [thanks to Adafruit\_Blinka, our CircuitPython-for-Python compatibility library](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).

# CircuitPython Microcontroller Wiring

First wire up a MMA8451 to your board exactly as shown on the previous pages for Arduino using an I2C connection. &nbsp;Here's an example of wiring a Feather M0 to the sensor with I2C:

- **Board 3V** &nbsp;to&nbsp; **sensor VIN**
- **Board GND** &nbsp;to&nbsp; **sensor GND**
- **Board SCL** &nbsp;to&nbsp; **sensor&nbsp;SCL**
- **Board SDA** &nbsp;to&nbsp; **sensor&nbsp;SDA**

![sensors_m0_mma8451_bb.png](https://cdn-learn.adafruit.com/assets/assets/000/050/644/medium640/sensors_m0_mma8451_bb.png?1534866617)

# Python Computer Wiring

Since there's _dozens_ of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, [please visit the guide for CircuitPython on Linux to see whether your platform is supported](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux).&nbsp;

Here's the Raspberry Pi wired with I2C:

- **Pi 3V3** to **sensor VIN**
- **Pi GND** to **sensor GND**
- **Pi SCL** to **sensor SCL**
- **Pi SDA** to **sensor SDA**

![sensors_raspi_mma8451_i2c_bb.jpg](https://cdn-learn.adafruit.com/assets/assets/000/059/497/medium640/sensors_raspi_mma8451_i2c_bb.jpg?1534866843)

Warning: 

# CircuitPython Installation of MMA8451 Library

Next you'll need to install the&nbsp;[Adafruit CircuitPython MMA8451](https://github.com/adafruit/Adafruit_CircuitPython_MMA8451)&nbsp;library on your CircuitPython board.

First make sure you are running the&nbsp;[latest version of Adafruit CircuitPython](../../../../welcome-to-circuitpython/installing-circuitpython)&nbsp;for your board.

Next you'll need to install the necessary libraries&nbsp;to use the hardware--carefully follow the steps to find and install these libraries from&nbsp;[Adafruit's CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle).&nbsp; Our introduction guide has&nbsp;[a great page on how to install the library bundle](../../../../welcome-to-circuitpython/circuitpython-libraries)&nbsp;for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

- **adafruit\_mma8451.mpy**
- **adafruit\_bus\_device**

Before continuing make sure your board's lib folder or root filesystem has the&nbsp; **adafruit\_mma8451.mpy,&nbsp;** and **&nbsp;adafruit\_bus\_device**** &nbsp; **files and folders** &nbsp;**copied over.

Next[&nbsp;connect to the board's serial REPL](https://learn.adafruit.com/welcome-to-circuitpython/the-repl) so you are at the CircuitPython&nbsp; **\>\>\>** &nbsp;prompt.

# Python Installation of MMA8451 Library

You'll need to install the Adafruit\_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. [Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready](https://learn.adafruit.com/circuitpython-on-raspberrypi-linux)!

Once that's done, from your command line run the following command:

- `sudo pip3 install adafruit-circuitpython-mma8451`

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

# CircuitPython & Python Usage

To demonstrate the usage of the sensor we'll initialize it and read the acceleration from the board's Python REPL. &nbsp;Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

```python
import board
import adafruit_mma8451
i2c = board.I2C()
sensor = adafruit_mma8451.MMA8451(i2c)
```

Now you're ready to read values from the sensor using any of these properties:

- **acceleration** &nbsp;- This returns a 3-tuple of X, Y, Z acceleration values in meters per second squared (i.e. 9.8m/s^2 is the force of gravity on the surface of the earth).
- **orientation** &nbsp;-&nbsp;This is a value the MMA8451 calculates to help you understand what orientation the sensor is in, kind of like how a smartphone detects if its landscape or portrait orientation.&nbsp; This will return one of the following values:
  - **adafruit\_mma8451.PL\_PUF:** Portrait, up, front
  - **adafruit\_mma8451.PL\_PUB:** Portrait, up, back
  - **adafruit\_mma8451.PL\_PDF:** Portrait, down, front
  - **adafruit\_mma8451.PL\_PDB:** Portrait, down, back
  - **adafruit\_mma8451.PL\_LRF:** Landscape, right, front
  - **adafruit\_mma8451.PL\_LRB:** Landscape, right, back
  - **adafruit\_mma8451.PL\_LLF:** Landscape, left, front
  - **adafruit\_mma8451.PL\_LLB:** Landscape, left, back

```
x, y, z = sensor.acceleration
print('Acceleration: x={0:0.3f} m/s^2 y={1:0.3f} m/s^2 z={2:0.3f} m/s^2'.format(x, y, z))
orientation = sensor.orientation
print('Orientation: {0}'.format(orientation))
```

![](https://cdn-learn.adafruit.com/assets/assets/000/050/725/medium800/sensors_Screen_Shot_2018-02-05_at_1.56.50_PM.png?1517935576)

In addition there are a few properties you can read and write to change the behavior of the sensor:

- **range&nbsp;** - The range of the accelerometer measurements.&nbsp; This must be a value of:
  - **adafruit\_mma8451.RANGE\_2G:** +/- 2G range
  - **adafruit\_mma8451.RANGE\_4G:** +/- 4G range (the default)
  - **adafruit\_mma8451.RANGE\_8G:** +/- 8G range

- **data\_rate** - The rate at which the sensor measures acceleration data.&nbsp; This must be a value of:
  - **adafruit\_mma8451.DATARATE\_800HZ:** 800hz
  - **adafruit\_mma8451.DATARATE\_400HZ:** 400hz
  - **adafruit\_mma8451.DATARATE\_200HZ:** 200hz
  - **adafruit\_mma8451.DATARATE\_100HZ:** 100hz
  - **adafruit\_mma8451.DATARATE\_50HZ:** 50hz
  - **adafruit\_mma8451.DATARATE\_12\_5HZ:** 12.5hz
  - **adafruit\_mma8451.DATARATE\_6\_25HZ:** 6.25hz
  - **adafruit\_mma8451.DATARATE\_1\_56HZ:** 1.56hz

```
sensor.range = adafruit_mma8451.RANGE_8G
sensor.data_rate = adafruit_mma8451.DATARATE_400HZ
```

![](https://cdn-learn.adafruit.com/assets/assets/000/050/726/medium800/sensors_Screen_Shot_2018-02-05_at_1.58.35_PM.png?1517935584)

That's all there is to using the MMA8451 with CircuitPython!

The following is a complete example that will print the orientation and acceleration of the sensor every second.&nbsp; Save this as **code.py** on your board and open the REPL to see the output.

# Full Example Code
https://github.com/adafruit/Adafruit_CircuitPython_MMA8451/blob/main/examples/mma8451_simpletest.py

# Adafruit MMA8451 Accelerometer Breakout

## Python Docs

# Adafruit MMA8451 Accelerometer Breakout

## Downloads

# Datasheet & Files

- [MMA8451-Q Datasheet](http://www.adafruit.com/datasheets/MMA8451Q-1.pdf)
- [Fritzing object in Adafruit Fritzing library](https://github.com/adafruit/Fritzing-Library)
- [EagleCAD PCB files on GitHub](https://github.com/adafruit/Adafruit-MMA8451-Breakout-PCB)

# Schematics
![](https://cdn-learn.adafruit.com/assets/assets/000/018/513/medium800/sensors_mmasch.png?1406746638)

# Fabrication print
Dimensions are in Inches  
![](https://cdn-learn.adafruit.com/assets/assets/000/018/511/medium800/sensors_mmafabprint.png?1406746599)


## Featured Products

### Adafruit Triple-Axis Accelerometer - ±2/4/8g @ 14-bit - MMA8451

[Adafruit Triple-Axis Accelerometer - ±2/4/8g @ 14-bit - MMA8451](https://www.adafruit.com/product/2019)
So many accelerometers and so little time! We've expanded our accelerometer selection even more with this high-precision and inexpensive **MMA8451 Triple-Axis Accelerometer w/ 14-bit ADC.** You can detect motion, tilt and basic orientation with a digital accelerometer - and...

In Stock
[Buy Now](https://www.adafruit.com/product/2019)
[Related Guides to the Product](https://learn.adafruit.com/products/2019/guides)

## Related Guides

- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [Pro Trinket as a USB HID Mouse](https://learn.adafruit.com/pro-trinket-usb-hid-mouse.md)
- [Talking d20 20-Sided Gaming Die](https://learn.adafruit.com/talking-d20-20-sided-gaming-die.md)
- [Cartoon Network MakeCode: Garnet's Gauntlets from Steven Universe](https://learn.adafruit.com/cartoon-network-makecode-garnets-gauntlets-from-steven-universe.md)
- [Adafruit 1.44" Color TFT with Micro SD Socket](https://learn.adafruit.com/adafruit-1-44-color-tft-with-micro-sd-socket.md)
- [Adafruit Qualia High Res Displayport Desktop Monitor](https://learn.adafruit.com/qualia-high-res-displayport-desktop-monitor.md)
- [Adafruit 2.4" Color TFT Touchscreen Breakout](https://learn.adafruit.com/adafruit-2-4-color-tft-touchscreen-breakout.md)
- [Adafruit APDS9999 Proximity, Lux Light & Color Sensor](https://learn.adafruit.com/adafruit-apds9999-proximity-lux-light-color-sensor.md)
- [Adafruit OLED FeatherWing](https://learn.adafruit.com/adafruit-oled-featherwing.md)
- [Analog Feedback Servos](https://learn.adafruit.com/analog-feedback-servos.md)
- [PM2.5 Air Quality Sensor](https://learn.adafruit.com/pm25-air-quality-sensor.md)
- [How to Scan and Detect I2C Addresses](https://learn.adafruit.com/scanning-i2c-addresses.md)
- [Adafruit CH9328 UART to HID Keyboard Breakout](https://learn.adafruit.com/adafruit-ch9328-uart-to-hid-keyboard-breakout.md)
- [Accessing and Using Adafruit PCB Design Files](https://learn.adafruit.com/accessing-and-using-adafruit-pcb-design-files.md)
- [Wireless LED Juggling Balls with ESP-NOW](https://learn.adafruit.com/wireless-juggling-balls-esp-now.md)
