Hi, I am having trouble with a Kionix accelerometer (tri-axis). As you can see, I have tried multiple things to alleviate my problem but to no avail.
While I can get the accelerometer to give me values and about half of them are valid, the other half aren't. When the accelerometer is in the 0 position and as I turn in the positive X direction, the values go from 0 to 64 (rawcount) and then from 64 back to 0. By this point, the device has turned 180 degrees, on the 181 degree, the output is 255 and that value goes to 225 where it will increase back to 255 it reaches 0 again in the original position.
I thought the readings should go from 0 to 64 and back down to 0, and then start the negative readings. Below is my code and attached is the datasheet and a getting started guide.
*Note, this happening on multiple devices I have. Tested on Terminal window in sketch and an OLED display.
//Include Files
#include <Arduino.h>
#include <SPI.h>
#include <Wire.h>
#include <U8x8lib.h>
#include <math.h> /* Include math header file */
//Globals
int z_angle,r_AcX,r_AcY,r_AcZ;
uint16_t AcX,AcY,AcZ;
const int kio_addr = 0x0F; //I2C address of the KXTJ3
const int CNTL1 = 0x1B; //Control Register 1
const int DCR = 0x21; //Output Data Rate Control Resister
const int V1 = 0X00; // Value sent to CNTL2, Disable Sensor
const int V2 = 0x02; //Value Sent to Output Data Rate Control Register
const int V3 = 0xC0; //Set CNTL1 to high resolution and range at 2g, Enable Operating mode
char bufferx[80];
void setup()
{
Wire.begin();
Wire.beginTransmission(kio_addr);
Wire.write(CNTL1);
Wire.write(V1);
Wire.endTransmission(true);
Wire.beginTransmission(kio_addr);
Wire.write(DCR);
Wire.write(V2);
Wire.endTransmission(true);
Wire.beginTransmission(kio_addr);
Wire.write(CNTL1);
Wire.write(V3);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop()
{
Wire.beginTransmission(kio_addr);
Wire.write(0x07); // starting with register 0x06 (ACCEL_XOUT_H)
//Wire.write(0x06); // starting with register 0x06 (ACCEL_XOUT_L)
//Wire.write(0x08); // starting with register 0x06 (ACCEL_YOUT_L)
//Wire.write(0x0A); // starting with register 0x06 (ACCEL_ZOUT_L)
//Wire.endTransmission(false);
Wire.endTransmission();
//Wire.requestFrom(kio_addr,12); // request a total of registers
Wire.requestFrom(kio_addr,1); // request a total of registers
if(Wire.available() <=1)
{
AcX = Wire.read();
}
//AcX = Wire.read()<<12|Wire.read(); // 0x07 (ACCEL_XOUT_H) & 0x06 (ACCEL_XOUT_L)
//AcY = Wire.read()<<12|Wire.read(); // 0x09 (ACCEL_YOUT_H) & 0x08 (ACCEL_YOUT_L)
//AcZ = Wire.read()<<12|Wire.read(); // 0x0B (ACCEL_ZOUT_H) & 0x0A (ACCEL_ZOUT_L)
if(AcX >= 0)
{
r_AcX = round(AcX); //Raw x Rounded
//int to string
sprintf(bufferx,"Xaccel %d",r_AcX);
}
else
{
r_AcX = round(AcX*-1); //Raw x Rounded
//int to string for OLED Display
sprintf(bufferx,"Xaccel -%d",r_AcX);
}
//r_AcX = round(AcX); //Raw x Rounded
//r_AcY = round(AcY); //Raw y Rounded
//r_AcZ = round(AcZ); //Raw z Rounded
//z_angle = round(acos(AcZ/64)*57.29577951);
//sprintf(buffery,"Yaccel %d",r_AcY);
//sprintf(bufferz,"Zaccel %d",r_AcZ);
//sprintf(bufferz,"%d",z_angle);
//Serial.print("AcX = "); Serial.println(AcX);
//Serial.print(" | AcY = "); Serial.println(AcY);
//Serial.print(" | AcZ = "); Serial.println(AcZ);
delay(100);
}
AN065-Getting-Started.pdf (493 KB)