Faulty ADXL337 Data at Rest

Have questions about a SparkFun product or board? This is the place to be.

Moderators: phalanx, TS-Tim

Post Reply
Posts: 1
Joined: Sun Jul 16, 2017 6:22 pm

Faulty ADXL337 Data at Rest

Post by ArduionoHelp » Sun Jul 16, 2017 7:05 pm

Yes, I know I spelled Arduino wrong, I was careless in the registration process, but I do not think I was careless with the code I used following the ADXL337/377 Accelerometer Hookup Guide. I am trying to calibrate the ADXL337 accelerometer at rest for a 3.3V power supply, but I am receiving wonky ADC numbers that are not the typical 512 output (x=328, y=330, z=407). I would like clarification if I have received a faulty accelerometer, or if there are issues with the secondary equipment (i.e. soldering quality, breadboard, cable pins, Arduino board).

Here's an image of the output:

My code is a modified form of the example:

Code: Select all

Code adapted from:
Jordan McConnell
3 July 2014
Code modified by:
Ryan Hegarty
16 July 2017
Code collects raw accelerometer data from x, y, z, axes,
converts data into g-forces, and prints output to Serial Monitor.

int range = 3; // Range of g forces (±3g's) measured by accelerometer

void setup()
  Serial.begin(115200); // Initialize serial communication at 115200 baud

void loop()
  int rawX = analogRead(A0); // Reads raw x axis data
  int rawY = analogRead(A1); // Reads raw y axis data
  int rawZ = analogRead(A2); // Reads raw z axis data
  // Set power source to 3.3V to properly scale data
  float scaledX, scaledY, scaledZ; // Declares scaled variables for each axis
  scaledX = conv(rawX, 0, 1023, -range, range);
  scaledY = conv(rawY, 0, 1023, -range, range);
  scaledZ = conv(rawZ, 0, 1023, -range, range);
  // Print out raw X,Y,Z accelerometer readings
  Serial.print("X: "); Serial.println(rawX);
  Serial.print("Y: "); Serial.println(rawY);
  Serial.print("Z: "); Serial.println(rawZ);
  // Print out scaled X,Y,Z accelerometer readings
  Serial.print("X: "); Serial.print(scaledX); Serial.println(" g");
  Serial.print("Y: "); Serial.print(scaledY); Serial.println(" g");
  Serial.print("Z: "); Serial.print(scaledZ); Serial.println(" g");
  delay(200); // Minimum delay of 0.2 milliseconds between sensor reads (500 Hz)

// Function that converts voltage values to g-forces
// Similar to the Arduino Map function but stores values as decimals instead of integers
float conv(float x, float in_min, float in_max, float out_min, float out_max)
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;

Posts: 315
Joined: Fri Oct 17, 2014 10:02 am
Location: Southern California

Re: Faulty ADXL337 Data at Rest

Post by darrellg » Mon Jul 17, 2017 9:12 am

You're using a 5V microcontroller with a 3.3V signal. Your analog input is never going to get to 1023, because the accelerometer can only output 3.3V, but 1023 would be 5V.

You need to set the analog reference to 3.3V by connecting 3.3V to the AREF pin and tell the Arduino to use that, as described at https://www.arduino.cc/en/Reference/AnalogReference. Once you've done that, 1023 will be 3.3V and you should get the 512-ish expected mid-scale raw analog reading.

Post Reply