Tesla value of the magnetic field drops in the vehicle about 20 microtesla

357 views Asked by At

I have implemented a snippet code to calculate the Tesla value of the magnetic field. I have tested it recently and I noticed when I am in the bus the value is 24 but when I am outside the value is about 45. I mean the bus is metal so the value should be higher inside of it. Is there any explanation for this strange behaviour?

I appreciate any help.

Code:

public class MainActivity extends ActionBarActivity implements
        SensorEventListener {
    String telsaString;

    private TextView magneticX;
    private TextView magneticY;
    private TextView magneticZ;
    private TextView magneticT;
    private SensorManager sensorManager = null;

    private long lastUpdate = 0;

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

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        // Capture magnetic sensor related view elements
        magneticX = (TextView) findViewById(R.id.valMag_X);
        magneticY = (TextView) findViewById(R.id.valMag_Y);
        magneticZ = (TextView) findViewById(R.id.valMag_Z);
        magneticT = (TextView) findViewById(R.id.valMag_T);

        // Register magnetic sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);


    }

    @Override
    protected void onPause() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onPause();
    }

    @Override
    protected void onStop() {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Register magnetic sensor
        sensorManager.registerListener(this,
                sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    public void onSensorChanged(SensorEvent sensorEvent) {
        synchronized (this) {

            float magX = sensorEvent.values[0];

            float magY = sensorEvent.values[1];

            float magZ = sensorEvent.values[2];

            magneticX.setText(Float.toString(sensorEvent.values[0]));
            magneticY.setText(Float.toString(sensorEvent.values[1]));
            magneticZ.setText(Float.toString(sensorEvent.values[2]));

            int teslaXYZ = (int)(Math.sqrt((magX * magX) + (magY * magY)
                    + (magZ * magZ)));
            String str = String.valueOf(teslaXYZ);
            magneticT.setText(str);

            long curTime = System.currentTimeMillis();
            if ((curTime - lastUpdate) > 15000) {

                lastUpdate = curTime;

                try {
                    JSONObject tesla = new JSONObject();
                    tesla.put("tesla", teslaXYZ);
                    //tesla.put("tesla", teslaXYZ);
                    telsaString = tesla.toString();
                    new MyAsyncTask().execute(telsaString);

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }
    }
1

There are 1 answers

0
Svj0hn On

A vehicle like a bus will contain many metal parts, some of which will be magnetized and have their own magnetic fields. The magnetometer of your device will pick up the superposition of the geomagnetic field and these additional fields and will show bad directions and absolute values compared to when you're standing away from the vehicle.

The engine and electric infrastructure of the car can also cause interference (While testing, I noticed a clear difference in magnetometer readings when starting the engine of my car).