-
Notifications
You must be signed in to change notification settings - Fork 1
/
Magnetometer_Accelerometer_Test.py
41 lines (32 loc) · 1.16 KB
/
Magnetometer_Accelerometer_Test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Make sure the following libraries are installed:
# sudo pip3 install RPI.GPIO
# sudo pip3 install adafruit-blinka
# sudo pip3 install adafruit-circuitpython-lsm303-accel
# For LSM303AGR:
# sudo pip3 install adafruit-circuitpython-lis2mdl
# For LSM303DLH:
# sudo pip3 install adafruit-circuitpython-lsm303dlh-mag
import board
import busio
import adafruit_lsm303_accel
#for LSM303AGR
import adafruit_lis2mdl
#for LSM303DLH
#import adafruit_lsm303dlh_mag
#Set up I2C link
i2c = busio.I2C(board.SCL, board.SDA)
#Set up link to accelerometer
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
#Set up link to magnetometer
#for LSM303AGR
mag = adafruit_lis2mdl.LIS2MDL(i2c)
#for LSM303DLH
#mag = adafruit_lsm303dlh_mag.LSM303DLH_Mag(i2c)
#Print values
print ("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
print ("Magnetometer (uTeslas): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic)
while True:
# accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
mag = adafruit_lis2mdl.LIS2MDL(i2c)
# print ("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
print ("Magnetometer (uTeslas): X=%0.3f \t Y=%0.3f \t Z=%0.3fi \t"%mag.magnetic)