Triple Axis Accelerometer Breakout - KX13x (Qwiic) Hookup Guide

Pages
Contributors: El Duderino, Englandsaurus, Elias The Sparkiest
Favorited Favorite 0

Python Examples

The Qwiic KX13X Python Package includes four examples to get users started with either Qwiic KX13x board using Python. In this section we'll go over the examples and highlight how they work.

To use the examples, open them from the Qwiic KX13X Py location or copy the code into your preferred Python interpreter.

Note, the examples default to using the Qwiic KX132 so if a Qwiic KX1334 is used, adjust the code by un-commenting this line:

language:python
myKX = qwiic_kx13x.QwiicKX134()

And replace any instance of kx132 with kx134. The acceleration range can also be adjusted by uncommenting this line and adjusting the value set for the range:

language:python
myKx.set_range(myKx.KX132_RANGE8G)

Example 1 - Simple Example

The first example is a basic example demonstrating how to initialize a Qwiic KX13x board on the I2C bus using its default settings. The full example code can be found below if you would prefer to copy it into your preferred Python interpreter:

language:python
from __future__ import print_function
import qwiic_kx13x
import time
import sys
import RPi.GPIO

def run_example():

    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)

Example 2 -

The second example shows how to enable Hardware Interrupt Pin 1 on the KX13x and fires it whenever data is ready. The complete example code can be found below if you prefer to copy/paste it into your prefered Python interpreter:

language:python
from __future__ import print_function
import qwiic_kx13x
import time
import sys
import RPi.GPIO

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.INT_SETTINGS) # Load basic settings 

    dataReadyPin = 5
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(dataReadyPin, GPIO.IN)

    while True:

        if GPIO.INPUT(dataReadyPin) == 1:

            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)

Example 3 - Software Interrupts

Example 3 shows how to use software interrupts to signal when accelerometer data is ready. The primary difference between this and the hardware interrupts is none of the KX13x Hardware Interrupt Pins are enabled. The full example is below for users who prefer to copy/paste it into their Python interpreter:

language:python
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.SOFT_INT_SETTINGS) # Load basic settings 

    while True:

        if myKx.data_trigger():

            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)

Example 4 - Buffer Interrupt

The fourth and final example builds on the hardware interrupt example and uses the Hardware Interrupt Pins to indicate when a buffer is full and ready to be read. Copy/paste the code below into your preferred Python interpreter:

language:python
from __future__ import print_function
import qwiic_kx13x
import time
import sys
import RPi.GPIO

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.BUFFER_SETTINGS) # Load basic settings 

    dataReadyPin = 5
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(dataReadyPin, GPIO.IN)

    while True:

        if GPIO.INPUT(dataReadyPin) == 1: # When the buffer is full, the pin will go high

            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)