Magnetometer as Compass | The stm32f3-Discovery Board Sensor-“Part-1”

Reading Time: 4 minutes

Hi people, hope you are doing fine. In today’s blog, we are going to build a Compass. Yes, you read it right we are going to build a Compass using a Sensor of the stm32f3 -Discovery Board.

In the previous blog, we read about a package named Lsm303dlhc which basically works for the older version of the Discovery board. We do have a newer version of Discovery Board therefore we are going to use a different package named Lsm303agr which contains the Sensor Magnetometer and Accelerometer. So we need to use this package to build the Compass.

As we are working with the Embedded Systems they make our task fast and reliable. Also, they are much smaller in size compared to traditional computers, which makes them compact and portable, and useful for mass production. Management of Embedded Systems is pretty easy, as elements used in their creation are cheap & long-lasting. Embedded Systems are also cost-effective.

Let’s build it.


A Magnetometer is a sensor on which we are going to work to build a Compass. The compass we are going to build will provide us the Direction of the Magnetic Field. This sensor is not open on the board instead this sensor is packed in Lsm303agr package.

Ultrasensitive magnetic-field sensor could be made from a wobbling compass  needle – Physics World

So to use this sensor we need to access the Lsm303agr package.

How to access the Magnetometer sensor from Lsm303agr Package?

First, we need to add a dependency in our Cargo.toml which is this:

lsm303agr = "0.1"
LSM303AGR click - 3D accelerometer and 3D magnetometer

Now to access the Magnetometer Sensor we have to use the I2c Bus which will take us to the Sensor present inside the package.

We need a clock line and data line of the i2c protocol which will help us in taking us near to the Sensor.

We can access both the lines using the gpio peripheral and its b – port. Both the lines are present at pin pb6 and pb7.

let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);

Now after taking charge of i2c we can access the Lsm303agr package.

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

Now after this we can take Magnetometer Data using this:


So like this we can access (Magnetometer)Sensor of the stm32f3 -Discovery Board and its readings from the Lsm303agr package.

Magnetic fields have both a magnitude, measured in Gauss or Teslas and a direction. The magnetometer on the F3 measures both the magnitude and the direction of an external magnetic field.

Access the LEDs of the Discovery Board

Now our other task is to access the f3 board LEDs so that we can blink them based on the Magnetic Field Direction.

To access the LEDs using gpio peripheral. You can check this git repository for the code.

After this, I assume you are done with the LEDs part.

What’s Next?

We are done with the implementation. We have successfully accessed the LEDs and the package. Now we have to code to get the readings from the Sensor.

Note: We are only working with the horizontal direction and therefore we will not read the vertical directions. So we will only read the x and y axis values of the Magnetometer Sensor. Now let’s get the readings from the sensor in a variable.

let magnetometer_data = lsm303agr.mag_data();
        let x_y_axis = match magnetometer_data {
            Ok(x_y_axis) => x_y_axis,
            Err(error) => {
                panic!("Reading not found {:?}", error)

So till here, we are done with the readings part. We have got the readings in a variable named magnetometer_data.

From magnetometer_data we are taking readings of the x and y-axis in an x_y_axis variable.

What exactly we would achieve?

Okay, before going further let’s clear out what are we trying to achieve here.


So we will work with the 4 directions:

  • Southeast
  • Northeast
  • Southwest
  • Northwest

We will basically work with the 4 – quadrants. Based on the value of the x and y-axis we will change the quadrant and different led will blink based on the different quadrant. Here each quadrant will tell us the Direction of the Magnetic Field.

Based on the condition, this is what we have coded.

The Coordinate Plane
 let direction = match (x_y_axis.x > 0, x_y_axis.y > 0) {
            (true, true) => Direction::Southeast,
            (false, true) => Direction::Northeast,
            (true, false) => Direction::Southwest,
            (false, false) => Direction::Northwest,

So now when we move the board in a different direction it will read different readings for the x and y-axis and based on that reading we are going to match them with the particular direction where the Direction is already created as an Enum.

This is the end of this blog. It’s not over yet let’s not make this blog too lengthy so in the next part of this series, we are definitely going to use our sensor as Compass. In the next, we will learn how to code the LEDs to blink based on quadrant and about itm readings. Thanks for reading this!!

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 

Nitin is a Software Consultant, with experience of more than 1.4 years. He works on Rust Programming Language and Embedded Development using Rust. He is also fond of Java Programming & Artificial Intelligence. Apart from that, his hobbies are Watching Netflix, Reading, Singing & Writing.

2 thoughts on “Magnetometer as Compass | The stm32f3-Discovery Board Sensor-“Part-1”6 min read

Comments are closed.