Sparkfun KX134 parameters

Hello everyone!

I searched and didn’t find (sorry if it went unnoticed by me) the following information:

  • How to configure setOutputDataRate to values ​​other than the default (50Hz)

  • In case of using buffer mode, how to check if there is data to be read.

Thanks in advance!

https://learn.sparkfun.com/tutorials/tr … e-overview

The KX134 and configuring its output data rate and using its buffer mode requires interacting with its registers. Below are the steps you need to follow for both tasks:

### Configuring Output Data Rate

To configure the output data rate (ODR) of the KX134, you need to modify the CNTL1 register. The ODR is controlled by the ODR bits within this register. Here’s a general process:

  1. Read the current value of the CNTL1 register.

  2. Modify the ODR bits (bits 6-4) to set your desired ODR.

  3. Write the new value back to the CNTL1 register.

Here’s a table for the ODR settings:

| ODR (Hz) | ODR Bits (CNTL1[6:4]) |

|----------|------------------------|

| 0.781 | 000 |

| 1.563 | 001 |

| 3.125 | 010 |

| 6.25 | 011 |

| 12.5 | 100 |

| 25 | 101 |

| 50 | 110 |

| 100 | 111 |

Here’s an example assuming you are using an I2C library to communicate with the KX134:

#define KX134_ADDR 0x1E // Replace with your actual I2C address
#define CNTL1 0x18

uint8_t readRegister(uint8_t reg) {
    // Your implementation for reading a register over I2C
}

void writeRegister(uint8_t reg, uint8_t value) {
    // Your implementation for writing a value to a register over I2C
}

void setOutputDataRate(uint8_t odr) {
    uint8_t cntl1 = readRegister(CNTL1);
    cntl1 &= ~(0x70); // Clear ODR bits
    cntl1 |= (odr << 4); // Set new ODR bits
    writeRegister(CNTL1, cntl1);
}

// Example usage to set ODR to 100Hz
setOutputDataRate(0x07);

### Checking for Data in Buffer Mode

To use the buffer mode and check if there is data to be read, you need to interact with the buffer status registers. Here’s a general approach:

  1. Enable buffer mode by configuring the BUF_CNTL2 and BUF_CNTL1 registers.

  2. Check the BUF_STATUS_1 register to see if data is available.

The BUF_STATUS_1 register provides the number of samples available in the buffer.

Here’s an example:

#define BUF_CNTL1 0x3A
#define BUF_CNTL2 0x3B
#define BUF_STATUS_1 0x3C

void enableBufferMode() {
    writeRegister(BUF_CNTL2, 0x80); // Enable buffer mode
    writeRegister(BUF_CNTL1, 0x80); // Set buffer watermark for interrupt (optional)
}

bool isDataAvailable() {
    uint8_t status = readRegister(BUF_STATUS_1);
    return (status & 0x3F) > 0; // Check if there are samples in the buffer
}

// Example usage
enableBufferMode();
if (isDataAvailable()) {
    // Read data from the buffer
}

These snippets assume you have functions for reading and writing I2C registers. Adjust the I2C address and register values as needed based on your specific setup and library.