Simple test¶
Ensure your device works with this simple test.
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
7import board
8import adafruit_mma8451
9
10
11# Create sensor object, communicating over the board's default I2C bus
12i2c = board.I2C() # uses board.SCL and board.SDA
13
14# Initialize MMA8451 module.
15sensor = adafruit_mma8451.MMA8451(i2c)
16# Optionally change the address if it's not the default:
17# sensor = adafruit_mma8451.MMA8451(i2c, address=0x1C)
18
19# Optionally change the range from its default of +/-4G:
20# sensor.range = adafruit_mma8451.RANGE_2G # +/- 2G
21# sensor.range = adafruit_mma8451.RANGE_4G # +/- 4G (default)
22# sensor.range = adafruit_mma8451.RANGE_8G # +/- 8G
23
24# Optionally change the data rate from its default of 800hz:
25# sensor.data_rate = adafruit_mma8451.DATARATE_800HZ # 800Hz (default)
26# sensor.data_rate = adafruit_mma8451.DATARATE_400HZ # 400Hz
27# sensor.data_rate = adafruit_mma8451.DATARATE_200HZ # 200Hz
28# sensor.data_rate = adafruit_mma8451.DATARATE_100HZ # 100Hz
29# sensor.data_rate = adafruit_mma8451.DATARATE_50HZ # 50Hz
30# sensor.data_rate = adafruit_mma8451.DATARATE_12_5HZ # 12.5Hz
31# sensor.data_rate = adafruit_mma8451.DATARATE_6_25HZ # 6.25Hz
32# sensor.data_rate = adafruit_mma8451.DATARATE_1_56HZ # 1.56Hz
33
34# Main loop to print the acceleration and orientation every second.
35while True:
36 x, y, z = sensor.acceleration
37 print(
38 "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)
39 )
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)