def read(self): #Set up link to magnetometer #for LSM303AGR mag = adafruit_lis2mdl.LIS2MDL(self.i2c) #for LSM303DLH #mag = adafruit_lsm303dlh_mag.LSM303DLH_Mag(self.i2c) return mag.magnetic
# SPDX-FileCopyrightText: 2019 Bryan Siepert for Adafruit Industries # # SPDX-License-Identifier: MIT from time import sleep import board import busio import adafruit_lsm303_accel import adafruit_lis2mdl i2c = busio.I2C(board.SCL, board.SDA) mag = adafruit_lis2mdl.LIS2MDL(i2c) accel = adafruit_lsm303_accel.LSM303_Accel(i2c) while True: print("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration) print("Magnetometer (micro-Teslas)): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic) print("") sleep(0.5)
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ Display magnetometer data once per second """ import time import board import busio import adafruit_lis2mdl i2c = busio.I2C(board.SCL, board.SDA) sensor = adafruit_lis2mdl.LIS2MDL(i2c) while True: mag_x, mag_y, mag_z = sensor.magnetic print("X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format( mag_x, mag_y, mag_z)) print("") time.sleep(1.0)
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT """ Calibrate the magnetometer and print out the hard-iron calibrations """ import time import board import busio import adafruit_lis2mdl i2c = busio.I2C(board.SCL, board.SDA) magnetometer = adafruit_lis2mdl.LIS2MDL(i2c) # calibration for magnetometer X (min, max), Y and Z hardiron_calibration = [[1000, -1000], [1000, -1000], [1000, -1000]] def calibrate(): start_time = time.monotonic() # Update the high and low extremes while time.monotonic() - start_time < 10.0: magval = magnetometer.magnetic print("Calibrating - X:{0:10.2f}, Y:{1:10.2f}, Z:{2:10.2f} uT".format(*magval)) for i, axis in enumerate(magval): hardiron_calibration[i][0] = min(hardiron_calibration[i][0], axis) hardiron_calibration[i][1] = max(hardiron_calibration[i][1], axis) print("Calibration complete:") print("hardiron_calibration =", hardiron_calibration)
#Author: Jesse Rosart-Brodnitz #Contact: [email protected] #function to read magnetometer over I2C #returns unfiltered xyz #address is 0x1e #requires adafruit_lis2mdl libbrary import time import board import busio import adafruit_lis2mdl i2c=busio.I2C(board.SCL, board.SDA) #reads from i2c ports sensor=adafruit_lis2mdl.LIS2MDL(i2c) #declares device within library and means of connection def magRead():#callable function to return raw magnet readings mag_x, mag_y, mag_z = sensor.magnetic#reads from sensor mag=[mag_x, mag_y, mag_z]#assings to array for storage return(mag)# if __name__ == "__main__": while True: magXYZ=magRead(); print("x: {0:2.2f}, y: {1:2.2f}, z: {2:2.2f} uTesla".format(magXYZ[0],magXYZ[1],magXYZ[2])) time.sleep(.5)