Qwiic_KX13X_Py

follow on Twitter

Python module for the SparkFun Qwiic KX132 Accerlerometer and the SparkFun Qwiic KX134 Accelerometer.

This python package is a port of the existing SparkFun KX13X Arduino Library

This package can be used in conjunction with the overall SparkFun qwiic Python Package

New to qwiic? Take a look at the entire SparkFun qwiic ecosystem.

Dependencies

This driver package depends on the qwiic I2C driver: Qwiic_I2C_Py

Documentation

The SparkFun Qwiic KX13X module documentation is hosted at ReadTheDocs

Installation

PyPi Installation

This repository is hosted on PyPi as the sparkfun-qwiic-kx13x package. On systems that support PyPi installation via pip, this library is installed using the following commands

For all users (note: the user must have sudo privileges):

sudo pip install sparkfun-qwiic-kx13x

For the current user:

pip install sparkfun-qwiic-kx13x

Local Installation

To install, make sure the setuptools package is installed on the system.

Direct installation at the command line:

python setup.py install

To build a package for use with pip:

python setup.py sdist

A package file is built and placed in a subdirectory called dist. This package file can be installed using pip.

cd dist
pip install sparkfun_qwiic_kx13x-<version>.tar.gz

Example Use

See the examples directory for more detailed use examples.

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)

SparkFun - Start Something

Table of Contents

API Reference

Example One - This basic example simply reads accelerometer data.

examples/qwiic_kx13x_ex1.py
 1#!/usr/bin/env python3
 2#-----------------------------------------------------------------------------
 3# qwiic_kx13x_ex1.py
 4#
 5# Simple example for the Qwiic KX132/4 Accelerometer
 6#------------------------------------------------------------------------
 7#
 8# Written by  SparkFun Electronics, April 2021
 9# 
10# This python library supports the SparkFun Electroncis qwiic 
11# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
12# board computers. 
13#
14# More information on qwiic is at https://www.sparkfun.com/qwiic
15#
16# Do you like this library? Help support SparkFun. Buy a board!
17#
18#==================================================================================
19# Copyright (c) 2021 SparkFun Electronics
20#
21# Permission is hereby granted, free of charge, to any person obtaining a copy 
22# of this software and associated documentation files (the "Software"), to deal 
23# in the Software without restriction, including without limitation the rights 
24# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
25# copies of the Software, and to permit persons to whom the Software is 
26# furnished to do so, subject to the following conditions:
27#
28# The above copyright notice and this permission notice shall be included in all 
29# copies or substantial portions of the Software.
30#
31# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
32# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
33# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
34# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
35# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
36# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
37# SOFTWARE.
38#==================================================================================
39# Example 1
40# A simple example for the kx132 showing asychronous data streaming i.e. continuous streaming.
41
42from __future__ import print_function
43import qwiic_kx13x
44import time
45import sys
46
47def runExample():
48
49    print("\nSparkFun KX13X Accelerometer Example 1\n")
50    # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
51    myKx = qwiic_kx13x.QwiicKX132()
52
53    if myKx.connected == False:
54            print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
55                    file=sys.stderr)
56            return
57
58    if myKx.begin():
59        print("Ready.")
60    else:
61        print("Make sure you're using the KX132 and not the KX134")
62
63    # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
64    myKx.initialize(myKx.BASIC_SETTINGS) # Load basic settings 
65
66    while True:
67            
68        myKx.get_accel_data()
69        print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
70                                               myKx.kx132_accel.y,
71                                               myKx.kx132_accel.z))
72        time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02
73
74
75if __name__ == '__main__':
76	try:
77		runExample()
78	except (KeyboardInterrupt, SystemExit) as exErr:
79		print("\nEnding Example 1")
80		sys.exit(0)

Example 2 - This example demonstrates the use of hardware interrupts.

