simpletest.py
1 # Simple demo of reading the MMA8451 orientation every second. 2 # Author: Tony DiCola 3 import time 4 5 import board 6 import busio 7 8 import adafruit_mma8451 9 10 11 # Initialize I2C bus. 12 i2c = busio.I2C(board.SCL, board.SDA) 13 14 # Initialize MMA8451 module. 15 sensor = 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. 35 while True: 36 x, y, z = sensor.acceleration 37 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)) 38 orientation = sensor.orientation 39 # Orientation is one of these values: 40 # - PL_PUF: Portrait, up, front 41 # - PL_PUB: Portrait, up, back 42 # - PL_PDF: Portrait, down, front 43 # - PL_PDB: Portrait, down, back 44 # - PL_LRF: Landscape, right, front 45 # - PL_LRB: Landscape, right, back 46 # - PL_LLF: Landscape, left, front 47 # - PL_LLB: Landscape, left, back 48 print('Orientation: ', end='') 49 if orientation == adafruit_mma8451.PL_PUF: 50 print('Portrait, up, front') 51 elif orientation == adafruit_mma8451.PL_PUB: 52 print('Portrait, up, back') 53 elif orientation == adafruit_mma8451.PL_PDF: 54 print('Portrait, down, front') 55 elif orientation == adafruit_mma8451.PL_PDB: 56 print('Portrait, down, back') 57 elif orientation == adafruit_mma8451.PL_LRF: 58 print('Landscape, right, front') 59 elif orientation == adafruit_mma8451.PL_LRB: 60 print('Landscape, right, back') 61 elif orientation == adafruit_mma8451.PL_LLF: 62 print('Landscape, left, front') 63 elif orientation == adafruit_mma8451.PL_LLB: 64 print('Landscape, left, back') 65 time.sleep(1.0)