Accelerometer Project

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
AlyMirza
 
Posts: 4
Joined: Mon Jun 02, 2014 11:11 am

Accelerometer Project

Post by AlyMirza »

Hi,

Im currently using the ADXL377 for an arduino project im working on. I have calibrated my sensor using the sketch uploaded on the tutorial page. My concern is how do I use the ranges and raw values from my calibration so I can move forward with my project? Also, since I was talking about calibration I think my values from the calibration sketch are weird:
Raw Ranges: X: 512-513, Y: 511-512, Z: 511-512
512, 511, 512 :: -1.00G, -1.00G, 1.00G
Raw Ranges: X: 504-514, Y: 508-512, Z: 506-513
508, 511, 512 :: -0.20G, 0.50G, 0.71G
Raw Ranges: X: 504-514, Y: 508-514, Z: 506-523
509, 514, 512 :: 0.00G, 1.00G, -0.29G
Raw Ranges: X: 504-514, Y: 507-514, Z: 506-523
510, 509, 512 :: 0.20G, -0.43G, -0.29G
Raw Ranges: X: 504-514, Y: 507-514, Z: 506-523
510, 511, 514 :: 0.20G, 0.14G, -0.06G
Raw Ranges: X: 504-514, Y: 507-514, Z: 506-523
510, 511, 510 :: 0.20G, 0.14G, -0.53G
Raw Ranges: X: 504-514, Y: 507-514, Z: 506-523
512, 511, 512 :: 0.60G, 0.14G, -0.29G

Here is the code I used:

Code: Select all

const int xInput = A2;
const int yInput = A1;
const int zInput = A0;

int flag;

// Raw Ranges:
// initialize to mid-range and allow calibration to
// find the minimum and maximum for each axis
int xRawMin = 512;
int xRawMax = 512;

int yRawMin = 512;
int yRawMax = 512;

int zRawMin = 512;
int zRawMax = 512;

// Take multiple samples to reduce noise
const int sampleSize = 10;

void setup() 
{
  analogReference(EXTERNAL);
  Serial.begin(9600);
}

void loop() 
{
  int xRaw = ReadAxis(xInput); xRaw = ReadAxis(xInput);
  int yRaw = ReadAxis(yInput); yRaw = ReadAxis(yInput);
  int zRaw = ReadAxis(zInput); zRaw = ReadAxis(zInput);
 if(Serial.read()-'0'==1)
 {
    Serial.print("Raw Ranges: X: ");
    Serial.print(xRawMin);
    Serial.print("-");
    Serial.print(xRawMax);
    
    Serial.print(", Y: ");
    Serial.print(yRawMin);
    Serial.print("-");
    Serial.print(yRawMax);
    
    Serial.print(", Z: ");
    Serial.print(zRawMin);
    Serial.print("-");
    Serial.print(zRawMax);
    Serial.println();
    Serial.print(xRaw);
    Serial.print(", ");
    Serial.print(yRaw);
    Serial.print(", ");
    Serial.print(zRaw);
    
    // Convert raw values to 'milli-Gs"
    long xScaled = map(xRaw, xRawMin, xRawMax, -1000, 1000);
    long yScaled = map(yRaw, yRawMin, yRawMax, -1000, 1000);
    long zScaled = map(zRaw, zRawMin, zRawMax, -1000, 1000);
  
    // re-scale to fractional Gs
    float xAccel = xScaled / 1000.0;
    float yAccel = yScaled / 1000.0;
    float zAccel = zScaled / 1000.0;
  
    Serial.print(" :: ");
    Serial.print(xAccel);
    Serial.print("G, ");
    Serial.print(yAccel);
    Serial.print("G, ");
    Serial.print(zAccel);
    Serial.println("G");
     delay(500);
 }
 else
 {
   AutoCalibrate(xRaw, yRaw, zRaw); 
 }
  
}

//
// Read "sampleSize" samples and report the average
//
int ReadAxis(int axisPin)
{
  long reading = 0;
  analogRead(axisPin);
  delay(1);
  for (int i = 0; i < sampleSize; i++)
  {
    reading += analogRead(axisPin);
  }
  return reading/sampleSize;
}

//
// Find the extreme raw readings from each axis
//
void AutoCalibrate(int xRaw, int yRaw, int zRaw)
{
  //Serial.println("Calibrate");
  if (xRaw < xRawMin)
  {
    xRawMin = xRaw;
  }
  if (xRaw > xRawMax)
  {
    xRawMax = xRaw;
  }
  
  if (yRaw < yRawMin)
  {
    yRawMin = yRaw;
  }
  if (yRaw > yRawMax)
  {
    yRawMax = yRaw;
  }

  if (zRaw < zRawMin)
  {
    zRawMin = zRaw;
  }
  if (zRaw > zRawMax)
  {
    zRawMax = zRaw;
  }
}

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Accelerometer Project

Post by adafruit_support_bill »

The following formula can be applied for all 3 axis:

float RawLow = (reading at -1G)
float RawRange = (reading at 1G - reading at -1G)
float ScaleRange = 2.0;

Then you can scale any reading using the formula:

float Accel = ((RawReading - RawLow) * (ScaleRange / RawRange)) + ScaleLow ;

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”