examples/qwiic_kx13x_ex2.py
 1#!/usr/bin/env python3
 2#-----------------------------------------------------------------------------
 3# qwiic_kx13x_ex2.py
 4#
 5# Simple example for the Qwiic KX132/4 Accelerometer using hardware interrupts
 6# to indicate that data is ready.
 7#------------------------------------------------------------------------
 8#
 9# Written by  SparkFun Electronics, April 2021
10# 
11# This python library supports the SparkFun Electroncis qwiic 
12# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
13# board computers. 
14#
15# More information on qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun. Buy a board!
18#
19#==================================================================================
20# Copyright (c) 2021 SparkFun Electronics
21#
22# Permission is hereby granted, free of charge, to any person obtaining a copy 
23# of this software and associated documentation files (the "Software"), to deal 
24# in the Software without restriction, including without limitation the rights 
25# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
26# copies of the Software, and to permit persons to whom the Software is 
27# furnished to do so, subject to the following conditions:
28#
29# The above copyright notice and this permission notice shall be included in all 
30# copies or substantial portions of the Software.
31#
32# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
33# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
34# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
35# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
36# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
37# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
38# SOFTWARE.
39#==================================================================================
40# Example 2: Using hardware interrupts.
41
42from __future__ import print_function
43import qwiic_kx13x
44import time
45import sys
46import RPi.GPIO
47
48def runExample():
49
50    print("\nSparkFun KX13X Accelerometer Example 1\n")
51    # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
52    myKx = qwiic_kx13x.QwiicKX132()
53
54    if myKx.connected == False:
55        print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
56                file=sys.stderr)
57        return
58
59    if myKx.begin():
60        print("Ready.")
61    else:
62        print("Make sure you're using the KX132 and not the KX134")
63
64    # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
65    myKx.initialize(myKx.INT_SETTINGS) # Load basic settings 
66
67    dataReadyPin = 5
68    GPIO.setmode(GPIO.BCM)
69    GPIO.setup(dataReadyPin, GPIO.IN)
70
71    while True:
72            
73        if GPIO.INPUT(dataReadyPin) == 1:
74
75            myKx.get_accel_data()
76            print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
77                                                   myKx.kx132_accel.y,
78                                                   myKx.kx132_accel.z))
79
80        time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02
81
82if __name__ == '__main__':
83	try:
84		runExample()
85	except (KeyboardInterrupt, SystemExit) as exErr:
86		print("\nEnding Example 1")
87		sys.exit(0)

Example 3 - This example demonstrates of polling the “data ready” bit, i.e. software interrupt.

examples/qwiic_kx13x_ex3.py
 1#!/usr/bin/env python3
 2#-----------------------------------------------------------------------------
 3# qwiic_kx13x_ex3.py
 4#
 5# Simple example for the Qwiic KX132/4 Accelerometer using software interrupts
 6# to indicate that data is ready.
 7#------------------------------------------------------------------------
 8#
 9# Written by  SparkFun Electronics, April 2021
10# 
11# This python library supports the SparkFun Electroncis qwiic 
12# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
13# board computers. 
14#
15# More information on qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun. Buy a board!
18#
19#==================================================================================
20# Copyright (c) 2021 SparkFun Electronics
21#
22# Permission is hereby granted, free of charge, to any person obtaining a copy 
23# of this software and associated documentation files (the "Software"), to deal 
24# in the Software without restriction, including without limitation the rights 
25# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
26# copies of the Software, and to permit persons to whom the Software is 
27# furnished to do so, subject to the following conditions:
28#
29# The above copyright notice and this permission notice shall be included in all 
30# copies or substantial portions of the Software.
31#
32# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
33# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
34# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
35# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
36# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
37# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
38# SOFTWARE.
39#==================================================================================
40# Example 3: Using software interrupts.
41
42from __future__ import print_function
43import qwiic_kx13x
44import time
45import sys
46
47def runExample():
48
49    print("\nSparkFun KX13X Accelerometer Example 1\n")
50    # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
51    myKx = qwiic_kx13x.QwiicKX132()
52
53    if myKx.connected == False:
54        print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
55                file=sys.stderr)
56        return
57
58    if myKx.begin():
59        print("Ready.")
60    else:
61        print("Make sure you're using the KX132 and not the KX134")
62
63    # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
64    myKx.initialize(myKx.SOFT_INT_SETTINGS) # Load basic settings 
65
66    while True:
67            
68        if myKx.data_trigger():
69
70            myKx.get_accel_data()
71            print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
72                                                   myKx.kx132_accel.y,
73                                                   myKx.kx132_accel.z))
74
75        time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02
76
77if __name__ == '__main__':
78	try:
79		runExample()
80	except (KeyboardInterrupt, SystemExit) as exErr:
81		print("\nEnding Example 1")
82		sys.exit(0)

