Home CodeCircuitpython Raspberry Pi Pico and MMA8451Q accelerometer circuitpython example

Raspberry Pi Pico and MMA8451Q accelerometer circuitpython example

by rp2040guy71

In this article we connect a MMA8451Q accelerometer to a Raspberry Pi Pico running Circuitpython

Sensor Information

The MMA8451Q is a smart, low-power, three-axis, capacitive, micromachined accelerometer with 14 bits of resolution. This accelerometer is packed with embedded functions with flexible user programmable options, configurable to two interrupt pins.

Embedded interrupt functions allow for overall power savings relieving the host processor from continuously polling data.

There is access to both low-pass filtered data as well as high-pass filtered data, which minimizes the data analysis required for jolt detection and faster transitions.

The device can be configured to generate inertial wakeup interrupt signals from any combination of the configurable embedded functions allowing the MMA8451Q to monitor events and remain in a low-power mode during periods of inactivity.

Features

• 1.95 V to 3.6 V supply voltage
• 1.6 V to 3.6 V interface voltage
• ±2 g/±4 g/±8 g dynamically selectable full scale
• Output data rates (ODR) from 1.56 Hz to 800 Hz
• 99 μg/√Hz noise
• 14-bit and 8-bit digital output
• I2C digital output interface
• Two programmable interrupt pins for seven interrupt sources
• Three embedded channels of motion detection
— Freefall or motion detection: one channel
— Pulse detection: one channel
— Jolt detection: one channel
• Orientation (portrait/landscape) detection with programmable hysteresis
• Automatic ODR change for auto-wake and return to sleep
• 32-sample FIFO
• High-pass filter data available per sample and through the FIFO
• Self-test
• Current consumption: 6 μA to 165 μA

Parts Required

 

Name Link
Pico Raspberry Pi Pico Development Board
MMA8451 MMA8451 Digital Triaxial Accelerometer High-precision Inclination Module
Connecting cables Aliexpress link

Amazon.com link

Ebay link

 

Schematic/Connection

Black for GND
Red for V+
Blue for SDA
Yellow for SCL

So color coded for ease of use, this layout shows a connection to the module

rp2040 and mma8451

rp2040 and mma8451

Code Example

I used Thonny for development

The following is based on a library , I copied the adafruit_mma8451.mpy library for this device to the lib folder on my Raspberry Pi Pico – https://circuitpython.org/libraries

This is the basic example which comes with the library

import time
import board
import adafruit_mma8451
import busio
 
i2c = busio.I2C(scl=board.GP1, sda=board.GP0) # uses board.SCL and board.SDA
 
sensor = adafruit_mma8451.MMA8451(i2c, address=0x1C)
 
# Main loop to print the acceleration and orientation every second.
while True:
    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
    # Orientation is one of these values:
    #  - PL_PUF: Portrait, up, front
    #  - PL_PUB: Portrait, up, back
    #  - PL_PDF: Portrait, down, front
    #  - PL_PDB: Portrait, down, back
    #  - PL_LRF: Landscape, right, front
    #  - PL_LRB: Landscape, right, back
    #  - PL_LLF: Landscape, left, front
    #  - PL_LLB: Landscape, left, back
    print("Orientation: ", end="")
    if orientation == adafruit_mma8451.PL_PUF:
        print("Portrait, up, front")
    elif orientation == adafruit_mma8451.PL_PUB:
        print("Portrait, up, back")
    elif orientation == adafruit_mma8451.PL_PDF:
        print("Portrait, down, front")
    elif orientation == adafruit_mma8451.PL_PDB:
        print("Portrait, down, back")
    elif orientation == adafruit_mma8451.PL_LRF:
        print("Landscape, right, front")
    elif orientation == adafruit_mma8451.PL_LRB:
        print("Landscape, right, back")
    elif orientation == adafruit_mma8451.PL_LLF:
        print("Landscape, left, front")
    elif orientation == adafruit_mma8451.PL_LLB:
        print("Landscape, left, back")
    time.sleep(1.0)

Output

Here is what I saw in Thonny REPL window

Acceleration: x=-1.470m/s^2 y=6.742m/s^2 z=6.311m/s^2
Orientation: Portrait, down, front
Acceleration: x=1.839m/s^2 y=7.034m/s^2 z=-6.416m/s^2
Orientation: Portrait, down, back
Acceleration: x=9.840m/s^2 y=1.140m/s^2 z=2.442m/s^2
Orientation: Landscape, right, front
Acceleration: x=3.170m/s^2 y=4.707m/s^2 z=-7.853m/s^2
Orientation: Portrait, down, back
Acceleration: x=1.523m/s^2 y=6.713m/s^2 z=-7.072m/s^2
Orientation: Portrait, down, back

Links

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More