Triple Axis Accelerometer Breakout - KX13x (Qwiic) Hookup Guide

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

KX13x Arduino Library

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

The SparkFun KX13x Arudino library makes it easy to get started measuring acceleration data from the sensor. You can install this library through the Arduino Library Manager. Search for "SparkFun KX13x Arduino Library" and install the latest version. If you prefer manually downloading the library from the GitHub repository, you can grab it here:

Library Functions

The list below outlines all of the functions available in the SparkFun Qwiic KX13x Arduino Library along with quick descriptions of what they do. The examples cover many of the functions on this list so refer to them to get started or to demonstrate how to integrate them into your own code.

Class

In the global scope, construct your KX13x object (such as accel or myKX13x) without arguments.

  • QwiicKX13X accel;

The KX13x Arduino Library has a class for each version of the KX13x. Construct the object (such as kxAccel or myKX13x) without arguments:

  • QwiicKX132 kxAccel;

or

  • QwiicKX134 kxAccel;

The examples default to using the KX132 so adjust the object to KX134 if needed.

The library also uses an object for the accelerometer data. Construct the object (such as myData or accelData) in the global class:

  • outputData myData;

Device Setup & Configuration

  • bool begin(uint8_t deviceAddress = KX13X_DEFAULT_ADDRESS, TwoWire &wirePort = Wire); - Start communication with the KX13x via I2C.
  • bool beginSPI(uint*_t, uint32_t spiPortSpeed = 10000000, SPIClass &spiPort = SPI); - Start communication with the KX13x via SPI.
  • bool initialize(uint8_t settings = DEFAULT_SETTINGS); - Initialize the KX13x at the specified settings. These settings are specified according to the AN092 Getting Started App Note and can be adjusted to have additional presets. The available settings in the library are:
    • DEFAULT_SETTINGS - Initialize the KX13x with no alternate settings active.
    • INT_SETTINGS - Initialize the KX13x with the Data Ready Engine enabled (CNTL1 register, bit 5), the KX13x Interrupt 1 pin enabled and configured to trigger on Data Ready.
    • SOFT_INT_SETTINGS - Initialize the KX13x with the Data Ready Engine enabled (CNTL1 register, bit 5), no hardware interrupt pin enabled.
    • BUFFER_SETTINGS - Initialize the KX13x with the Data Ready Engine enabled (CNTL1 register, bit 5), the KX13x Interrupt 1 pin enabled and configured to trigger when the buffer is full. Buffer enabled and settings configured to FIFO mode and 16 bit samples.
  • bool accelControl(bool); - Sets the operating mode of the KX13x to stand-by mode or High-Performance/Low Power mode.
  • uint8_t readAccelSate(); - Reads whether the operating state of the KX13x (Stand By or Active Mode).
  • bool setRange(uint8_t range); - Set the range of the KX13x. Split into dedicated values for either the KX132 or KX134 to avoid errors in data conversion handling. Valid options for this setting are:
    • KX132_RANGE2G
    • KX132_RANGE4G
    • KX132_RANGE8G
    • KX132_RANGE16G
    • KX134_RANGE8G
    • KX134_RANGE16G
    • KX134_RANGE32G
    • KX134_RANGE64G
  • bool setOutputDataRate(uint8_t rate) - Set the refresh rate of the KX13x's output data in Hz. The default value is 50Hz (0b0110). Refer to the table on page 26 of the Technical Reference Manual (KX132 & KX134 for valid entries.
  • float readOutputDataRate(); - Reads the value set for the Output Data Rate.
  • bool setInterruptPin(bool enable, uint8_t polarity = 0, uint8_t pulseWidth = 0, bool latchControl = false); - Configure the Interrupt pin settings. Note: settings just one sets all others to their default.
  • bool routeHardwareInterrupt(uint8_t, uint8_t pin = 1); - Route any of the interrupt pin settings to either interrupt pin 1 or 2.
  • bool clearInterrupt(); - Clear the interrupt register by reading the interrupt latch release register.
  • bool dataTrigger(); - Triggers collection of data by the KX13X.
  • bool setBufferThreshold(uint8_t); - Sets the number of samples (not bytes) held in the buffer. Minimum value is two, maximum depends on the buffer resolution (8 or 16bit).
  • bool setBufferOperation(uint8_t, uint8_t); - Sets the resolution and operation mode of the buffer. Resolution can be 8 or 16 bit. Operation modes are FIFO, Stream & Trigger. More information on the buffer modes found on Table 16 of the Technical Reference Manual.
  • bool enableBuffer(bool, bool); - Enables the buffer and sets whether the buffer triggers an interrupt event when full.
  • bool runCommandTest(); - Checks the integrity of the IC. Primarily for manufacturing use.

Acceleration Data

  • bool getRawAccelData(rawOutputData*); - Pull raw acceleration data values for X, Y and Z axes from either the buffer or output registers depending on if buffer usage is specified by the user.
  • bool convAccelData(outputData*, rawOutputData*); - Convert the acceleration data into g using the value stored for setRange();.
  • outputData getAccelData(); - Pull converted acceleration data for X, Y and Z axes. Call data for the specific axis by using myData.xData (or yData/zData) where myData is the definition of outputData class. Refer to the examples in the Arduino library for more information.