Simple test

Ensure your device works with this simple test.

examples/lsm9ds1_simpletest.py
 1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Simple demo of the LSM9DS1 accelerometer, magnetometer, gyroscope.
 5# Will print the acceleration, magnetometer, and gyroscope values every second.
 6import time
 7
 8import board
 9
10import adafruit_lsm9ds1
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
15sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
16
17# SPI connection:
18# from digitalio import DigitalInOut, Direction
19# spi = board.SPI()
20# csag = DigitalInOut(board.D5)
21# csag.direction = Direction.OUTPUT
22# csag.value = True
23# csm = DigitalInOut(board.D6)
24# csm.direction = Direction.OUTPUT
25# csm.value = True
26# sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)
27
28# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
29# values every second and print them out.
30while True:
31    # Read acceleration, magnetometer, gyroscope, temperature.
32    accel_x, accel_y, accel_z = sensor.acceleration
33    mag_x, mag_y, mag_z = sensor.magnetic
34    gyro_x, gyro_y, gyro_z = sensor.gyro
35    temp = sensor.temperature
36    # Print values.
37    print(f"Acceleration (m/s^2): ({accel_x:0.3f},{accel_y:0.3f},{accel_z:0.3f})")
38    print(f"Magnetometer (gauss): ({mag_x:0.3f},{mag_y:0.3f},{mag_z:0.3f})")
39    print(f"Gyroscope (rad/sec): ({gyro_x:0.3f},{gyro_y:0.3f},{gyro_z:0.3f})")
40    print(f"Temperature: {temp:0.3f}C")
41    # Delay for a second.
42    time.sleep(1.0)