Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rljohnsn/4cc860cfc3b93c90107a6f4ab1359040 to your computer and use it in GitHub Desktop.
Save rljohnsn/4cc860cfc3b93c90107a6f4ab1359040 to your computer and use it in GitHub Desktop.
Python sample to get accel/magno/gyro readings off of LIS3MDL and LSM6DS
# combined two sample scripts, one for each sensor, into a single example
import time
import board
import busio
import adafruit_lis3mdl
from os import system
i2c = busio.I2C(board.SCL, board.SDA)
sensor1 = adafruit_lis3mdl.LIS3MDL(i2c)
# pylint:disable=no-member
from adafruit_lsm6ds import Rate, AccelRange, GyroRange
# from adafruit_lsm6ds.lsm6dsox import LSM6DSOX as LSM6DS
from adafruit_lsm6ds.lsm6ds33 import LSM6DS33 as LSM6DS
# from adafruit_lsm6ds.lsm6dso32 import LSM6DSO32 as LSM6DS
# from adafruit_lsm6ds.ism330dhcx import ISM330DHCX as LSM6DS
sensor = LSM6DS(i2c)
sensor.accelerometer_range = AccelRange.RANGE_8G
sensor.gyro_range = GyroRange.RANGE_2000_DPS
sensor.accelerometer_data_rate = Rate.RATE_1_66K_HZ
# sensor.accelerometer_data_rate = Rate.RATE_12_5_HZ
sensor.gyro_data_rate = Rate.RATE_1_66K_HZ
while True:
system('clear')
print("Accelerometer range set to: %d G" % AccelRange.string[sensor.accelerometer_range])
print("Accelerometer rate set to: %d HZ" % Rate.string[sensor.accelerometer_data_rate])
print("Gyro range set to: %d DPS" % GyroRange.string[sensor.gyro_range])
print("Gyro rate set to: %d HZ" % Rate.string[sensor.gyro_data_rate])
print("Accel X:%10.2f Y:%10.2f Z:%10.2f ms^2" % (sensor.acceleration))
print("Gyro X:%10.2f Y:%10.2f Z:%10.2f radians/s" % (sensor.gyro))
mag_x, mag_y, mag_z = sensor1.magnetic
print('Magnetic X:{0:10.2f} Y:{1:10.2f} Z:{2:10.2f} uT'.format(mag_x, mag_y, mag_z))
print('')
time.sleep(0.50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment