Magnitude

We have been working with the direction of the magnetic field but what’s its real magnitude? Thenumber that the magnetic_field function reports are unit-less. How can we convert those values toGauss?

The documentation will answer that question.

Section 2.1 Sensor characteristics - Page 10 - LSM303DLHC Data Sheet

The table in that page shows a magnetic gain setting that has different values according to thevalues of the GN bits. By default, those GN bits are set to 001. That means that magnetic gain ofthe X and Y axes is 1100 LSB / Gauss and the magnetic gain of the Z axis is 980 LSB / Gauss. LSBstands for Least Significant Bits and the 1100 LSB / Gauss number indicates that a reading of1100 is equivalent to 1 Gauss, a reading of 2200 is equivalent to 2 Gauss and so on.

So, what we need to do is divide the X, Y and Z values that the sensor outputs by its correspondinggain. Then, we’ll have the X, Y and Z components of the magnetic field in Gauss.

With some extra math we can retrieve the magnitude of the magnetic field from its X, Y and Zcomponents:

  1. let magnitude = (x * x + y * y + z * z).sqrt();

Putting all this together in a program:

  1. #![deny(unsafe_code)]
  2. #![no_main]
  3. #![no_std]
  4. #[allow(unused_imports)]
  5. use aux15::{entry, iprint, iprintln, prelude::*, I16x3};
  6. use m::Float;
  7. #[entry]
  8. fn main() -> ! {
  9. const XY_GAIN: f32 = 1100.; // LSB / G
  10. const Z_GAIN: f32 = 980.; // LSB / G
  11. let (_leds, mut lsm303dlhc, mut delay, mut itm) = aux15::init();
  12. loop {
  13. let I16x3 { x, y, z } = lsm303dlhc.mag().unwrap();
  14. let x = f32::from(x) / XY_GAIN;
  15. let y = f32::from(y) / XY_GAIN;
  16. let z = f32::from(z) / Z_GAIN;
  17. let mag = (x * x + y * y + z * z).sqrt();
  18. iprintln!(&mut itm.stim[0], "{} mG", mag * 1_000.);
  19. delay.delay_ms(500_u16);
  20. }
  21. }

This program will report the magnitude (strength) of the magnetic field in milligauss (mG). Themagnitude of the Earth’s magnetic field is in the range of 250 mG to 650 mG (the magnitudevaries depending on your geographical location) so you should see a value in that range or close tothat range — I see a magnitude of around 210 mG.

Some questions:

Without moving the board, what value do you see? Do you always see the same value?

If you rotate the board, does the magnitude change? Should it change?