Hi,
i tried to connect the KX134 in SPI mode to a Sparkfun thing Micro-B [1] (and also ESP32-S3-DevKitC-1 [2]) as described in this post [3] AKA “sever the trace between the “Center” and “Left” pads of the ADR jumper”. Then i connected the cabels like this:
KX134 3.3V → 3.3V thing esp32
KX134 GND → GND thing esp32
KX134 CS → GPIO (PIN 21) thing esp32
KX134 SDO → MOSI (PIN 18) thing esp32
KX134 SDI → MISO (PIN 19) thing esp32
KX134 SCLK → SCK (PIN 5) thing esp32
I used this code [4] to test the connection but i it always stops with “Could not communicate with the the KX13X. Freezing.”. I also tried to run it again with the QWIIC but is can’t detect the chip ether so i assumed i correctly severed the ADR jumper.
How can i debug this or did is miss something?
Kind Regards,
Josip
[1] https://www.sparkfun.com/products/15663
[2] https://www.amazon.de/dp/B0CQNB74R9
[3] https://learn.sparkfun.com/tutorials/tr … ll?print=1
[4]
/*
example5-BasicReadings-SPI
...
*/
#include <SPI.h>
#include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X
SparkFun_KX132_SPI kxAccel;
// SparkFun_KX134_SPI kxAccel; // For the KX134, uncomment this and comment line above
outputData myData; // Struct for the accelerometer's data
// define what pin
byte chipSelect = 21; // Change to fit your project.
void setup()
{
// Get the chip select pin ready.
pinMode(chipSelect, OUTPUT);
digitalWrite(chipSelect, HIGH);
SPI.begin();
Serial.begin(115200);
Serial.println("Welcome.");
// Wait for the Serial monitor to be opened.
while (!Serial)
delay(50);
if (!kxAccel.begin(chipSelect))
{
Serial.println("Could not communicate with the the KX13X. Freezing.");
while (1)
;
}
Serial.println("Ready.");
// Reset the chip so that old settings don't apply to new setups.
if (kxAccel.softwareReset())
Serial.println("Reset.");
// Give some time for the accelerometer to reset.
// It needs two, but give it five for good measure.
delay(5);
// Reset the chip so that old settings don't apply to new setups.
if (kxAccel.softwareReset())
Serial.println("Reset.");
// Give some time for the accelerometer to reset.
// It needs two, but give it five for good measure.
delay(5);
// Many settings for KX13X can only be
// applied when the accelerometer is powered down.
// However there are many that can be changed "on-the-fly"
// check datasheet for more info, or the comments in the
// "...regs.h" file which specify which can be changed when.
kxAccel.enableAccel(false);
kxAccel.enableBufferInt(); // Enables the Buffer interrupt
kxAccel.enablePhysInterrupt(); // Enables interrupt pin 1
kxAccel.routeHardwareInterrupt(0x40); // Routes the data ready bit to pin 1
kxAccel.enableSampleBuffer(); // Enable buffer.
kxAccel.setBufferOperationMode(0x00); // Enable the buffer to be FIFO.
kxAccel.setOutputDataRate(8); // 200 Hz
// kxAccel.setOutputDataRate(12); // 3200 Hz
// Additional Buffer Settings
// uint8_t numSamples = 140;
// kxAccel.setBufferThreshold(numSamples); // Set the number of sample that can be stored in the buffer
kxAccel.setBufferResolution(); // Change sample resolution to 16 bit, 8 bit by default.
// This will change how many samples can be held in buffer.
// Comment the above line if you want to try using 8-bit data.
// kxAccel.clearBuffer(); // Clear the buffer
// kxAccel.getSampleLevel(); // Get the number of samples in the buffer. This number
// Changes depending on the resolution, see datasheet for more info.
// kxAccel.setRange(SFE_KX132_RANGE2G); // 2g Range
kxAccel.setRange(SFE_KX134_RANGE8G); // 8g for the KX134
// kxAccel.setOutputDataRate(); // Default is 50Hz
kxAccel.enableAccel();
}
void loop()
{
// We could use the KX13X interrupt pin and dataReadyPin to indicate when data is ready.
// But we can also use getSampleLevel. getSampleLevel will return how much data is in the buffer.
if (kxAccel.getSampleLevel() > 0)
{
/*
// getAccelData is slow as it manually checks if the buffer is being used
// and if the data resolution is 16-bit or 8-bit.
if (kxAccel.getAccelData(&myData) == true)
{
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();
}
*/
// We can read the data more quickly by calling getRawAccelBufferData because we know
// the buffer is being used and what the data resolution is.
// The default buffer resolution is 8-bit. It will be 16-bit because we called setBufferResolution above.
// If you comment setBufferResolution, change the '1' to a '0' for 8-bit data.
rawOutputData myRawData;
if (kxAccel.getRawAccelBufferData(&myRawData, 1) == true) // Change the '1' to a '0' for 8-bit data.
{
kxAccel.convAccelData(&myData, &myRawData); // Manually convert the raw data to floating point
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();
}
}
}