lsm9ds0_simpletest.py
1 # Simple demo of the LSM9DS0 accelerometer, magnetometer, gyroscope. 2 # Will print the acceleration, magnetometer, and gyroscope values every second. 3 import time 4 5 import board 6 import busio 7 8 # import digitalio # Used with SPI 9 10 import adafruit_lsm9ds0 11 12 # I2C connection: 13 i2c = busio.I2C(board.SCL, board.SDA) 14 sensor = adafruit_lsm9ds0.LSM9DS0_I2C(i2c) 15 16 # SPI connection: 17 # from digitalio import DigitalInOut, Direction 18 # spi = busio.SPI(board.SCK, board.MOSI, board.MISO) 19 # gcs = DigitalInOut(board.D5) 20 # xmcs = DigitalInOut(board.D6) 21 # sensor = adafruit_lsm9ds0.LSM9DS0_SPI(spi, xmcs, gcs) 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)