Raspberry Pi Accelerometer and Gyroscope: Detailed GuideMandip
You know that the gyroscope and accelerometer are the things that help to control and measure the movements of the components in a device. They help to estimate the angles and to take readings. Today, I will provide a detailed guide to Raspberry Pi accelerometer and gyroscope.
Let’s have a look at what these both things do:
It calculates the rotation of the device over the time & determines the current angle. If you want a precise tracking of sharp & sudden movements of your Pi robot, then gyroscope is best for it.
It can calculate the angle at the time given by the user. It can take both dynamic and static readings. So, the tracking is eliminated, unlike the gyroscope. An accelerometer is noisy compared to gyro though. If you want a long-term angle measurement on your Raspberry Pi, then the accelerometer is the best fit.
Now, before providing the guide on how to interface accelerometer and gyroscope on your Raspberry Pi, I will explain the best Raspberry Pi accelerometer and gyroscope components.
1) GY-521 MPU-6050 Raspberry Pi 3 Axis Accelerometer 3 Axis Gyroscope
This chip is one of the best Raspberry Pi 3 accelerometer and gyroscope modules available today. The MPU6050 from Generic is compatible across various Raspberry Pi models as well as Arduino boards.
Specifications and features of GY-521 MPU6050 Raspberry Pi 3-Axis Accelerometer 3-Axis Gyroscope module are:
- The gyroscope has a range of 250 to 2000 ° / s (±).
- It is compatible with the standard ICC communication protocol.
- The accelerometer has a range of 2g to 16g (±).
- It has a built in AD converter of 16bit.
- It operates on a 3V-5V power supply with a built-in regulator for low dropout.
2) BerryGPS-IMUv2 Raspberry Pi Accelerometer Gyroscope Magnetometer & Barometric/Altitude Sensor
It doesn’t matter which Raspberry Pi board, this BerryGPS-IMUv2 from Ozzmaker will be compatible with it. This multiple-sensor module has inbuilt gyroscope, accelerometer, barometer, and magnetometer.
Features and specifications of BerryGPS-IMUv2 Gyroscope and Accelerometer for Raspberry Pi are:
- It comes with an internal antenna.
- You can put the GPS module in the standby mode if you want with the help of the included pin.
- Once you successfully fix the connection, the inbuilt LED will flash after every second to indicate.
- Its GPS module can track over 22 satellites.
- The port to connect the external antenna will help to access the signals when there are obstacles.
- The data will be stored for almost 4 hours in the battery backup RAM during power cycles.
How to Interface Accelerometer & Gyroscope on your Raspberry Pi?
Now, I will explain how to interface the gyroscope and accelerometer modules on the Pi board. For this tutorial, I will use the MPU6050 sensor module.
Things you will need:
- Raspberry Pi board
- Accelerometer & Gyroscope (I used MPU-6050)
- Jumper wires
- Pi compatible power supply
Now, follow the steps below to establish the connection:
Step-1: Raspbian Set up and I2C Communication
First, you need to enable the I2C communication on your Raspberry Pi board.
- Open the Raspberry Pi command prompt
- Then type the command below to open the configuration window
- From the options on the screen, choose the ‘Interfacing Options’ on number 5
- In the next window, click on the ‘I2C’ option
- When you asked whether you like to enable the I2C interface, click on Yes button
- Now, reboot your Raspberry Pi device
Step-2: Install drivers and library
After you restart the Raspberry Pi system, install the needed drivers and libraries for the interface.
- Open the command prompt and type the below command
sudo vi /etc/modules
- Now, add the below lines of text at the end of the file
- Reboot your Raspberry Pi
- After that, open the blacklist file and add a hash sign (#) at the start of the below lines (if they are not present in the file, then do nothing)
- Save and exit
- Now, again open the command prompt and install the GPIO & Python library by entering the commands below
sudo apt-get install build-essential python-dev python-pip
sudo pip installs RPi.GPIO
- Install Raspberry Pi smbus library by entering the below code
sudo apt-get install python-smbus
Step-3: Connect the sensor module
Connect the accelerometer and gyroscope sensors (here MPU6050) with the Raspberry Pi board. Pin 1, 3, 5, and 6 are essential to which the VCC, SDA, SCL, and GND should be connected respectively.
- Install the I2C tools and test the sensors after you connect them successfully with the board. Enter the below commands in command prompt:
sudo apt-get install i2c-tools
sudo i2cdetect –y 1
- In the above code, I have written 1 because I have a Revision 2 board. If you are using Revision 1 board, then write 0 instead of 1.
- According to Pi and the sensor address detected by your Raspberry Pi enter the below command to retrieve the value in your desired register.
sudo i2cget –y 1 (identified register address) (desired register address)
- Now, install the library for the sensor module:
sudo pip install mpu6050
- Save and exit.
Now, as per your knowledge and requirement, complete the Python code. If you want, you can take help of some online codes that are available to complete your project.
Check the source codes here: Interfacing MPU6050 with Raspberry Pi in C and Python
The sensor module for gyroscope & accelerometer for Raspberry Pi like the MPU6050 comes at an affordable price & delivers dominant performance. Here, I have tried to cover the necessary things for interfacing the Raspberry Pi accelerometer and gyroscope. You can customize your coding as per your requirements and understandings.
Share your thoughts on this guide here. I will be happy to hear from you.