Interface an inertial measurement unit (IMU) with raspberry pi

First things first, if you have not checked out my previous blog about how to interface an ultrasonic sensor and a camera with raspberry pi, then have a quick read this blog.

“Knowing yourself is the beginning of all wisdom.” — Aristotle

This robot can sense it’s environment. But it has very less information about itself. I get a feeling, it’s a good time to make this robot wiser, after all it is getting older by the day ;p.

What does it mean, “knowing oneself” for a robot, I hear someone asking. The robot can know about it’s presence. Here’s a caveat, It can not magically know where it is in the 3D space directly, however, what it can know/track is it’s movement. This, in robotics, is called (jargon alert ⚠️ ) State Estimation.

The sensor or rather a bunch of sensors used for this purpose are together called an Inertial Measurement Unit (IMU).

There can be a number of sensors for different IMUs. The one that I am using (GY-521 module (MPU-6050)) has:

  1. 3 axis accelerometer (measures acceleration along 3 orthogonal axes)
  2. 3-axis gyroscope (measures angular velocity along 3 separate axes)
  3. Temperature sensor (you know what it does ;p)

From a hardware point of view, how to connect an IMU with raspberry pi?
This IMU has 8 pins, from VCC to INT. We only use the first 4 pins to connect it to the PI. These are:

  1. VCC — Voltage Common Collector (power input).
  2. GND — Ground.
  3. SCL — Serial Clock Line for IIC/I²C communication.
  4. SDA — Serial Data Line for IIC/I²C communication.

Following are the connections with the GPIO pins of PI:

# — Sensor — — — — — — ——PI (GPIO) — — — or — — — PI (Pin Num)

  1. VCC — — — — — — — — — 5V pin — — — -which is— — — — Pin 4
  2. GND — — — — — — — — — GND — — — — which is— — —- Pin 6
  3. SCL — — — — — — — — — GPIO 3- — — — which is — — — — Pin 5
  4. SDA — — — — — — — — — GPIO 2- — — — which is — — — Pin 3

Once the connections are made and raspberry pi is powered on, the IMU light switches on.

Great. The IMU has been interfaced with the raspeberry pi. Before running a program. I²C communication has to be enabled in raspberry pi.

Follow the steps below to enable the I²C communication in pi:

  1. Run the following command to start the raspberry configuration tool.

2. Select no. 5 which says Interfacing options.

3. Select and enable the I2C (P5).

Now, let’s test this sensor by writing a small basic program in python.

In order to use code from a dedicated standard library for MPU-6050 IMU, install the library from here and import it.

Code is available at the repository mentioned below.

The output is as follows:

Hmm. I know what you are thinking. Z- component of acceleration should be somewhere around 9.8 m/s² (acc. due to gravity). But it is not. Cheaper IMUs have a big disadvantage, they produce noisy measurements and are sensitive to temperature based fluctuations.

As a result, I will try to make the sensor model keeping this problem in mind and can further use a filtering technique (Kalman Filter) to make this data more reliable. But this is for future blogs. Let’s not get ahead of ourselves.

In this blog, we learnt how to connect the IMU with raspberry pi and how to extract three types of data from it. This robot has enough sensors now for me to start working on the software module.

Thanks for reading this long blog. We have made a good progress and if you like this blog, please clap.