Example 4 - This example demonstrates the use of the FIFO buffer to store and retrieve accelerometer data.

examples/qwiic_kx13x_ex4.py
 1#!/usr/bin/env python3
 2#-----------------------------------------------------------------------------
 3# qwiic_kx13x_ex4.py
 4#
 5# Simple example for the Qwiic KX132/4 Accelerometer using hardware interrupts
 6# to indicate that the buffer is full and ready to be read.
 7#------------------------------------------------------------------------
 8#
 9# Written by  SparkFun Electronics, April 2021
10# 
11# This python library supports the SparkFun Electroncis qwiic 
12# qwiic sensor/board ecosystem on a Raspberry Pi (and compatable) single
13# board computers. 
14#
15# More information on qwiic is at https://www.sparkfun.com/qwiic
16#
17# Do you like this library? Help support SparkFun. Buy a board!
18#
19#==================================================================================
20# Copyright (c) 2021 SparkFun Electronics
21#
22# Permission is hereby granted, free of charge, to any person obtaining a copy 
23# of this software and associated documentation files (the "Software"), to deal 
24# in the Software without restriction, including without limitation the rights 
25# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
26# copies of the Software, and to permit persons to whom the Software is 
27# furnished to do so, subject to the following conditions:
28#
29# The above copyright notice and this permission notice shall be included in all 
30# copies or substantial portions of the Software.
31#
32# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
33# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
34# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
35# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
36# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
37# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 
38# SOFTWARE.
39#==================================================================================
40# Example 4: Using the Buffer
41
42from __future__ import print_function
43import qwiic_kx13x
44import time
45import sys
46import RPi.GPIO
47
48def runExample():
49
50    print("\nSparkFun KX13X Accelerometer Example 1\n")
51    # myKx = qwiic_kx13x.QwiicKX134() # If using the KX134 un-comment this line and replace other instances of "kx132" with "kx134"
52    myKx = qwiic_kx13x.QwiicKX132()
53
54    if myKx.connected == False:
55        print("The Qwiic KX13X Accelerometer device isn't connected to the system. Please check your connection", \
56                file=sys.stderr)
57        return
58
59    if myKx.begin():
60        print("Ready.")
61    else:
62        print("Make sure you're using the KX132 and not the KX134")
63
64    # myKx.set_range(myKx.KX132_RANGE8G) # Update the range of the data output.
65    myKx.initialize(myKx.BUFFER_SETTINGS) # Load basic settings 
66
67    dataReadyPin = 5
68    GPIO.setmode(GPIO.BCM)
69    GPIO.setup(dataReadyPin, GPIO.IN)
70
71    while True:
72            
73        if GPIO.INPUT(dataReadyPin) == 1: # When the buffer is full, the pin will go high
74
75            myKx.get_accel_data()
76            print("X: {0}g Y: {1}g Z: {2}g".format(myKx.kx132_accel.x,
77                                                   myKx.kx132_accel.y,
78                                                   myKx.kx132_accel.z))
79
80        time.sleep(.02) #Set delay to 1/Output Data Rate which is by default 50Hz 1/50 = .02
81
82if __name__ == '__main__':
83	try:
84		runExample()
85	except (KeyboardInterrupt, SystemExit) as exErr:
86		print("\nEnding Example 1")
87		sys.exit(0)

Indices and tables