• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Is the accelerometer maximum adjustable?

Someone asked me to look into this for them and I'm not sure how to figure this out. Apparently they are using the accelerometer to measure the number of "g's". They've tried using a few different phones and found that they seem to be capped on what they can measure. Is some cases they couldn't measure any more than 2g's. Does anyone know if this is info is accessible somehow?

I started writing a program to test what my own phone is going. I accessed and displayed the accelerometer data. I'm trying to store the display the max value of each axis but I'm doing something wrong. Here is the code. Note, that I only have worked on the x axis max at this point.

Code:
public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);

        final float[] mValuesMagnet = new float[3];
        final float[] mValuesAccel = new float[3];
        final float[] mValuesOrientation = new float[3];
        final float[] mRotationMatrix = new float[9];
        final double xmax = 0;
        final double xmaxold = 0;


        final TextView txt1 = (TextView) findViewById(R.id.textView1);
        final TextView txt2 = (TextView) findViewById(R.id.textView2);
        final TextView txt3 = (TextView) findViewById(R.id.textView3);
        final TextView txt4 = (TextView) findViewById(R.id.textView4);

        final SensorEventListener mEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }

            public void onSensorChanged(SensorEvent event)
            {
                switch (event.sensor.getType())
                {
                    case Sensor.TYPE_ACCELEROMETER:
                        System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                        break;

                    case Sensor.TYPE_MAGNETIC_FIELD:
                        System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                        break;
                }
                SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
                SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);

                DecimalFormat precision = new DecimalFormat("0.0");
                final CharSequence gravdisplayx;
                final CharSequence gravdisplayy;
                final CharSequence gravdisplayz;
                final CharSequence xmaximum;
                double gravityx;
                double gravityy;
                double gravityz;


                gravityx = mValuesAccel[0];
                gravityy = mValuesAccel[1];
                gravityz = mValuesAccel[2];

                double xmax = max(gravityx,xmaxold);


                gravdisplayx = "X: " + precision.format(gravityx);
                gravdisplayy = "Y: " + precision.format(gravityy);
                gravdisplayz = "Z: " + precision.format(gravityz);
                xmaximum = "X Max: " + precision.format(xmax);

                txt1.setText(gravdisplayx);
                txt2.setText(gravdisplayy);
                txt3.setText(gravdisplayz);
                txt4.setText(xmaximum);
            }
            ;
        };

        setListners(sensorManager, mEventListener);
    }

    public static double max(double gravityx, double xmaxold)
    {
        double absgravx = Math.abs(gravityx);
        double result;
        if(absgravx > xmaxold)
        {
           result = absgravx;
        }
        else
        {
            result = xmaxold;
        }
     return result;
    }

    public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
    {
        sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

}
 
What happens when you run this code?

The accelerometer data for each axis is displayed correctly and changes appropriately. The max value for x that I want is just essentially displaying the absolute value of that data, it's not holding the highest value to the display. I know I have a problem there, just not sure what to do with it to make it do that. And as for my original question, I don't know what the limitation are on the values, the phone's sensors, behind the scenes specifications, etc...since the person who asked me about this told me they saw different results on different phones.
 
I've updated the code as I found a way to display the max range of the phone, rather than trying to find out by measurement, and I also got rid of some items that were in my original code that weren't need (I copied and pasted from another project of mine). In my phone's case, it is 19.6133 m/s^2, or 2g. Here is the code:

Code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.TextView;
import java.text.DecimalFormat;

import static android.hardware.Sensor.TYPE_ACCELEROMETER;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);


        final float[] mValuesAccel = new float[3];

        final String maxG = "Max measurable G: " +
                sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER).getMaximumRange();

        final TextView txt1 = (TextView) findViewById(R.id.textView1);
        final TextView txt2 = (TextView) findViewById(R.id.textView2);
        final TextView txt3 = (TextView) findViewById(R.id.textView3);
        final TextView txt5 = (TextView) findViewById(R.id.textView5);

        final SensorEventListener mEventListener = new SensorEventListener() {
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }

            public void onSensorChanged(SensorEvent event)
            {
                switch (event.sensor.getType())
                {
                    case TYPE_ACCELEROMETER:
                        System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                        break;


                }


                DecimalFormat precision = new DecimalFormat("0.0");
                final CharSequence gravdisplayx;
                final CharSequence gravdisplayy;
                final CharSequence gravdisplayz;

                double gravityx;
                double gravityy;
                double gravityz;

                gravityx = mValuesAccel[0];
                gravityy = mValuesAccel[1];
                gravityz = mValuesAccel[2];

                gravdisplayx = "X: " + precision.format(gravityx);
                gravdisplayy = "Y: " + precision.format(gravityy);
                gravdisplayz = "Z: " + precision.format(gravityz);

                txt1.setText(gravdisplayx);
                txt2.setText(gravdisplayy);
                txt3.setText(gravdisplayz);
                txt5.setText(maxG);
            }
            ;
        };

        setListners(sensorManager, mEventListener);
    }

    public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
    {
        sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        
    }

}

As I asked before, is there a way to adjust this range? From what I can find, most of these phones have accelerometers that are capable of 16g, but are set to lower levels. Pretty much all of the threads I can find anywhere on this are old, 5+ years and generally state that it can't be changed. Has that changed this then?
 
Back
Top Bottom