Simple test
Ensure your device works with this simple test.
examples/mma8451_simpletest.py
1# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple demo of reading the MMA8451 orientation every second.
5
6import time
7
8import board
9
10import adafruit_mma8451
11
12# Create sensor object, communicating over the board's default I2C bus
13i2c = board.I2C() # uses board.SCL and board.SDA
14# i2c = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller
15
16# Initialize MMA8451 module.
17sensor = adafruit_mma8451.MMA8451(i2c)
18# Optionally change the address if it's not the default:
19# sensor = adafruit_mma8451.MMA8451(i2c, address=0x1C)
20
21# Optionally change the range from its default of +/-4G:
22# sensor.range = adafruit_mma8451.RANGE_2G # +/- 2G
23# sensor.range = adafruit_mma8451.RANGE_4G # +/- 4G (default)
24# sensor.range = adafruit_mma8451.RANGE_8G # +/- 8G
25
26# Optionally change the data rate from its default of 800hz:
27# sensor.data_rate = adafruit_mma8451.DATARATE_800HZ # 800Hz (default)
28# sensor.data_rate = adafruit_mma8451.DATARATE_400HZ # 400Hz
29# sensor.data_rate = adafruit_mma8451.DATARATE_200HZ # 200Hz
30# sensor.data_rate = adafruit_mma8451.DATARATE_100HZ # 100Hz
31# sensor.data_rate = adafruit_mma8451.DATARATE_50HZ # 50Hz
32# sensor.data_rate = adafruit_mma8451.DATARATE_12_5HZ # 12.5Hz
33# sensor.data_rate = adafruit_mma8451.DATARATE_6_25HZ # 6.25Hz
34# sensor.data_rate = adafruit_mma8451.DATARATE_1_56HZ # 1.56Hz
35
36# Main loop to print the acceleration and orientation every second.
37while True:
38 x, y, z = sensor.acceleration
39 print(f"Acceleration: x={x:0.3f}m/s^2 y={y:0.3f}m/s^2 z={z:0.3f}m/s^2")
40 orientation = sensor.orientation
41 # Orientation is one of these values:
42 # - PL_PUF: Portrait, up, front
43 # - PL_PUB: Portrait, up, back
44 # - PL_PDF: Portrait, down, front
45 # - PL_PDB: Portrait, down, back
46 # - PL_LRF: Landscape, right, front
47 # - PL_LRB: Landscape, right, back
48 # - PL_LLF: Landscape, left, front
49 # - PL_LLB: Landscape, left, back
50 print("Orientation: ", end="")
51 if orientation == adafruit_mma8451.PL_PUF:
52 print("Portrait, up, front")
53 elif orientation == adafruit_mma8451.PL_PUB:
54 print("Portrait, up, back")
55 elif orientation == adafruit_mma8451.PL_PDF:
56 print("Portrait, down, front")
57 elif orientation == adafruit_mma8451.PL_PDB:
58 print("Portrait, down, back")
59 elif orientation == adafruit_mma8451.PL_LRF:
60 print("Landscape, right, front")
61 elif orientation == adafruit_mma8451.PL_LRB:
62 print("Landscape, right, back")
63 elif orientation == adafruit_mma8451.PL_LLF:
64 print("Landscape, left, front")
65 elif orientation == adafruit_mma8451.PL_LLB:
66 print("Landscape, left, back")
67 time.sleep(1.0)
DisplayIO Simpletest
This is a simple test for boards with built-in display.
examples/mma8451_displayio_simpletest.py
1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
3#
4# SPDX-License-Identifier: MIT
5
6import time
7
8import board
9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_mma8451
14
15# Simple demo of using the built-in display.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C() # uses board.SCL and board.SDA
20sensor = adafruit_mma8451.MMA8451(i2c)
21
22# Create Label(s) to show the readings. If you have a very small
23# display you may need to change to scale=1.
24display_output_label = Label(FONT, text="", scale=2)
25
26# place the label(s) in the middle of the screen with anchored positioning
27display_output_label.anchor_point = (0, 0)
28display_output_label.anchored_position = (
29 4,
30 board.DISPLAY.height // 2 - 60,
31)
32
33# add the label(s) to the main_group
34main_group.append(display_output_label)
35
36# set the main_group as the root_group of the built-in DISPLAY
37board.DISPLAY.root_group = main_group
38
39# begin main loop
40while True:
41 # update the text of the label(s) to show the sensor readings
42 x, y, z = sensor.acceleration
43 display_output_label.text = f"Acceleration:\nx={x:0.3f}m/s^2\ny={y:0.3f}m/s^2\nz={z:0.3f}m/s^2"
44 # wait for a bit
45 time.sleep(0.5)