ADXL345 i2c reading only x and y axis

I just tried playing with this ADXL3 I2C module. have used the program and circuit described here:

for some reason, i'm getting a good read from x and y, but z read is constant and equals to 511.

i'm using arduino nano, SDA is connected to A4, SCL connected to A5.

Any idea why is that and how to fix it?

have used the program and circuit described here

What program? Have you read the rules?

We need to know what you actually did not what you followed.
There are only two answers that can be given so far:-

  1. You didn't do what it said, but without a schematic and photograph of your layout how are we to know?
  2. Your board is broken.

Please read this:-
How to use this forum

i've attached a photo of my circuit. the module is ADXL345 - data sheet at:

http://www.analog.com/media/en/technical-documentation/data-sheets/ADXL345.pdf

my sketch code:

#include <Wire.h>

#define accel_module (0x53)
#define sensitivity 20

byte values[6];
char output[512];
int oldx = 0;
int oldy = 0;

void setup() {
 Wire.begin();
 Serial.begin(115200);
 
 Wire.beginTransmission(accel_module);
 Wire.write(0x2D);
 Wire.write(0);
 Wire.endTransmission();

 Wire.beginTransmission(accel_module);
 Wire.write(0x2D);
 Wire.write(16);
 Wire.endTransmission();

 Wire.beginTransmission(accel_module);
 Wire.write(0x2D);
 Wire.write(8);
 Wire.endTransmission();
 Serial.println("Restarted");
}


void loop() {
 int xyzregister = 0x32;
 int x,y,z;
 long now = millis();

 Wire.beginTransmission(accel_module);
 Wire.write(xyzregister);
 Wire.endTransmission();

 Wire.beginTransmission(accel_module);
 Wire.requestFrom(accel_module, 6);

 int i=0;
 while(Wire.available())
 {
   values[i] = Wire.read();
   i++;
 }
 Wire.endTransmission();

 x = (((int)values[1]) << 8) | values[0];
 y = (((int)values[3]) << 8) | values[2];
 z = (((int)values[5]) << 8) | values[4];
 if ((x - oldx > sensitivity) || (oldx - x > sensitivity) || (y - oldy > sensitivity) || (oldy - y > sensitivity))
 {
   oldx=x;
   oldy=y;
   sprintf(output, "%d %d %d", x+80, y+257, z);
   Serial.println(output);
 }
 delay(20);
}

Please edit your post and enclose the code in code tags (use the "</>" button).

jremington:
Please edit your post and enclose the code in code tags (use the "</>" button).

Done.

The 511 along z may just be the acceleration due to gravity, which you will always be measuring.

There is a fair amount of junk code in that program that is actually obscuring what the device is measuring.

I removed that junk. Try the following and while the program is running, orient the accelerometer in different directions. (I don't have an ADXL345 to test).

#include <Wire.h>

#define accel_module (0x53)

byte values[6];
char output[50];  //sprintf buffer

void setup() {

 Wire.begin();
 Serial.begin(115200);
 
 Wire.beginTransmission(accel_module);
 Wire.write(0x2D);
 Wire.write(0);
 Wire.endTransmission();

 Wire.beginTransmission(accel_module);
 Wire.write(0x2D);
 Wire.write(16);
 Wire.endTransmission();

 Wire.beginTransmission(accel_module);
 Wire.write(0x2D);
 Wire.write(8);
 Wire.endTransmission();
 Serial.println("Restarted");
}


void loop() {
 int xyzregister = 0x32;

 int x,y,z;


 Wire.beginTransmission(accel_module);
 Wire.write(xyzregister);
 Wire.endTransmission();

 Wire.beginTransmission(accel_module);
 Wire.requestFrom(accel_module, 6);

 int i=0;
 while(Wire.available())
 {
   values[i] = Wire.read();
   i++;
 }
 Wire.endTransmission();

if (i < 5) Serial.println("too few values returned");

 x = (((int)values[1]) << 8) | values[0];
 y = (((int)values[3]) << 8) | values[2];
 z = (((int)values[5]) << 8) | values[4];
 
   sprintf(output, "%d %d %d", x, y, z);
   Serial.println(output);
 delay(100);
}

Actually, the code was not junk - it was meant to write to serial only in certain angles' jumps and it works great.

I just tried your code - it behaves the same - no change in output of Z which is always 511 no matter what - even if I move it fast from high point to bottom, so I don't believe it is a gravity issue that holds it with this value.

Actually, the code was not junk - it was meant to write to serial only in certain angles' jumps and it works great.

Until you understand the readings, it makes no sense to modify or hide them.
What happens when the X axis points down?

when the ADXL345 text on the module is facing to me, tilting forward makes Y go higher (backward = lower),
tilting right makes X go higher (left = lower).

Z is always 511.

This seems like pulling teeth. What do X and Y actually read when they are pointing straight up and down?

It does sound like your module is defective. Each axis should display the equivalent of zero g when horizontal, and +/- 1 g when pointing up or down.

If you are using this with a 5V Arduino, does the module have 5V to 3.3V level shifters on all connectors?

that's the reason i used x+80, y+257 in the code i've posted - in order to make the x and y equal to 0 when it is resting straight on the table. this way it displayed the rotation correctly.

by not doing so - the readings are (when the module is "resting" on the table) x=-257 and y=-80 (approx.)

i didn't understand what you meant by: "does the module have 5V to 3.3V level shifters on all connectors".

i'll check the values when pointing at these points and post it back - in what way can it matter to the problem? do you have any lead?

Thanks.

impeham:
I didn't understand what you meant by: "does the module have 5V to 3.3V level shifters on all connectors".

See:
I²C bi-directional level shifter.

The accelerometer runs on 3.3V and will be destroyed if connected to a 5V Arduino without level shifters.

jremington:
The accelerometer runs on 3.3V and will be destroyed if connected to a 5V Arduino without level shifters.

Its Vcc and I²C pull-up resistors are connected to 3.3V from the Arduino Nano as shown in the photograph attached to post #3.

I bought 2 ADXL354 off Ebay. Both with a defect Z-axis. I suspect there is a broken batch on the market

chip ID

345B
#137
9372
PHIL

and

345B
#136
9860
PHIL

both defect Z-Axis