Reading incorrect values when using ADXL335 with the Photon

Hello All
I have a question about the ADXL335.
I am trying to use the ADXL335 with the Particle Photon and reading the (x,y,z) using the docklight serial communication software and the Particle Console.
The values that I am getting is not correct!. These are samples for the values that I got
25.13 19.28 24.00
25.07 19.28 23.97
25.05 19.25 23.99
25.10 19.25 24.00
which looks not correct!
However, if I connect the ADXL335 with the Arduino Uno. I got these values which looks correct
0.19 -0.87 -0.28
-0.10 -0.56 -0.31
0.04 -0.12 1.07
0.02 0.18 1.19
-0.82 0.09 0.16
-0.85 0.09 -0.07
-0.93 0.15 0.04
0.79 0.20 0.84
Please any advice?
Here is the code that I’m using

note:vcc-->5v ,but ADXL335 Vs is 3.3V
The circuit:
      5V: VCC
analog 0: x-axis
analog 1: y-axis
analog 2: z-axis
After burning the program, open the serial monitor debugging window, where you can see the data detected being displayed. When the acceleration varies, the figure will vary accordingly.
const int xpin = A0;                  // x-axis of the accelerometer
const int ypin = A1;                  // y-axis
const int zpin = A2;                  // z-axis (only on 3-axis models)
void setup()
 // initialize the serial communications:
void loop()
 int x = analogRead(xpin);  //read from xpin
 delay(1); //
 int y = analogRead(ypin);  //read from ypin
 int z = analogRead(zpin);  //read from zpin
 float zero_G = 338.0; //ADXL335 power supply by Vs 3.3V:3.3V/5V*1024=676/2=338
float zero_Gx=331.5;//the zero_G output of x axis:(x_max + x_min)/2
float zero_Gy=329.5;//the zero_G outgput of y axis:(y_max + y_min)/2
float zero_Gz=340.0;//the zero_G output of z axis:(z_max + z_min)/2

float scale = 67.6;//power supply by Vs 3.3V:3.3v /5v *1024/3.3v *330mv/g =67.6g
float scale_x = 65;//the scale of x axis: x_max/3.3v*330mv/g
float scale_y = 68.5;//the scale of y axis: y_max/3.3v*330mv/g
float scale_z = 68;//the scale of z axis: z_max/3.3v*330mv/g

Serial.print(((float)x - zero_Gx)/scale_x);  //print x value on serial monitor
Serial.print(((float)y - zero_Gy)/scale_y);  //print y value on serial monitor
Serial.print(((float)z - zero_Gz)/scale_z);  //print z value on serial monitor
delay(1000);  //wait for 1 second 

@ScruffR @peekay123 @Moors7
Thanks all in advance

@rabee.hagem2017, is it the exact same code running on the Arduino?

1 Like

Yes it is the same code.

float scale = 67.6;//power supply by Vs 3.3V:3.3v /5v *1024/3.3v *330mv/g =67.6g

You’ll need to make sure you’re using the correct voltages as well as resolutions for the analog read. Those are not the same as on a Uno.


Do you have any suggestion please?

Try recalculation that formula using the correct data?
Both the voltages used (which might depend on how you hooked it up), as well as the analog read resolution can be found in the docs. Never hurts to take a look at those :wink: