Hi
I am starting to try with ADXL345 with this sketch:
/*
I2C comunication
read acceleration value
CS pin 3v3
SDA pin SDA +10k to 3v3
SDO pin GND
SCL pin SCL +10k to 3v3
3v3 VCC
GND GND
*/
#define _2g (4)
#define _4g (8)
#define _8g (16)
#define _16g (32)
#include <Wire.h>
#define DEVICE (0x53) //ADXL345 device address
#define TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis)
#define samples (100)
float x1, y1, z1;
int sum_x, sum_y, sum_z;
int j;
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
int regAddress;
int x, y, z;
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
//Turning on the ADXL345
writeTo(DEVICE, 0x31, 0b00001011);//0b00001011 self mode off with full resolution 16g
writeTo(DEVICE, 0x2D, 0x08);
writeTo(DEVICE, 0x2E, 0x80);
regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
int sum_x = 0;
int sum_y = 0;
int sum_z = 0;
}
void loop()
{
// readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
/* x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3]) << 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
*/
for(j = 0; j < samples; j++)
{
readFrom(DEVICE, regAddress, TO_READ, buff); //read the acceleration data from the ADXL345
//each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!!
//thus we are converting both bytes in to one int
x = (((int)buff[1]) << 8) | buff[0];
y = (((int)buff[3]) << 8) | buff[2];
z = (((int)buff[5]) << 8) | buff[4];
sum_x += x;
sum_y += y;
sum_z += z;
}
x1 = sum_x / samples;
y1 = sum_y / samples;
z1 = sum_z / samples;
sum_x = 0;
sum_y = 0;
sum_z = 0;
// Serial.println("Start");
Serial.print(x1, 2);
Serial.print('\t');
Serial.print(y1, 2);
Serial.print('\t');
Serial.print(z1, 2);
Serial.print('\t');
Serial.println();
delay(20);//It appears that delay is needed in order not to clog the port
}
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); // send register address
Wire.write(val); // send value to write
Wire.endTransmission(); //end transmission
}
void readFrom(int device, byte address, int num, byte buff[]) {
Wire.beginTransmission(device); //start transmission to device
Wire.write(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(device); //start transmission to device (initiate again)
Wire.requestFrom(device, num); // request 6 bytes from device
int i = 0;
while(Wire.available()) //device may send less than requested (abnormal)
{
buff[i] = Wire.read(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
if I define samples=100
I have this output:
-220.00 -268.00 140.00
-221.00 -267.00 139.00
-221.00 -268.00 140.00
-221.00 -268.00 139.00
-221.00 -268.00 139.00
-221.00 -267.00 141.00
-221.00 -268.00 139.00
-220.00 -268.00 139.00
-221.00 -267.00 139.00
-221.00 -268.00 141.00
-221.00 -267.00 139.00
-221.00 -268.00 139.00
if I define samples=10
I have this output:
-222.00 -269.00 796.00
-222.00 -270.00 794.00
-221.00 -269.00 796.00
-221.00 -267.00 793.00
-221.00 -270.00 794.00
-222.00 -270.00 794.00
-223.00 -267.00 796.00
-222.00 -269.00 794.00
-221.00 -268.00 795.00
-222.00 -269.00 794.00
-222.00 -269.00 794.00
-221.00 -270.00 795.00
someone can me explain that
Stefano