Hi, I just gotten my 9Dof IMU sensor stick from Sparkfun that includes ADXL345 accelerometer, ITG-3200 MEMS gyroscope and HMC5883L Magnetometer.
I decided to just do a test on just the Accelerometer first. I’ve connected 4.7k resistor Ohms to both SDA and SCL. My input voltage to 3V of my Arduino Micro.
Here is my ADXL345 Accelerometer code from the Internet:
#include <Adafruit_Sensor.h>
#include <Wire.h>
#define ADXL345_ADDRESS (0x53) // (0xA6 >> 1): Dec 83 for I2C comm. (7-bit)
#define ADXL345_REGISTER_XLSB (0x32)
#define ADXL_POWER_CTL (0x2D)
#define ADXL_PWRCTL_MEASURE (1 << 3)
int accelerometer_data[3];
char c;
void setup()
{
Wire.begin();
Serial.begin(9600);
for(int i = 0; i < 3; ++i)
{
accelerometer_data[i] = 0;
}
init_adxl345();
}
void loop() {
read_adxl345();
Serial.print("ACCEL: ");
Serial.print(accelerometer_data[0]); // x-axis
Serial.print("\t");
Serial.print(accelerometer_data[1]); // y-axis
Serial.print("\t");
Serial.print(accelerometer_data[2]); // z-axis
Serial.print("\n");
delay(500);
}
void init_adxl345()
{
byte data = 0;
i2c_write(ADXL345_ADDRESS, ADXL_POWER_CTL, ADXL_PWRCTL_MEASURE);
i2c_read(ADXL345_ADDRESS, ADXL_POWER_CTL, 1, &data);
Serial.println((unsigned int)data);
}
void i2c_write(int address, byte reg, byte data) {
Wire.beginTransmission(0x53);
Wire.write(reg);
Wire.write(data);
Wire.endTransmission();
}
void i2c_read(int address, byte reg, int count, byte* data) {
int i = 0;
Wire.beginTransmission(0x53);
Wire.write(reg);
Wire.endTransmission();
Wire.beginTransmission(0x53);
Wire.requestFrom(0x53,count);
while(Wire.available()){
c = Wire.read();
data[i] = c;
i++;
}
Wire.endTransmission();
}
void read_adxl345()
{
byte bytes[6];
memset(bytes,0,6);
i2c_read(ADXL345_ADDRESS, ADXL345_REGISTER_XLSB, 6, bytes);
for (int i=0;i<3;++i)
{
accelerometer_data[i] = (int)bytes[2*i] + (((int)bytes[2*i + 1]) << 8);
}
}
My display output, I am getting:
X: 0 Y: 5 Z: 264
My ‘X’ remains at 1, 0 or -1
My Y remains around 5,6,7 values
My Z remains around 263, 264
All these values is when my board is resting on a horizontal surface. I understand i should get a display of 0,0,1 for my x, y, z values respectively… but no. Is it because I should convert it into another unit?
Im not quite sure how to check the accuracy of the values displayed… Is there a method to check?
and how does the conversion of units from bytes to unit, g ?
Please help, and Thank you in advance!