Simple tests

Ensure your device works with these simple tests.

examples/lsm303_simpletest.py
 1""" Display both accelerometer and magnetometer data once per second """
 2
 3import time
 4import board
 5import busio
 6import adafruit_lsm303
 7
 8i2c = busio.I2C(board.SCL, board.SDA)
 9sensor = adafruit_lsm303.LSM303(i2c)
10
11while True:
12    acc_x, acc_y, acc_z = sensor.acceleration
13    mag_x, mag_y, mag_z = sensor.magnetic
14
15    print('Acceleration (m/s^2): ({0:10.3f}, {1:10.3f}, {2:10.3f})'.format(acc_x, acc_y, acc_z))
16    print('Magnetometer (gauss): ({0:10.3f}, {1:10.3f}, {2:10.3f})'.format(mag_x, mag_y, mag_z))
17    print('')
18    time.sleep(1.0)
examples/lsm303_fast_accel.py
 1""" Read data from the accelerometer and print it out, ASAP! """
 2
 3import board
 4import busio
 5
 6import adafruit_lsm303
 7
 8i2c = busio.I2C(board.SCL, board.SDA)
 9sensor = adafruit_lsm303.LSM303(i2c)
10
11while True:
12    accel_x, accel_y, accel_z = sensor.acceleration
13    print('{0:10.3f} {1:10.3f} {2:10.3f}'.format(accel_x, accel_y, accel_z))
examples/lsm303_fast_mag.py
 1""" Read data from the magnetometer and print it out, ASAP! """
 2
 3import board
 4import busio
 5import adafruit_lsm303
 6
 7i2c = busio.I2C(board.SCL, board.SDA)
 8sensor = adafruit_lsm303.LSM303(i2c)
 9
10while True:
11    mag_x, mag_y, mag_z = sensor.magnetic
12    print('{0:10.3f} {1:10.3f} {2:10.3f}'.format(mag_x, mag_y, mag_z))
examples/lsm303_raw_and_cooked.py
 1""" Display both accelerometer and magnetometer data once per second """
 2
 3import time
 4import board
 5import busio
 6
 7import adafruit_lsm303
 8
 9i2c = busio.I2C(board.SCL, board.SDA)
10sensor = adafruit_lsm303.LSM303(i2c)
11
12while True:
13    raw_accel_x, raw_accel_y, raw_accel_z = sensor.raw_acceleration
14    accel_x, accel_y, accel_z = sensor.acceleration
15    raw_mag_x, raw_mag_y, raw_mag_z = sensor.raw_magnetic
16    mag_x, mag_y, mag_z = sensor.magnetic
17
18    print('Acceleration raw: ({0:6d}, {1:6d}, {2:6d}), (m/s^2): ({3:10.3f}, {4:10.3f}, {5:10.3f})'
19          .format(raw_accel_x, raw_accel_y, raw_accel_z, accel_x, accel_y, accel_z))
20    print('Magnetometer raw: ({0:6d}, {1:6d}, {2:6d}), (gauss): ({3:10.3f}, {4:10.3f}, {5:10.3f})'
21          .format(raw_mag_x, raw_mag_y, raw_mag_z, mag_x, mag_y, mag_z))
22    print('')
23    time.sleep(1.0)