In this article we connect the ever popular KX134 accelerometer to a raspberry Pi Pico and we will then display readings via the serial monitor window. We are using the Arduino IDE.
Lets look at the sensor first
Features
- Measurement Range: ±8g, ±16g, ±32g, ±64g (User Selectable)
- High Resolution (8 or 16-bit)
- User-Configurable Output Data Rate (ODR) up to 25600Hz
- User-Configurable 3-stage Advanced Data Path featuring low-pass filter, low-pass/high-pass filter and RMS calculation engine
- Wide range of built-in sensing functions
- Free Fall
- Directional-Tap™ / Double-Tap™
- Device Orientation & Activity Algorithms
- Low Noise: 130µg/√Hz (varies based on ODR, power mode & other settings)
- High-Resolution Wake-Up & Back-to-Sleep Detection with a configurable threshold as low as 3.9mg
- 512-byte FIFO buffer that continues recording data while being read
- Selectable Low-Power or High-Performance operating modes
- Low Power with Integrated Voltage Regulator
- High-Performance Operating Current Consumption (400Hz ODR + Wake-Up Detection): 148µA
- Low Power Operating Current Consumption (0.781Hz ODR + Wake-Up Detection): 0.53µA
- Standby Current Consumption: 0.5µA
- Self-Test Functionality
- Digital I2C up to 3.4MHz and Digital SPI up to 10MHz
- 2x Qwiic Connectors
- SPI available on PTH Header Pins
- I2C Address: 0x1E (0x1F alternate)
Parts Required
Name | Link | |
Raspberry Pi Pico | ||
KX134 |
|
|
Connecting cables |
Schematic/Connection
Here is a layout in fritzing to show this
Code Example
I used the Sparkfun KX13X library and this is the default example which worked, there are several examples to experiment with
#include <Wire.h> #include <SparkFun_KX13X.h> // Click here to get the library: http://librarymanager/All#SparkFun_KX13X //SparkFun_KX132 kxAccel; SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above outputData myData; // Struct for the accelerometer's data void setup() { Wire.begin(); Serial.begin(115200); Serial.println("Welcome."); // Wait for the Serial monitor to be opened. while (!Serial) delay(50); if (!kxAccel.begin()) { Serial.println("Could not communicate with the the KX13X. Freezing."); while (1) ; } Serial.println("Ready."); 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.setRange(SFE_KX132_RANGE16G); // 16g Range kxAccel.setRange(SFE_KX134_RANGE16G); // 16g for the KX134 kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready. // kxAccel.setOutputDataRate(); // Default is 50Hz kxAccel.enableAccel(); } void loop() { // Check if data is ready. if (kxAccel.dataReady()) { kxAccel.getAccelData(&myData); 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(); } delay(200); // Delay should be 1/ODR (Output Data Rate), default is 1/50ODR }
Output
Move the sensor in various directions to test
X: 0.0273 Y: -0.4626 Z: -0.8560
X: 0.0073 Y: -0.4831 Z: -0.8623
X: 0.0595 Y: -0.4431 Z: -0.8535
X: 0.0063 Y: -0.4758 Z: -0.8799
X: -0.0229 Y: -0.2689 Z: -0.6803
X: -0.0605 Y: -0.3026 Z: -0.9199
X: -0.1147 Y: -0.4197 Z: -0.9062
X: -0.7745 Y: -0.2235 Z: 0.3777
X: -0.2991 Y: -1.1151 Z: -0.4099