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);
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.
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.