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)