In this article we look at 2 very similar Triple-axis Magnetometer that I recently purchased, the KX132 and KX134 Triple Axis Accelerometers, we will connect them to our trusty ESP8266 board
Sensor Information
The KX132 and KX134 are digital accelerometers from Kionix.
The KX132 is a low-power, 16-bit resolution three-axis accelerometer with four user-selectable acceleration measurement ranges of ±2g/4g/8g/16g and has up to a 10kHz (max) output data rate making it ideal for a wide range of acceleration measurements as well as high-speed applications such as vibration and tap sensing.
The KX134 is a low-power, 16-bit resolution three-axis accelerometer capable of measuring ±8g/16g/32g/64g (user-selectable) and has up to a 10kHz output data rate making it ideal for high-g measurements as well as high-speed applications such as vibration sensing
The KX132/134 includes a host of features including Freefall detection, Directional Tap™ and Double-Tap™ detection, tilt orientation detection and more.
The Qwiic KX132/134 can interface with controllers using both I2C and SPI at high speeds so you can use it in an existing Qwiic/I2C chain or on a SPI bus.
Parts Required
Parts List
Here are the parts I used
Name | Links | |
Wemos Mini | ||
KX132 | ||
KX134 | ||
Connecting cables |
Schematic/Connection
I used 3.3v from my ESP8266 board I used for testing
Code Example
I installed the Sparkfun library using the Arduino ide
Click the Manage Libraries … menu item, search for KX132, and select the Sparkfun KX13x library like this
You can select the sensor you are using by commenting or removing one of these lines. The rest of the code is the same for both modules
SparkFun_KX132 kxAccel;
// SparkFun_KX134 kxAccel; // For the KX134, uncomment this and comment line above
This is one of the examples that gets installed with the library, with a few comments and unused lines removed.
There are 5 examples to explore
#include <Wire.h> #include <SparkFun_KX13X.h> SparkFun_KX132 kxAccel; outputData myData; // Struct for the accelerometer's data void setup() { Wire.begin(); Serial.begin(115200); // Wait for the Serial monitor to be opened. while (!Serial) delay(50); if (!kxAccel.begin()) { Serial.println("Could not communicate with the the KX13X"); while (1) ; } Serial.println("Ready."); if (kxAccel.softwareReset()) Serial.println("Reset."); // Give some time for the accelerometer to reset. delay(5); // Many settings for KX13X can only be // applied when the accelerometer is powered down. kxAccel.enableAccel(false); kxAccel.setRange(SFE_KX132_RANGE16G); // 16g Range kxAccel.enableDataEngine(); // Enables the bit that indicates data is ready. 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(500); }
Output
When running I saw this in the serial monitor window and the sensor was moved around
X: -0.2381 Y: 0.2303 Z: -0.9072
X: 0.3148 Y: 0.2069 Z: -0.8032
X: 0.4519 Y: 0.6403 Z: -0.4446
X: 0.0732 Y: 0.4709 Z: -0.6427
X: -0.0356 Y: 0.2977 Z: -0.9609
X: 0.0420 Y: 0.1132 Z: -1.0404
Links
https://www.kionix.com/product/KX132-1211
https://kionixfs.azureedge.net/en/datasheet/kx134-1211-e.pdf