/ examples / lsm9ds1_simpletest.py
lsm9ds1_simpletest.py
 1  # Simple demo of the LSM9DS1 accelerometer, magnetometer, gyroscope.
 2  # Will print the acceleration, magnetometer, and gyroscope values every second.
 3  import time
 4  import board
 5  import busio
 6  import adafruit_lsm9ds1
 7  
 8  # I2C connection:
 9  i2c = busio.I2C(board.SCL, board.SDA)
10  sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
11  
12  # SPI connection:
13  # from digitalio import DigitalInOut, Direction
14  # spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
15  # csag = DigitalInOut(board.D5)
16  # csag.direction = Direction.OUTPUT
17  # csag.value = True
18  # csm = DigitalInOut(board.D6)
19  # csm.direction = Direction.OUTPUT
20  # csm.value = True
21  # sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)
22  
23  # Main loop will read the acceleration, magnetometer, gyroscope, Temperature
24  # values every second and print them out.
25  while True:
26      # Read acceleration, magnetometer, gyroscope, temperature.
27      accel_x, accel_y, accel_z = sensor.acceleration
28      mag_x, mag_y, mag_z = sensor.magnetic
29      gyro_x, gyro_y, gyro_z = sensor.gyro
30      temp = sensor.temperature
31      # Print values.
32      print(
33          "Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
34              accel_x, accel_y, accel_z
35          )
36      )
37      print(
38          "Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})".format(mag_x, mag_y, mag_z)
39      )
40      print(
41          "Gyroscope (degrees/sec): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
42              gyro_x, gyro_y, gyro_z
43          )
44      )
45      print("Temperature: {0:0.3f}C".format(temp))
46      # Delay for a second.
47      time.sleep(1.0)