Error with adafruit_lsm303_accel on Raspberry Pi Pico W

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
plasmatech8
 
Posts: 3
Joined: Sat May 27, 2023 1:11 am

Error with adafruit_lsm303_accel on Raspberry Pi Pico W

Post by plasmatech8 »

Hello,

I am using the LSM303DLH magnetometer/accelerometer with the Raspberry Pi Pico W & CircuitPython.

I have successfully used the adafruit_lsm303dlh_mag library to read the magnetometer data and it works great.

However, I'm encountering an issue with the adafruit_lsm303_accel library.

Problem Description:

When I run the following code, I receive the following error message:

Code: Select all

import time
import board
import adafruit_lsm303_accel

i2c = board.STEMMA_I2C()
sensor = adafruit_lsm303_accel.LSM303_Accel(i2c)

while True:
    acc_x, acc_y, acc_z = sensor.acceleration
    print('Acceleration (m/s^2): ({0:10.3f}, {1:10.3f}, {2:10.3f})'.format(acc_x, acc_y, acc_z))
    time.sleep(1.0)
Error:

Code: Select all

ValueError: No I2C device at address: 0x19
Background Information:
  • Raspberry Pi Pico W is connected to the LSM303DLH via I2C on pins GP4 and GP5.
  • Installed CircuitPython using Thonny and copied the required dependencies into the lib folder on the device.
Other Questions:
  • Is the LSM303AGR better than the LSM303DLH?
  • Does the magnetic field measurements (in adafruit_lsm303dlh_mag) automatically add acceleration compensation? (I assume not)
  • Are there any troubleshooting steps I can try to resolve this error?

Any help or suggestions would be greatly appreciated. Thanks!

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Error with adafruit_lsm303_accel on Raspberry Pi Pico W

Post by mikeysklar »

Can you show your wiring for this?

Please also try running the included i2c scan.

Code: Select all

# SPDX-FileCopyrightText: 2022 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""CircuitPython I2C Device Address Scan for PiCowbell Proto and Pico"""
import time
import board

i2c = board.STEMMA_I2C()

while not i2c.try_lock():
    pass

try:
    while True:
        print(
            "I2C addresses found:",
            [hex(device_address) for device_address in i2c.scan()],
        )
        time.sleep(2)

finally:  # unlock the i2c bus when ctrl-c'ing out of the loop
    i2c.unlock()

User avatar
plasmatech8
 
Posts: 3
Joined: Sat May 27, 2023 1:11 am

Re: Error with adafruit_lsm303_accel on Raspberry Pi Pico W

Post by plasmatech8 »

When I run the code, it gives:

Code: Select all

I2C addresses found: ['0x18', '0x1e']
Do I need to manually specify an I2C address, since it doesn't work out-of-the-box?

The magnetometer works. I assume that one of (0x18, 0x1e) is that, and the other is the accelerometer.
Attachments
20230530_135915.jpg
20230530_135915.jpg (521.75 KiB) Viewed 92 times

User avatar
plasmatech8
 
Posts: 3
Joined: Sat May 27, 2023 1:11 am

Re: Error with adafruit_lsm303_accel on Raspberry Pi Pico W

Post by plasmatech8 »

Thanks for the code snippet, I'll save that for later. :)

I think I have figured out the problem now.

I can see here in the CircuitPython library, it looks for the Accelerometer at address 0x19, but it is actually on 0x18.

It seems that the existing library is unusable for me since the code does not allow changing the address. (plus, the files are .mpy, so I cannot edit them directly)

e.g.

Code: Select all

i2c = board.STEMMA_I2C()
adafruit_lsm303_accel._ADDRESS_ACCEL = 0x18 # Does not do anything
sensor = adafruit_lsm303_accel.LSM303_Accel(i2c) # There is no parameter such as: address=0x18
sensor._accel_device = I2CDevice(i2c, 0x18) # Line never reached due to error
But I can fix the problem by:
1. Download the adafruit_lsm303_accel.py Python file
2. Change 0x19 to 0x18
3. Rename the file to adafruit_lsm303_accel_v2.py or whatever
4. Copy the file over to the device and use it in code

Code: Select all

import time
import board
import adafruit_lsm303_accel_v2

i2c = board.STEMMA_I2C()
sensor = adafruit_lsm303_accel_v2.LSM303_Accel(i2c)

while True:
    acc_x, acc_y, acc_z = sensor.acceleration
    print('Acceleration (m/s^2): ({0:10.1f}, {1:10.1f}, {2:10.1f})'.format(, acc_y, acc_z), end='\r')
    time.sleep(0.1)

Code: Select all

Acceleration (m/s^2): (      -1.1,        3.3,        9.9)
Works! Looks like it outputs rotation in some sort of measurement range.

Now I will take a look into how I can use it to make the magnetometer/compass more accurate.

It would be better if the Python library had an input parameter for I2C address (or have basic no-logic constructors like a lot of ML models have). But looking good now :)

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Error with adafruit_lsm303_accel on Raspberry Pi Pico W

Post by mikeysklar »

Good job working around the i2c initialization with a custom library mod.

Your wiring is mostly correct thus you can see the i2c and communicate, but you should not be using VBUS (which is 5v) to power your i2c devices. Use the 3v3(OUT) pin 36 to power your i2c stuff.

It looks like you are using a non-adafruit board thus the unknown address. You can request an enhancement or submit a PR for the library to accept custom i2c addresses on initialization.

User avatar
slight
 
Posts: 120
Joined: Wed Sep 12, 2012 2:23 am

Re: Error with adafruit_lsm303_accel on Raspberry Pi Pico W

Post by slight »

some days ago i also found that there are a bunch of different versions of LSM303 chips...
the guide has some more info on this...
https://learn.adafruit.com/lsm303-accel ... -do-i-have
your breakout board uses LSM303DLH - so seems this one is on 0x18...
and back then i also found
https://forum.pololu.com/t/lsm303d-rasp ... river/7698
as i had an very old pololu board that uses an LSM303DLHC - so i also had to tweak some small things...

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”