Sensor problem

I use the Arduino Leonardo to measure the acceleration of ADXL335, but the value are all negative however I move the ADXL335.Please help me solve the problem.

This is my code:

const int xpin=A1;
const int ypin=A2;
const int zpin=A3;
int sampleDelay=500;
void setup() {
Serial.begin(9600);
pinMode(xpin,INPUT);
pinMode(ypin,INPUT);
pinMode(zpin,INPUT);
}

void loop() {
int x=analogRead(xpin);
delay(1);
int y=analogRead(ypin);
delay(1);
int z=analogRead(zpin);
delay(1);
float zero_G=512.0;
float scale=102.3;
Serial.print(((float)x-zero_G)/scale);
Serial.print("\t");
Serial.print(((float)y-zero_G)/scale);
Serial.print("\t");
Serial.print(((float)z-zero_G)/scale);
Serial.print("\n");
delay(sampleDelay);

}

1.png

2.png

Is that a voltage regulator for 3.3V on the sensor board ?
How do you power it ? Using the 3.3V pin from the Arduino board ?

The example code is seems to be for an 3.3V Arduino, and you use a 5V Arduino board.

I use the 3.3v, maybe the picture is vague.

The Arduino 3.3V pin to the sensor module ?
The voltage regulator on the sensor module has a voltage drop, and outputs perhaps 3.2V for the sensor. When the ADXL335 sensor runs at 3.2V, the analog output is 1.60V (for zero acceleration/gravity).

The Arduino is running at 5.0V, and your sketch assumes that the voltage is 2.50 for zero acceleration.

To get accurate results, use the Arduino 5V pin to power the sensor module.
You also need to be sure that the Arduino is running at 5.0V (not 4.5V or so). You might need a power supply 7.5V to 9V to the DC barrel jack to be sure that the Arduino is running at 5.0V.

After that, you have to fix your sketch for an input of 0V to 3.3V.

Sorry if I made it less easier. It would be no problem when a 3.3V Arduino was used and the sensor module was without voltage regulator. It is also possible to set the reference voltage of the Arduino to 3.3V, then it would still be accurate when the Arduino 5V is only 4.5V. However, settings the reference to 3.3V is dangerous, it could cause an internal shortcut in the microcontroller when the sketch is wrong.

At Adafruit the reference voltage is set to the 3.3V from the sensor module. That is even better. After that they run a calibration, which make it even more accurate.

My ADXL model is different from the adafruit, but thank you.

const int ap1 = A1; 
const int ap2 = A2;
const int ap3 = A3;

int sv1 = 0;        
int ov1 = 0;    
int sv2 = 0;      
int ov2= 0;      
int sv3 = 0;       
int ov3= 0;      

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
 
}

void loop() {
  analogReference(EXTERNAL);    //connect 3.3v to AREF
  // read the analog in value:
  sv1 = analogRead(ap1);            
  // map it to the range of the analog out:
  ov1 = map(sv1, 0, 1023, 0, 255);  
  // change the analog out value:
  delay(2);                     
  //
  sv2 = analogRead(ap2);            
  
  ov2 = map(sv2, 0, 1023, 0, 255); 
 // 
  delay(2);                 
  //
  sv3 = analogRead(ap3);            
  
  ov3 = map(sv3, 0, 1023, 0, 255);  
  
  // print the results to the serial monitor:
  Serial.print("Xsensor1 = " );                       
  Serial.print(sv1);      
  Serial.print("\t output1 = ");      
  Serial.println(ov1);   
 
  Serial.print("Ysensor2 = " );                       
  Serial.print(sv2);      
  Serial.print("\t output2 = ");      
  Serial.println(ov2);   

  Serial.print("Zsensor3 = " );                       
  Serial.print(sv3);      
  Serial.print("\t output3 = ");      
  Serial.println(ov3);   

  delay(3000);                     
  
}

Here is sample code to test out. please share serial output this time for code

When the ADXL model is static I get the first picture, when I move the model I get the second picture.

1.PNG

2.PNG

I think those values are good.
Did you connect AREF to 3.3V ?

Yes, but why should I link the 3.3v to AREF.

The analogReference is default set to 'DEFAULT', that is the 5V as reference for a 5V Arduino board.

The sensor outputs 0 to 3.3V, since the sensor itself runs at 3.3V.
When the 3.3V is connected to AREF, the function "analogReference(EXTERNAL);" must be called, and the input range for analog inputs becomes 0 to 3.3V.