In this article we look at one of 2 similar Triple-axis Magnetometer that I recently purchased, the KX132 Triple Axis Accelerometer, and connect it to an ESP32
Sensor Information
The KX132 is a digital accelerometer 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 KX132 includes a host of features including Freefall detection, Directional Tap™ and Double-Tap™ detection, tilt orientation detection and more.
The Qwiic KX132 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.
Features
Measurement Range: ±2g, ±4g, ±8g, ±16g (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 can be used as an alternate)
Parts Required
You can connect to the sensor using DuPont style jumper wire.
Name | Link | |
ESP32 | ||
KX132 | ||
Connecting cables |
Schematic/Connection
I used 3.3v from the ESP32 Lolin board I used for testing
I also used a Qwiic cable but if you do not have one of them there is an unpopulated set of pins you can solder a header to. This is how you would wire this up
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
This is one of the examples that gets installed with the library, with a few comments and unused lines removed.
#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 run and the sensor was moved around
Welcome.
Ready.
Reset.
X: 0.0005 Y: -0.6676 Z: -0.7608
X: 0.0464 Y: -0.6598 Z: -0.7544
X: 0.0600 Y: -0.6324 Z: -0.8428
X: 0.1459 Y: -0.6759 Z: -0.8589