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
 7import board
 8import adafruit_lsm9ds1
 9
10# Create sensor object, communicating over the board's default I2C bus
11i2c = board.I2C()  # uses board.SCL and board.SDA
12# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
13sensor = adafruit_lsm9ds1.LSM9DS1_I2C(i2c)
14
15# SPI connection:
16# from digitalio import DigitalInOut, Direction
17# spi = board.SPI()
18# csag = DigitalInOut(board.D5)
19# csag.direction = Direction.OUTPUT
20# csag.value = True
21# csm = DigitalInOut(board.D6)
22# csm.direction = Direction.OUTPUT
23# csm.value = True
24# sensor = adafruit_lsm9ds1.LSM9DS1_SPI(spi, csag, csm)
25
26# Main loop will read the acceleration, magnetometer, gyroscope, Temperature
27# values every second and print them out.
28while True:
29    # Read acceleration, magnetometer, gyroscope, temperature.
30    accel_x, accel_y, accel_z = sensor.acceleration
31    mag_x, mag_y, mag_z = sensor.magnetic
32    gyro_x, gyro_y, gyro_z = sensor.gyro
33    temp = sensor.temperature
34    # Print values.
35    print(
36        "Acceleration (m/s^2): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
37            accel_x, accel_y, accel_z
38        )
39    )
40    print(
41        "Magnetometer (gauss): ({0:0.3f},{1:0.3f},{2:0.3f})".format(mag_x, mag_y, mag_z)
42    )
43    print(
44        "Gyroscope (rad/sec): ({0:0.3f},{1:0.3f},{2:0.3f})".format(
45            gyro_x, gyro_y, gyro_z
46        )
47    )
48    print("Temperature: {0:0.3f}C".format(temp))
49    # Delay for a second.
50    time.sleep(1.0)