This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How anomaly ADXL345 data UART? needed help

Here is calibration of UART data using ADXL345.i2c calibration

and the data is followed by x, y & z value, for example x = -42, y = 20, z = 268. and then after calibrated 10x which is I got mean value from (x,y,z*10 of data)/10.0.. I've got x = -26237, y = -18, z = 278.

and my question is why the data of x value is large to calculated, perhaps it's just maybe around -45 (above/lower).

anyone who can explain to fix it..

here is the code:

int calibratex()
{
  int readings[3] = {0, 0, 0};
  led1=1;
  long int sum1  = 0;
  for (int i=0; i<10; i++)
 {
   accelerometer.getOutput(readings);
   xval[i]=(readings[0]);  //nilai x adxl345
   sum1 = yval[i]+sum1;
 }

  xavg=sum1/10.0;

  led1=0;     
  return xavg;
}
 int calibratey()
 {
  int readings[3] = {0, 0, 0};
  led1=1;
  long int sum2 = 0;
  for (int i=0; i<10; i++)
  {
   accelerometer.getOutput(readings);
    yval[i]=(readings[1]); //nilai y adxl345
   sum2 = yval[i]+sum2;
 }
   yavg=sum2/10.0;
   led1=0;     
   return yavg;
 }
  int calibratez()
  {
     int readings[3] = {0, 0, 0};

     led1=1;
     long int sum3 = 0;
     for (int i=0; i<10; i++)
     {
      accelerometer.getOutput(readings);
      zval[i]=(readings[2]); //nilai z adxl345
      sum3 = zval[i]+sum3;
  }

    zavg=sum3/10.0;
    led1=0;     
    return zavg;
 }
  • Not sure if this is the solution, but there is an inconsistency in your three calibration functions:

    int calibratex()
    {
    	int readings[3] = {0, 0, 0};
    	led1=1;
    	long int sum1  = 0;
    	for (int i=0; i<10; i++)
    	{
    		accelerometer.getOutput(readings);
    		xval[i]=(readings[0]);  //nilai x adxl345
    		sum1 = yval[i]+sum1;                                     // <- BUG???? Should it be xval?
    	}
    
    	xavg=sum1/10.0;
    
    	led1=0;     
    	return xavg;
    }
    
  • thank you for your answer, my code is fine.. sorry for just just wrong variable in my code statement. this is the full link for my mbed code. but why there is wrong calculation for x array.

    for example I have x=-27, y=48, z 280.. after 10x mean calcutation. that's show x=-25882, y = 47, z=220. the main problem is why x calculate -25882 ? after 10x mean calculation must be -25 without 822 (-25,882) for example..

    how to replace last 3 value of the sensor?

  • Have you tried to print both the raw x value and your mean x value? Remember that you will always have the gravitational force of 1g and that will show on one of your axes. Depending on your range settings x=-25822 might actually be correct and just your accelerometer measuring gravity.

  • can you explain it with the code? because the problem of sensor is located just at x array.. I thinks It's not about hardware problem but after change from 2's complement to array string.. perhaps explain the raw x value with the mean

  • Your calibratex, y, and z functions look identical to me. Hence it must be something weird going on with your input value. You only call calibratex() once, and that is immediately after you have powered on the ADXL345. Maybe the sensor havn't had the time to sample a valid value yet. It could be interesting to see what would happen if you put a small delay between these two lines

    accelerometer.setPowerControl(0x08);   
    
    int avg1 = calibratex();
    

    It could also be worth trying to call calibratex() from within your while(1) loop and monitor the values at regular intervals.

Related