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