It's easy to use the LSM303 sensor with CircuitPython and the Adafruit CircuitPython LSM303 Accelerometer and Adafruit CircuitPython LIS2MDL or Adafruit CircuitPython LSM303DLH Magnetometer libraries.  These libraries allow you to easily write Python code that reads the accelerometer and magnetometer values from the sensor.

You can use this sensor with any CircuitPython microcontroller board or with a computer that has GPIO and Python thanks to Adafruit_Blinka, our CircuitPython-for-Python compatibility library.

Make sure you know which version of the sensor you have before continuing

CircuitPython Microcontroller Wiring

First wire up a LSM303 to your board exactly as shown on the previous pages for Arduino using an I2C connection.  Here's an example of wiring a Feather M0 to the sensor with I2C:

  • Board 3V to sensor VIN (red wire)
  • Board GND to sensor GND (black wire)
  • Board SCL to sensor SCL (yellow wire)
  • Board SDA to sensor SDA (blue wire)
  •  

Python Computer Wiring

Since there's dozens of Linux computers/boards you can use we will show wiring for Raspberry Pi. For other platforms, please visit the guide for CircuitPython on Linux to see whether your platform is supported

Here's the Raspberry Pi wired with I2C:

  • Pi 3V to sensor VIN (red wire)
  • Pi GND to sensor GND (black wire)
  • Pi SCL to sensor SCL (yellow wire)
  • Pi SDA to sensor SDA (blue wire)

CircuitPython Installation of LSM303 Library

Next you'll need to instal the Adafruit CircuitPython LSM303 accelerometer and Adafruit CircuitPython LIS2MDL  or Adafruit CircuitPython LSM303DLH magnetometer (depending on which LSM303 you have) libraries on your CircuitPython board.  

First make sure you are running the latest version of Adafruit CircuitPython for your board.

Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle.  Our introduction guide has a great page on how to install the library bundle for both express and non-express boards.

Remember for non-express boards like the, you'll need to manually install the necessary libraries from the bundle:

  • adafruit_lis2mdl.mpy or adafruit_lsm303dlh_mag.mpy
  • adafruit_lsm303_accel.mpy
  • adafruit_bus_device
  • adafruit_register

Before continuing make sure your board's lib folder or root filesystem has the adafruit_lis2mdl.mpy or adafruit_lsm303dlh_mag.mpy, adafruit_lsm303_accel.mpy, adafruit_register, and adafruit_bus_device files and folders copied over.

Next connect to the board's serial REPL so you are at the CircuitPython >>> prompt.

Why LIS2MDL? That's the model number of the magnetometer included in the LSM303AGR

Python Installation of LSM303 Libraries

You'll need to install the Adafruit_Blinka library that provides the CircuitPython support in Python. This may also require enabling I2C on your platform and verifying you are running Python 3. Since each platform is a little different, and Linux changes often, please visit the CircuitPython on Linux guide to get your computer ready!

Once that's done, from your command line run the following command:

  • sudo pip3 install adafruit-circuitpython-lsm303-accel

Then install the magnetometer library for your version of the LSM303:

LSM303AGR:

  • sudo pip3 install adafruit-circuitpython-lis2mdl

LSM303DLH:

  • sudo pip3 install adafruit-circuitpython-lsm303dlh-mag

If your default Python is version 3 you may need to run 'pip' instead. Just make sure you aren't trying to use CircuitPython on Python 2.x, it isn't supported!

CircuitPython & Python Usage

These examples use the adafruit_lis2mdl library. If you have a lsm303dlh you'll have to change the code to use the adafruit_lsm303dlh_mag library

To demonstrate the usage of the sensor we'll initialize it and read the accelerometer and compass/magnetometer from the board's Python REPL.

Run the following code to import the necessary modules and initialize the I2C connection with the sensor:

LSM303AGR

import board
import adafruit_lsm303_accel
import adafruit_lis2mdl

i2c = board.I2C()
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
mag = adafruit_lis2mdl.LIS2MDL(i2c)

