Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# Simple demo of the LSM9DS0 accelerometer, magnetometer, gyroscope.
5# Will print the acceleration, magnetometer, and gyroscope values every second.
6import time
7
8import board
9import busio
10
11# import digitalio # Used with SPI
12
13import adafruit_lsm9ds0
14
15# I2C connection:
16i2c = busio.I2C(board.SCL, board.SDA)
17sensor = adafruit_lsm9ds0.LSM9DS0_I2C(i2c)
18
19# SPI connection:
20# from digitalio import DigitalInOut, Direction
21# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
22# gcs = DigitalInOut(board.D5)
23# xmcs = DigitalInOut(board.D6)
24# sensor = adafruit_lsm9ds0.LSM9DS0_SPI(spi, xmcs, gcs)
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 (degrees/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)