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

Reading Time: 4 minutes

Welcome everyone, hope all are doing great and learning each day. So today we are going to take this series further to the next part. In today’s blog, we are going to connect our LEDs with the Direction enum so that we can set the LED on for the particular Direction which will be based on the Magnetic Field readings. We will be working on the same stm32f3-Discovery Board Sensor.

Note: If you have not read the previous part then I suggest you go through that first.

The Magnetometer Sensor will get the readings of the Magnetic Field from all three axis.

Let’s work on the Led and Direction of “The stm32f3-Discovery Board Sensor”

I hope from the previous part you have gained access to stm32f3 Board LEDs. Now it’s time to use those LEDs according to our Directional Values.


Now from the initialization, we are taking Leds of the Board in variable named LEDs.

let (leds, mut lsm303agr, mut delay, mut itm) = init();
stm32f3-Discovery Board Sensor

After this, we want to iterate through our LEDs so that we have to convert the coming LEDs into an array form.

 let mut stm_leds = leds.into_array();

Using this statement we will get the Led arrays using which we will be able to iterate through the LEDs.

Now what?

By default, we will keep our LEDs off so that we can only turn “on” the particular Led we want which will be based on the Magnetic Field Direction values. The values we get from the sensor.

The decision of which led to turn “on” is made based on the value of the x and y-axis. Based on the x, y-axis we can verify the quadrant and then can blink the Led accordingly. Let’s turn “off” the LEDs for now.

stm_leds.iter_mut().for_each(|leds| match leds.off() {
            Ok(led) => led,
            Err(..) => {}
        });

Connect the Led with the Direction(Enum) values

You maybe remember a variable named direction was getting the Direction(Enum) values based on quadrant values. So we will be using the same variable output.

Let’s iterate and find out which led we need to turn on.

stm_leds[direction as usize].on().ok();
Y Axis Definition (Illustrated Mathematics Dictionary)

With this, we will be turning “on” the Led to the particular Direction.

ITM show the Readings

At last, we will be printing the readings on the itm console. In the background, all this data will be continuously saved in an itm.txt file from which data will be fetched and parsed using the itmdump.

We need itmdump to parse the data present in the itm.txt file as the data works with frames where each frame has a header and a variable-length payload. OpenOCD will receive these frames and write them directly to a file without parsing them.

So, if the microcontroller sends the string “Hello, world!” using the iprintln macro, OpenOCD’s output file won’t exactly contain that string.

iprintln!(&mut itm.stim[0], "x = {} y = {}", x_y_axis.x, x_y_axis.y);
Magnetic Field Lines - Definition, Properties, How to Draw - Teachoo

We will get readings from the sensors after some delay, for that we will use the delay.

delay.delay_ms(1_000_u16);

That’s all we are done with the making of Compass using a Magnetometer Sensor.

The Code

This is the main code that will relate to what we have done.

#![no_main]
#![no_std]

use stm32_compass::config::initialization::{
    entry, init, iprintln, switch_hal::OutputSwitch, Direction,
};
use stm32f3_discovery::stm32f3xx_hal::prelude::*;

/// This program is going to print the (x,y) axis values on itm terminal and will blink the led
/// based on the (x,y)->Magnetic Field Direction.
/// This program will use the discovery board as a Compass.
///
/// #Return
/// Program is using [no_std] & [no_main] therefore it will neither end nor return anything.
#[entry]
fn main() -> ! {
    let (leds, mut lsm303agr, mut delay, mut itm) = init();
    let mut stm_leds = leds.into_array();

    loop {
        // Reading the magnetometer register's (x,y) value using mag_data().
        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)
            }
        };
        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,
        };

        stm_leds.iter_mut().for_each(|leds| match leds.off() {
            Ok(led) => led,
            Err(..) => {}
        });
        stm_leds[direction as usize].on().ok();
        iprintln!(&mut itm.stim[0], "x = {} y = {}", x_y_axis.x, x_y_axis.y);

        delay.delay_ms(1_000_u16);
    }
}

This was the final part of this series I hope you liked it. This is the link to the GitHub repo of this whole project.

I will see you in next till then take care and keep learning. We will discuss about some more stm32f3-Discovery Board Sensor.

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: https://bit.ly/2Vdlld7.

rust-times

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.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading