This library is archived and no longer supported

This library has been split into separate libararies for the magnetometer and accelerometer. The accelerometer code will be shared with another version of the LSM303 that uses the same accelerometer but not the magnetometer and this repo will be archived.

This library will no longer be supported. Please us the new libraries

The new, split libraries

https://github.com/adafruit/Adafruit_CircuitPython_LSM303_Accel

https://github.com/adafruit/Adafruit_CircuitPython_LSM303DLH_Mag

The library for the new magnetometer

https://github.com/adafruit/Adafruit_CircuitPython_LSM303AGR_Mag

You can find usage information for the new libraries in the sensor’s guide:

https://learn.adafruit.com/lsm303-accelerometer-slash-compass-breakout/python-circuitpython

Introduction

Documentation Status Discord Build Status

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.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-lsm303

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-lsm303

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install adafruit-circuitpython-lsm303

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.

Documentation

For information on building library documentation, please check out this guide.

Table of Contents

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)

adafruit_lsm303

CircuitPython driver for the LSM303 accelerometer + magnetometer.

  • Author(s): Dave Astels

Implementation Notes

Hardware:

Software and Dependencies:

class adafruit_lsm303.LSM303(i2c)[source]

Driver for the LSM303 accelerometer/magnetometer.

property acceleration

The processed accelerometer sensor values. A 3-tuple of X, Y, Z axis values in meters per second squared that are signed floats.

property mag_gain

The magnetometer’s gain.

property mag_rate

The magnetometer update rate.

property magnetic

The processed magnetometer sensor values. A 3-tuple of X, Y, Z axis values in microteslas that are signed floats.

property raw_acceleration

The raw accelerometer sensor values. A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.

property raw_magnetic

The raw magnetometer sensor values. A 3-tuple of X, Y, Z axis values that are 16-bit signed integers.

Indices and tables