Hi,
For the fastest sampling, you actually want to be using SPI, instead of I2C. But you probably don’t have time to go up that learning curve…? Maybe save that suggestion for later.
Some things that should help:
The Due Wire (I2C) bus almost certainly defaults to 100kHz. You can increase that to 400kHz by calling Wire.setClock(400000); after Wire.begin(); . There is a small complication in that the kxAccel.begin() may reset the bus back to 100kHz depending on how you call it. So, it will do no harm to (re)call Wire.setClock(400000); after kxAccel.begin() or kxAccel.softwareReset()
kxAccel.getAccelData is slow because it manually checks if the buffer is being used, and if the data resolution is 16-bit or 8-bit, each time it is called. That involves extra I2C bus transactions which slow things down. It is much more efficient to use the KX134’s buffer and to read the data directly from the buffer using kxAccel.getRawAccelBufferData. See Example3 for details:
https://github.com/sparkfun/SparkFun_KX … buffer.ino
If you are not using the buffer, kxAccel.getRawAccelRegisterData reads the data directly from the X, Y and Z registers. It too is more efficient than kxAccel.getAccelData
Those changes should increase the sample rate significantly. If you need to increase it further:
If you are using the buffer (based on Example3), then kxAccel.getSampleLevel() also slows things down a little by checking how much data is waiting in the buffer. Again it involves a bus transaction. You can speed things up by connecting the KX134 INT1 pin to a GPIO pin on the Due. When the pin goes high, data is ready and you can read it without needing to call kxAccel.getSampleLevel() first.
The code in Example3 generates an interrupt on INT1 when the buffer is full. In you case, it might be more efficient to generate the interrupt when data is ready (DRDYI1) instead of when the buffer is full (BFI1). The code in Example2 may work better for you. kxAccel.routeHardwareInterrupt(0x10); tells the KX134 to generate an interrupt when data is ready. (For buffer-full, the value is 0x40 instead of 0x10.)
https://github.com/sparkfun/SparkFun_KX … rrupts.ino
If you do use the code from Example2, don’t forget to use kxAccel.getRawAccelRegisterData instead of kxAccel.getAccelData to speed things up:
rawOutputData myRawData;
if (kxAccel.getRawAccelRegisterData(&myRawData) == true)
{
kxAccel.convAccelData(&myData, &myRawData); // Manually convert the raw data to floating point
Serial.println();
Serial.print("X: ");
Serial.print(myData.xData, 4);
Serial.print(" Y: ");
Serial.print(myData.yData, 4);
Serial.print(" Z: ");
Serial.print(myData.zData, 4);
Serial.println();
}
The getData functions read all three axes registers in one go. You could change the code so that it only reads a single axis, but you would need to hack the library at quite a low level to do that. If you think you need to do that, I’d suggest using SPI instead.
I hope this helps,
Paul