STM32F3’s Magnetometer | Use-cases & Reading Extraction (Part-1)

Reading Time: 3 minutes

An embedded system is computer hardware with software embedded in it. Or we can say it is a combination of computer processors, computer memory, and input/output devices and it can be an independent system or a part of a large system.

Hi folks, we are back again with another interesting article where we’ll understand the use-cases of Magnetometer sensor and how to extract readings of this sensor from STM32F3 discovery board.
Alright!!! let’s get started to update our knowledge pool by adding one more interesting row in it.

As we all know, embedded will be the future. Even today also we are surrounded by embedded devices. So by considering these facts we have done a small POC, which has two parts:

  • Extract Magnetometer’s reading from STM32F303VCT6 discovery board
  • Expose the extracted reading to the KAA server

So before discussing POC’s implementation, first let’s understand some areas where we can incorporate magnetometer:

Use-cases of Magnetometer

  • Virtual Reality and Augmented Reality
  • Compass
  • Gesture recognition
  • Navigation
  • Metal detection

Let’s understand above point with detail:

Virtual Reality and Augmented Reality

Magnetometer can be used on head-mounted tracking systems of virtual reality(VR) and augmented reality(AR). These systems can use magnetometer’s readings to help in the calibration of gyroscope readings.


A very common use-case of magnetometer is to create a compass based on its magnetic fields.

Gesture recognition

We can incorporate a magnetometer in gesture-controlled devices as well, like a user can make gestures in a 3D space around the device using a magnet. Therefore the movement of the magnet affects the magnetic field sensed by the compass sensor integrated into the device.
As a result, we can use gestures for sending different interaction commands to the device like turning a page, zooming in/out, etc.

The navigation system uses a magnetometer on mobile devices to detect magnetic fields.

Metal detection

Utility applications can also use magnetometers to detect the presence of metal nearby.

There are so many other use-cases are also present where we can incorporate a magnetometer.

Now, let’s jump into the POC, where we extracted magnetometer’s readings:

Brief on POC:

This POC pertains to extracting readings of the microcontroller’s magnetometer and sending the readings to the cloud server. It has two parts.
The first one is responsible for extracting the readings of the magnetometer and saving the readings into a file with the help of ITM. This part needs to be deployed in the STM32F303VCT6 micro-controller.

The second part is responsible for continuously monitoring the file (which we have used in the first part to store the data) and sending the updated readings to the KAA server after data conversion (packet -> de-packet).

Now let’s discuss the first part where we have extracted the readings from microcontroller’s magnetometer sensor.

Extract & store magnetometer’s reading into a file:

For this, we have used Lsm303agr package to work with magnetometer.
Here is the code snippet which we have used:

fn main() -> ! {

    let (mut lsm303agr, mut itm) = init();

    loop {
        let magnetometer_data = lsm303agr
            .expect("Unable to read magnetometer's data");
            &mut itm.stim[0],
            "{{ x: {:?}, y: {:?} , z: {:?} }}",

pub fn init() -> (
    Lsm303agr<I2cInterface<I2c<I2C1, (PB6<AF4>, PB7<AF4>)>>, mode::MagContinuous>,
) {
    let core_peripheral = cortex_m::Peripherals::take().unwrap();
    let device_peripheral = stm32::Peripherals::take().unwrap();

    let mut flash = device_peripheral.FLASH.constrain();
    let mut rcc = device_peripheral.RCC.constrain();

    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let mut gpiob = device_peripheral.GPIOB.split(&mut rcc.ahb);
    let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
    let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);

    let i2c = I2c::new(
        (scl, sda),
        &mut rcc.apb1,

    let mut lsm = Lsm303agr::new_with_i2c(i2c);

    (lsm.into_mag_continuous().ok().unwrap(), core_peripheral.ITM)

With the help of above code, we are extracting the readings of magnetometer sensor and storing it to a file which is provided by ITM.

In the next article, firstly we’ll understand this code and then the second part of the POC, which is continuously monitoring the sensor’s reading file and sending the latest info to the server.

Thanks for reading!!!

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter:


Written by 

Pawan Singh Bisht is a Software Consultant at Knoldus Software LLP, having a strong experience of more than two years in the technology field. He has been well versed in the core implementation of Rust and Java. He loves to contribute to the community which he attained from the community.