|
1 |
| -# Accelerometer example. |
2 |
| -# Reads the accelerometer x, y, z values and prints them every tenth of a second. |
3 |
| -# Open the serial port after running to see the output printed. |
4 |
| -# Author: Tony DiCola |
5 | 1 | import time
|
6 | 2 | import board
|
| 3 | +import digitalio |
7 | 4 | import busio
|
8 | 5 | import adafruit_lis3dh
|
9 | 6 |
|
10 |
| - |
11 |
| -# Uncomment _one_ of the hardware setups below depending on your wiring: |
12 |
| - |
13 | 7 | # Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
|
14 | 8 | # otherwise check I2C pins.
|
15 |
| -# pylint: disable=no-member |
16 | 9 | if hasattr(board, 'ACCELEROMETER_SCL'):
|
17 | 10 | i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
|
18 |
| - lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19) |
| 11 | + int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT) |
| 12 | + lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1) |
19 | 13 | else:
|
20 | 14 | i2c = busio.I2C(board.SCL, board.SDA)
|
21 |
| - lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) |
22 |
| - |
23 |
| -# Software I2C setup: |
24 |
| -#import bitbangio |
25 |
| -#i2c = bitbangio.I2C(board.SCL, board.SDA) |
26 |
| -#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c) |
| 15 | + int1 = digitalio.DigitalInOut(board.D9) # Set this to the correct pin for the interrupt! |
| 16 | + lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) |
27 | 17 |
|
28 | 18 | # Hardware SPI setup:
|
29 |
| -#import busio |
30 |
| -#spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
31 |
| -#cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin! |
32 |
| -#lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs) |
| 19 | +# spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 20 | +# cs = digitalio.DigitalInOut(board.D9) # Set to appropriate CS pin! |
| 21 | +# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs) |
33 | 22 |
|
34 | 23 |
|
35 | 24 | # Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
|
|
40 | 29 | # Read accelerometer values (in m / s ^ 2). Returns a 3-tuple of x, y,
|
41 | 30 | # z axis values. Divide them by 9.806 to convert to Gs.
|
42 | 31 | x, y, z = [value / adafruit_lis3dh.STANDARD_GRAVITY for value in lis3dh.acceleration]
|
43 |
| - print('x = {}G, y = {}G, z = {}G'.format(x, y, z)) |
| 32 | + print("x = %0.3f G, y = %0.3f G, z = %0.3f G" % (x, y, z)) |
44 | 33 | # Small delay to keep things responsive but give time for interrupt processing.
|
45 | 34 | time.sleep(0.1)
|
0 commit comments