LSM303DLH

import board
import adafruit_lsm303_accel
import adafruit_lsm303dlh_mag

i2c = board.I2C()
mag = adafruit_lsm303dlh_mag.LSM303DLH_Mag(i2c)
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)

Now you're ready to read values from the sensor using one of these properties:

Accelerometer

  • acceleration - A 3-tuple of X, Y, Z acceleration values in meters per second per second (m/s^2).

Magnetometer

  • magnetic - A 3-tuple of the X, Y, Z magnetometer readings in micro-Teslas.
print("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
print("Magnetometer (micro-Teslas)): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic)

Tap Detection

The LSM303's accelerometer can also detect single or double taps!

 

The set_tap method can be used to set the number of taps (1 or 2) to detect, as well as setting a tap threshold. The lower the threshold, the more sensitive the accelerometer will be to detecting taps. You will likely have to play around with this number to find a value that suits your needs

Here we're also going to use the range property to change the measurement range of the accelerometer to +/-8G

accel.range = adafruit_lsm303_accel.Range.RANGE_8G
accel.set_tap(1, 30)
while True:
    if accel.tapped:
        print("Tapped!\n")

When you type code above into the REPL and give the sensor a nice solid tap, you should see something similar to the output below. If you don't see the "Tapped!" message, you may need to adjust the threshold.

As you can see from the above, the tapped property will return True over a number of cycles for a single tap event. You will have to account for this in your code. Unfortunately this just seems to be a quirk of the sensor

That's all there is to using the LSM303 with CircuitPython!

Below is a complete example of reading the sensor and printing its values every second.  Save this as code.py on your board and open the REPL to see the output.

Tap Detection Code

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import board
import adafruit_lsm303_accel

i2c = board.I2C()  # uses board.SCL and board.SDA
# i2c = board.STEMMA_I2C()  # For using the built-in STEMMA QT connector on a microcontroller
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)
accel.range = adafruit_lsm303_accel.Range.RANGE_8G
accel.set_tap(1, 30)

while True:
    if accel.tapped:
        print("Tapped!\n")

LSM303AGR Example Code

# SPDX-FileCopyrightText: 2019 Bryan Siepert for Adafruit Industries
#
# SPDX-License-Identifier: MIT

from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lis2mdl

i2c = busio.I2C(board.SCL, board.SDA)
mag = adafruit_lis2mdl.LIS2MDL(i2c)
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)

while True:
    print("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
    print("Magnetometer (micro-Teslas)): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic)
    print("")
    sleep(0.5)

LSM303DLH Example Code

# SPDX-FileCopyrightText: 2019 Bryan Siepert for Adafruit Industries
#
# SPDX-License-Identifier: MIT

from time import sleep
import board
import busio
import adafruit_lsm303_accel
import adafruit_lsm303dlh_mag

i2c = busio.I2C(board.SCL, board.SDA)
mag = adafruit_lsm303dlh_mag.LSM303DLH_Mag(i2c)
accel = adafruit_lsm303_accel.LSM303_Accel(i2c)

while True:
    print("Acceleration (m/s^2): X=%0.3f Y=%0.3f Z=%0.3f"%accel.acceleration)
    print("Magnetometer (micro-Teslas)): X=%0.3f Y=%0.3f Z=%0.3f"%mag.magnetic)
    print("")
    sleep(0.5)
The Adafruit_CircuitPython_LSM303 Library has been archived

The Adafruit_CircuitPython_LSM303 Library for the older LSM303DLH breakout has been archived and replaced by Adafruit_CircuitPython_LSM303_Accel and Adafruit_CircuitPython_LSM303DLH_Mag libraries which are used above. We recommend switching to the newer libraries as the old one will not be supported or have features added.

If you still wish to use the old library, you can click the buttons below to go to the repository or download it as a ZIP

This guide was first published on Mar 10, 2013. It was last updated on Apr 16, 2024.

This page (Python & CircuitPython) was last updated on Apr 16, 2024.

Text editor powered by tinymce.