Accelerometer as Puncho-o-meter | The “stm32-Discovery Board Sensor”- Part 1

Reading Time: 4 minutes

Hello everyone, I hope you are fine and learning every day. In today’s blog, we are going to discuss a Sensor of the stm32-Discovery Board. Using this Sensor we will try to make a Punch-o-meter. This is going to be a series of blogs in which each part will take you nearer to the development of Punch-o-meter using one of the Discovery Sensors.

In previous blogs, we have discussed the Magnetometer Sensor of Discovery Board. That series is divided into two parts- PART 1 and PART 2. You can read them also to know about Magnetometer and how it works.

Before going to the Board’s Sensor let’s discuss the Accelerometer Sensor.

What is an Accelerometer and what it does?

An Accelerometer is a device that measures the vibration, or acceleration of motion of a structure. It is also considered as a tool that measures proper acceleration.

Proper acceleration is the acceleration (the rate of change of velocity) of a body in its own instantaneous rest frame; this is different from coordinate acceleration, which is acceleration in a fixed coordinate system. For example, an accelerometer at rest on the surface of the Earth will measure an acceleration due to Earth’s gravity, straight upwards.

Where do we use Accelerometer?

The Applications of the Accelerometer sensor are as follows:

  • Machinery health monitoring.
  • Navigation systems make use of Accelerometer sensors for knowing the direction.
  • For flight stabilization in drones.
  • For inertial navigation systems, highly sensitive Accelerometers are used.
  • To detect and monitor vibrations in rotating machinery.
  • To display images in an upright position on screens of digital cameras.

That’s all you need to know about the Accelerometer Sensor for now. Now let’s begin with the Accelerometer Sensor of the stm32f3-Discovery Board.

Accelerometer Sensor of stm32f3-Discovery Board

We can access this sensor of the Discovery Board using the package and the bus(which will take us to that package where the sensor is present).

Lsm303dlhc(Package)

The Accelerometer is also built inside the LSM303DLHC package. And just like the Magnetometer, it can also be accessed using the I2C bus. It also has the same coordinate system as the Magnetometer.

Just like we accessed the Magnetometer Sensor we will access the Accelerometer and get the readings in a struct.

Access the Accelerometer Sensor

Before going to access the sensor we need to add some of the Dependencies in our Project.

Add some dependencies in Cargo.toml

cortex-m = "0.6.3"
cortex-m-rt = "0.6.3"
panic-itm = "0.4.0"
stm32f3-discovery = "0.6.0"

Then,

First we need to access the Core, Device Peripherals and Clock.

let cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32::Peripherals::take().unwrap();

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

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

Okay after this we need to access the GPIO Port E to access pin 3 to set this pin high to work as an output pin.

let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
let mut nss = gpioe
    .pe3
    .into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
nss.set_high().unwrap();

After accessing this pin we need to move forward to access another port of GPIO. Now, this is going to be Port B using which we are going to access the data and clock line. These two (data, clock) lines are going to help us in accessing the i2c Bus. This i2c bus will help us to reach the package lsm303dlhc.

let mut gpiob = dp.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);

After accessing the two transfer lines we are going to access the i2c.

I2C BUS

I2C Background - Total Phase
let i2c = I2c::new(dp.I2C1, (scl, sda), 400.khz(), clocks, &mut rcc.apb1);

Now the i2c bus is in our control. After this the final task is to access the package which contains our sensor.

Access the lsm303dlhc Package

let lsm303dlhc = Lsm303dlhc::new(i2c).unwrap();

Till here we have to access the lsm303dlhc package. Now we will also access the delay to provide some pause while reading the Sensor’s readings.

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

This is the hidden portion of the main program. Using this we are going to get the readings of the Accelerometer Sensor with some delay.

In the next blog, we are going to get the readings of the “The stm32-Discovery Board Sensor” in a struct format. 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: https://bit.ly/2Vdlld7.

rust-times

Written by 

Nitin is a Software Consultant, with experience of more than 7 months. 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.

Leave a Reply