#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)
byte buff[TO_READ] ; //6 bytes buffer for saving data read from the device
char str[512]; //string buffer to transform data before sending it to the serial port
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
//Turning on the ADXL345
writeTo(DEVICE, 0x2D, 0);
writeTo(DEVICE, 0x2D, 16);
writeTo(DEVICE, 0x2D, 8);
}
void loop()
{
int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345
int x, y, z;
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];
//we send the x y z values as a string to the serial port
sprintf(str, "%d %d %d", x, y, z);
Serial.print(str);
Serial.print(10, BYTE);
//It appears that delay is needed in order not to clog the port
delay(15);
}
//---------------- Functions
//Writes val to address register on device
void writeTo(int device, byte address, byte val) {
Wire.beginTransmission(device); //start transmission to device
Wire.send(address); // send register address
Wire.send(val); // send value to write
Wire.endTransmission(); //end transmission
}
//reads num bytes starting from address register on device in to buff array
void readFrom(int device, byte address, int num, byte buff[]) {
Wire.beginTransmission(device); //start transmission to device
Wire.send(address); //sends address to read from
Wire.endTransmission(); //end transmission
Wire.beginTransmission(device); //start transmission to device
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.receive(); // receive a byte
i++;
}
Wire.endTransmission(); //end transmission
}
connected it that way (sorry for the huge picture, but it's the only way to see the details...):
What leaps out at me is how you have the wires connected to the 3.3V/5V converter board. Are those wires just plugged through the holes into the breadboard below? I wouldn't expect them to make contact at all that way, let alone the kind of reliable contact you're going to need.
Hi, you have the ADXL connected through another unit which I'm not familiar with.
Have you tried to connect the ADXL directly to the Arduino? If not please try and post both the wiring and the results.
Thanks for helping me here
I already tried your circuit without more success, but can send you a picture:
Black: GND
Orange: 3,3V
Green: Analog 4
Yellow: Analog 5
Very strange, I can't find the problem in the picture. Did you try to reset the Arduino etc? I mean did all the regular stuff when things ain't working?
Ok, I should have clarified that I built the circuit and it didn't work. My brother started from scratch and it didn't work. Then I looked at my brother's layout and spotted the error, then it all worked magically I also re-downloaded the code to make sure I had a clean version after I had tried to debug the previous attempts. I am also doing this on an Arduino Mega, if that makes any difference.
I took a look at your photo and compared it to mine and I couldn't spot anything, but don't take that to mean much. This is my first project and I don't necessarily understand everything I am doing on the breadboard.
I was reading the ADXL345 datasheet for the bazillionth time and I something occured to me about your picture. Are you making CS high? That should select the I2C mode. I see a wire coming off your CS pin, but I can't quite see where it goes. Might not be your issue, but figured I would throw it out there because it is easy to check.
Dude, I've rechecked all the things. Everything works fine on this end.
I've had the same problem (all 0's all the time) when I didn't "wake up" the ADXL, but the first 3 writeTo() function in setup should be provide a solution to that. So I don't really know what else could it be.
I'd do all the procedures before sending out the item for replacement.
Check the cables integrity (one of the grounds or the powers might not be properly connected). Reinstall Arduino etc.
Please post an update if you find a solution to this.
Since I am playing with the same code and same accelerometer, I thought I would post a couple of things I have learned in case they might help you or others. First is that the default bus speed on the ADXL is 100mhz for the I2C communication. If you set this to 400mhz, everything works well and you get lots more data lots more quickly:
//Set the 400hz bus speed for I2C
writeTo(DEVICE, REG_BW_RATE, 0xC);
0x0C is 400mhz, 0x0A is the default at 100mhz.
Second, the code as written is sending data out to the serial port at 9600 baud. The serial output becomes a bottleneck quickly. I upped the rate to 115200 and this helped, but not enough. The real solution was having a large string buffer I write out my serial data and then dump to the dataport as soon as it is full (about every 100 ms). This gives me better resolution most of the time. Right now I am doing this so I can crunch the data I am getting on my PC, but eventually everything will happen on the arduino so I won't need a serial out.
If you don't need the performance I am looking for, then feel free to ingore, but I thought this was a good place to share my discoveries. I am primarily using the ADXL345 as a shock sensor, so I need pretty good resolution so I don't miss shocks that are overwith very suddenly.