Gyroscope sensor of the stm32 board || Part 2

Reading Time: 4 minutes

Hi everyone, in today’s blog we are going to work further with the Gyroscope sensor of the stm32 discovery board. We are going to access the l3gd20 package and return the important data to the main method.

In the previous part of this series, we learned about the Gyroscope Sensor, how it works and where we use this sensor. So before reading it further, I suggest you please go through that first, you can read that part from here.

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.

So in the previous part of the series we have successfully accessed the core and the device peripherals of the F3 Board. Now we are moving further to access the pins of Port A so that we can get the measurements of the Gyroscope Sensor of the stm32 board.

Access the Port A

We need to access the Port A of General-purpose input-output and some of its pins. We are going to access Port A pin 5, pin 6, and pin 7. So using these pins we can easily access the spi peripheral which will lead us to the package l3gd20 and with the help of this l3gd20 package we can extract the Gyroscope sensor readings.

    let pin5 = gpioa.pa5.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
    let pin6 = gpioa.pa6.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
    let pin7 = gpioa.pa7.into_af5(&mut gpioa.moder, &mut gpioa.afrl);

Access the SPI Peripheral

After accessing 3 pins of the Gpio A port we are going to access the SPI peripheral but before that let’s discuss a little about SPI peripheral.

What is an SPI Peripheral?

PPT - Serial Peripheral Interface (SPI) Bus PowerPoint Presentation, free  download - ID:4356030

The serial peripheral interface is a synchronous serial communication interface specification used for short-distance communication, primarily in embedded systems. In simple words, this is a synchronous serial type communication method that we used as a way of communication generally in embedded systems. This type of communication was developed by Motorola in the mid-1980s.

let spi = Spi::spi1(
    (pin5, pin6, pin7),
    &mut rcc.apb2,

We here access the SPI peripheral using the device spi peripheral, the pins(pin5, pin6, and pin7). We also provide the mode to the l3gd20. So like this, we successfully access the spi peripheral using which we are going to perform the short-distance communication.

Access the Package L3gd20

Now everything is done and we can access the main package that is l3gd20. Why we are calling this l3gd20 the main package?

Pololu - L3GD20 3-Axis Gyro Carrier with Voltage Regulator

This package is going to help us in getting the readings of the Gyroscope Sensor of the stm32f3 board. Okay so let’s access the package then.

let mut l3 = L3gd20::new(spi, led).unwrap();

Everything is inside this l3 variable now and we need to access the readings from this l3 by unwrapping it.

let gyro_readings = l3.all().unwrap();

Okay so we are using l3.all() here and this is because we want to get the readings of all the 3 axes(x, y, z). Therefore using this .all() we get the measurements from all three axes of the Gyroscope Sensor.


Arduino Delay - JavaTpoint

We want to pause the process of getting the readings continuously for a few seconds and to do that we use this delay part in the code.

let delay = Delay::new(core_peripherals.SYST, clocks);

Using this delay we can pause the process for few seconds.

Return the important data

At last, we are going to return the most important data to provide it to the main method.

This program is going to return a tuple of l3g-package, delay, and itm.

  • l3gd20 -> a package to access the gyroscope sensor to get the readings.
  • Delay -> a time delay to pause the running program for a few seconds.
  • ITM -> a tool to print the sensor data on the itm console.
(delay, core_peripherals.ITM, gyro_readings)

So this was all for this part of the Gyro Series and in the next part we are surely going to extract the readings of the gyro sensor and I am also going to provide the Github link containing the running template of the same code.

Thanks for reading…see you in next.

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.

1 thought on “Gyroscope sensor of the stm32 board || Part 25 min read

Comments are closed.