Triple Axis Accelerometer Breakout - KX132 (Qwiic) ---- Error from Python example code

I am using the Triple Axis Accelerometer Breakout - KX132 on a Raspberry Pi 4B connected with the Sparkfun Raspberry Pi Qwiic SHIM. Raspberry Pi OS (2023-02-21 bullseye) and Python version 3.9.2.

Product Link: https://www.sparkfun.com/products/17871

I have followed the installation guide on github: https://github.com/sparkfun/Qwiic_KX13X_Py

and used the provided examble code that produces data that I want: https://github.com/sparkfun/Qwiic_KX13X … 13x_ex1.py

After running the code, I get the following error:

Traceback (most recent call last):
  File "<string>", line 2, in <module>
  File "/usr/local/lib/python3.9/dist-packages/qwiic_kx13x.py", line 334
    """
       ^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xa0 in position 173: invalid start byte

On the github issue section, other people have reported this same issue without a solution: https://github.com/sparkfun/Qwiic_KX13X_Py/issues/5

Does anyone know how to solve this issue?

I have followed the filepath on the second line of the error and went to line 334. A screenshot of it is attached.

qwiic_kx13x.py is what was installed for the installation process: sudo pip install sparkfun-qwiic-kx13x

I do not understand the syntax error message.

It looks like it might be specific to Pis…one strange thing I noticed in kx13x.py is that it uses both # and “”" to comment-out comments/unused code…maybe the Pi wants it to be in or the other? My first guess would be to open the file and do a search/replace of “”" with # and/or add # to any un-used lines and re-try?

To edit the file, I had to go through the file path on the command line and use sudo nano qwiic_kx13x.py to give myself edit permissions.

I went back to the comment above line 334, removed both of the “”", and commented out lines 328 - 334 with # .

Then I ran the code again and got a new error shown below:

SparkFun KX13X Accelerometer Example 1

Ready.
Traceback (most recent call last):
  File "/home/pi/Documents/Acceleremoter Module/Accelerometer Code.py", line 36, in <module>
    runExample()
  File "/home/pi/Documents/Acceleremoter Module/Accelerometer Code.py", line 23, in runExample
    myKx.initialize(myKx.BASIC_SETTINGS) # Load basic settings
AttributeError: 'QwiicKX132' object has no attribute 'BASIC_SETTINGS'

I then went back and replaced all “”" with # as you suggested, but received the same exact error.

Then I went back and put a # on each unused line, but still received the same error.

The new error posted above is now referencing the lines on the example program itself: https://github.com/sparkfun/Qwiic_KX13X … 13x_ex1.py

(I did not include the comments when I copied the code, so my line 36 is line 77 from the example, and my line 23 is line 64 from the example.)

I did not alter the code after pasting it.

After looking online, usually when people get an attribute error, the cause is a result of a minor formatting issue in the code such as an indentation, spelling error, or an extra comma. Although I do not see that here.

Here is the code:

from __future__ import print_function
import qwiic_kx13x
import time
import sys

def runExample():

    print("\nSparkFun KX13X Accelerometer Example 1\n")
    # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
    myKx = qwiic_kx13x.QwiicKX132()

    if myKx.connected == False:
            print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
                    file=sys.stderr)
            return

    if myKx.begin():
        print("Ready.")
    else:
        print("Make sure you're using the KX132 and not the KX134")

    # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
    myKx.initialize(myKx.BASIC_SETTINGS) # Load basic settings 

    while True:
            
        myKx.get_accel_data()
        print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
                                               myKx.kx132_accel.y,
                                               myKx.kx132_accel.z))
        time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02


if __name__ == '__main__':
    try:
        runExample()
    except (KeyboardInterrupt, SystemExit) as exErr:
        print("\nEnding Example 1")
        sys.exit(0)

I also searched for “BASIC_SETTINGS” on the qwiic_kx13x.py file but did not get any results.

I am not sure if it supposed to be on that file.

Try opening a new issue here https://github.com/sparkfun/Qwiic_KX13X_Py/issues - I can also try testing mine on Monday to see if I can figure anything out

Sorry for not noticing this sooner (I am new to github), but corrections to the qwiic_kx13x.py file and examples were made on the pull requests section: https://github.com/sparkfun/Qwiic_KX13X_Py/pull/2/files

Changes

Example 1 program:

  • line 64, “BASIC_SETTINGS” was changed to “DEFAULT_SETTINGS”

  • qwiic_kx13x.py file:
  • line 331, a random character was removed that was causing the Syntax Error mentioned in my original post

  • line 400, “accelControl” was changed to “accel_control”

  • line 628 was not indented correctly

    (this last one was not listed on github as one of the changes. I found the error when I got an Attribute error after making the listed changes)

  • The example code is now able to run. Everything is working completely fine.