Triple Axis Accelerometer Breakout - KX13x (Qwiic) Hookup Guide

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

Arduino Examples

The SparkFun Qwiic KX13x Arduino Library includes four examples to get started with both KX13x boards.

Example 1 - Basic Readings

Example 1 is a basic example to demonstrate how to read data from the accelerometer. Open the example by navigating to "File > Examples > SparkFun Qwiic KX13x Library > Example1BasicReadings". Next, open the Tools menu and select your board (in this case, Arduino Uno) and correct Port your board enumerated on. Upload the code, open the serial monitor and set the baud rate to 115200.

Code to note:

language:c
QwiicKX132 kxAccel;
outputData myData;

The setup starts the accelerometer and initializes it to the Default Settings. The code freezes if either process fails.

language:c
if( !kxAccel.begin() ){
    Serial.println("Could not communicate with the the KX13X. Freezing.");
    while(1);
  }
  else
    Serial.println("Ready.");



if( !kxAccel.initialize(DEFAULT_SETTINGS)){
    Serial.println("Could not initialize the chip.");
    while(1);
}
else
    Serial.println("Initialized...");

After initializing the IC, the code prints out data for all three axes every 20ms. The delay here is important as it should be 1/ODR (Output Data Rate) and the default setting is 50Hz.

language:c
void loop() {

  myData = kxAccel.getAccelData();
  Serial.print("X: ");
  Serial.print(myData.xData, 4);
  Serial.print("g ");
  Serial.print(" Y: ");
  Serial.print(myData.zData, 4);
  Serial.print("g ");
  Serial.print(" Z: ");
  Serial.print(myData.zData, 4);
  Serial.println("g ");

  delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz

}

Example 2 - Interrupts

Example 2 shows how to use the hardware interrupt pin(s) on the accelerometer. In order to use this example, connect the INT1 pin on the KX13x breakout to an interrupt-capable pin. This example assumes a SparkFun RedBoard Qwiic/Arduino Uno is used so adjust the code as necessary. To follow along with this example (as well as the Buffer Example), assemble your circuit with INT1 connected to D1 on your RedBoard/Uno similar to the photo below:

Interrupt circuit assembled.

Along with creating the KX13x and data objects, the code sets the physical interrupt pin as D1.

language:c
int dataReadyPin = D1;

The code sets the dataReadyPin as an input:

language:c
pinMode(dataReadyPin, INPUT);   

and initializes the accelerometer in the Interrupt Settings mode. By default, this mode enables INT1 and sets it to go HIGH when data is ready to be read:

language:c
if( !kxAccel.initialize(INT_SETTINGS)){
    Serial.println("Could not initialize the chip.");
    while(1);
}
else
    Serial.println("Initialized...");

After initializing the sensor, the main loop monitors the dataReadyPin (D1) and if it is HIGH, prints out data for all three axes:

language:c
void loop() {

  if( digitalRead(dataReadyPin) == HIGH ){ // Wait for new data to be ready.

    myData = kxAccel.getAccelData();
    Serial.print("X: ");
    Serial.print(myData.xData, 4);
    Serial.print("g ");
    Serial.print(" Y: ");
    Serial.print(myData.zData, 4);
    Serial.print("g ");
    Serial.print(" Z: ");
    Serial.print(myData.zData, 4);
    Serial.println("g ");

     //kxAccel.clearInterrupt();// Because the data is being read in "burst"
     //mode, meaning that all the acceleration data is being read at once, we don't
     //need to clear the interrupt.
  }
  delay(20); // Delay should be 1/ODR (Output Data Rate), default is 50Hz
}

Example 3 - Software Interrupts

The third example demonstrates how to use the KX13x to trigger software interrupts. The code initializes the KX13x in Software Interrupt mode. The primary difference between Example 3 and Example 2 is, as you may expect, the software interrupt does not use any of the interrupt pins on the KX13x.

The code initializes the KX13x in software interrupt mode:

language:c
if( !kxAccel.initialize(SOFTWARE_INT_SETTINGS)){
    Serial.println("Could not initialize the chip.");
    while(1);
}
else
    Serial.println("Initialized...");

The main loop waits for available data by polling the data ready bit and prints out acceleration data for all three axes whenever data is ready.

Example 4 - Buffer

The fourth example shows how to use the KX13x in the default buffer settings to trigger hardware interrupts when the buffer is full. Just like Example 2 , the code sets the physical interrupt/data ready pin as D1 which is driven HIGH when the buffer is full. Just like with Example 2, in order to use this example, connect the INT1 pin on the KX13x breakout to an interrupt-capable pin. This example assumes a SparkFun RedBoard Qwiic/Arduino Uno is used so adjust the code as necessary.:

language:c
int dataReadyPin = D1;

In the setup, the dataReadyPin is defined as an input:

language:c
pinMode(dataReadyPin, INPUT);

and the KX13x is initialized with default buffer settings (FIFO mode and 16 bit samples):

if( !kxAccel.initialize(BUFFER_SETTINGS)){
    Serial.println("Could not initialize the chip.");
    while(1);
}
else
    Serial.println("Initialized...");

After setting everything up, the main loop waits for the buffer to fill and drive the data ready pin HIGH. Once the pin goes HIGH, the code prints out acceleration data for all three axes just like the other examples.