Introduction¶
Adafruit CircuitPython module for the LSM303 6-DoF with 3-axis accelerometer and magnetometer
Dependencies¶
This driver depends on:
Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.
Usage Example¶
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)
Contributing¶
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.
Building locally¶
To build this library locally you’ll need to install the circuitpython-build-tools package.
python3 -m venv .env
source .env/bin/activate
pip install circuitpython-build-tools
Once installed, make sure you are in the virtual environment:
source .env/bin/activate
Then run the build:
circuitpython-build-bundles --filename_prefix adafruit-circuitpython-lsm303 --library_location .
Sphinx documentation¶
Sphinx is used to build the documentation based on rST files and comments in the code. First, install dependencies (feel free to reuse the virtual environment from above):
python3 -m venv .env
source .env/bin/activate
pip install Sphinx sphinx-rtd-theme
Now, once you have the virtual environment activated:
cd docs
sphinx-build -E -W -b html . _build/html
This will output the documentation to docs/_build/html
. Open the index.html in your browser to
view them. It will also (due to -W) error out on any warning like Travis will. This is a good way to
locally verify it will pass.
Table of Contents¶
Simple tests¶
Ensure your device works with these simple tests.
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)
|
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))
|
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))
|
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)
|
adafruit_lsm303
¶
CircuitPython driver for the LSM303 accelerometer + magnetometer.
- Author(s): Dave Astels
Implementation Notes¶
Hardware:
- Adafruit Triple-axis Accelerometer+Magnetometer (Compass) Board - LSM303 (Product ID: 1120)
- Adafruit FLORA Accelerometer/Compass Sensor - LSM303 - v1.0 (Product ID: 1247)
Software and Dependencies:
- Adafruit CircuitPython firmware for the ESP8622 and M0-based boards: https://github.com/adafruit/circuitpython/releases
- Adafruit’s Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
-
class
adafruit_lsm303.
LSM303
(i2c)[source]¶ Driver for the LSM303 accelerometer/magnetometer.
-
acceleration
¶ The processed accelerometer sensor values. A 3-tuple of X, Y, Z axis values in meters per second squared that are signed floats.
-
mag_gain
¶ The magnetometer’s gain.
-
mag_rate
¶ The magnetometer update rate.
-
magnetic
¶ The processed magnetometer sensor values. A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.
-
raw_acceleration
¶ The raw accelerometer sensor values. A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.
-
raw_magnetic
¶ The raw magnetometer sensor values. A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.
-