3

members

Easy Basics: Project 020a MPU 6050 GY 521 (GY-521) module 3 Axis Gyroscope and Accelerometer

of Acoptex.com in UNO

Basics: Project 020a

Project name: MPU 6050 GY 521 (GY-521) module 3 Axis Gyroscope and Accelerometer 

Attachments: MPU6050_short and example raw sketch, MPU6050_DMP6 sketch, Teapot sketch for Arduino and Teapot sketch for Processing and libraries

In this project, you need these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2. GY 521 module with MPU 6050 1pc

3.Arduino IDE ( you can download it from here  ) and Processing IDE ( you can download it from here  )

4.Jumper cables

5. Breadboard 1 pc

General

We will learn how to connect GY-521 module with a MPU 6050 chip 3 Axis Gyroscope and Accelerometer to Arduino board.

Understanding the GY-521 module

IMU sensors are one of the most common types of sensors used today in all kinds of electronic gadgets. IMU sensors help us in getting the attitude of an object, attached to the sensor in three-dimensional space. These values are usually in angles to help us to determine its attitude. They are used in smartphones to detect their orientation or in wearable gadgets like the Fit Bit, which use IMU sensors to track movement.

IMU sensors have a prolific number of applications. It is even considered to be an inexorable component in quadcopters. Some of the sensors: ADXL 345 accelerometer, ITG 3200 gyroscope, Sparkfun 6 DOF IMU sensor board, MPU 6050. The accelerometers and gyroscopes are not as accurate alone as when they are combined. 

IMU sensors usually consist of two or more parts. Listing them by priority, they are the accelerometer, gyroscope, magnetometer, and altimeter. MPU 6050 is reliable and accurate IMU sensor and it is significantly cheaper than the other sensors. The MPU 6050 is a 6 DOF (Degrees of Freedom) or a six-axis IMU sensor, which means that it gives six values as output. Three values from the accelerometer and three from the gyroscope. The MPU 6050 is a sensor based on MEMS (Micro Electro Mechanical Systems) technology. The MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefore it captures the x, y, and z channel at the same time.

- Chip: MPU-6050

- Power supply: 3.5V (But as there is a voltage regulator on the breakout board, you can use 5V directly)

- Communication mode: standard I2C (Inter-Integrated Circuit) protocol for communication.

- Chip built-in 16bit AD converter, 16bit data output

- Gyroscopes range: +/- 250 500 1000 2000 degree/sec

- Acceleration range: +/- 2g, +/- 4g, +/- 8g, +/- 16g

HOW DOES AN ACCELEROMETER WORK

An accelerometer works on the principle of the piezoelectric effect. Here, imagine a cuboidal box with a small ball inside it, like in the picture above. The walls of this box are made with piezoelectric crystals. Whenever you tilt the box, the ball is forced to move in the direction of the inclination, due to gravity. The wall that the ball collides with creates tiny piezoelectric currents. There are three pairs of opposite walls in a cuboid. Each pair corresponds to an axis in 3D space: X, Y, and Z axes. Depending on the current produced from the piezoelectric walls, we can determine the direction of inclination and its magnitude.

HOW DOES A GYROSCOPE WORK

See piezo electric gyro below:

Gyroscopes work on the principle of Coriolis acceleration. Imagine that there is a fork-like structure that is in a constant back and forth motion. It is held in place using piezoelectric crystals. Whenever you try to tilt this arrangement, the crystals experience a force in the direction of inclination. This is caused as a result of the inertia of the moving fork. The crystals thus produce a current in consensus with the piezoelectric effect, and this current is amplified. The values are then refined by the host microcontroller.

We will use MPU 6050 sensor for this project. For more information on the module look here.

You can download datasheet here. Register map can be found here.

Signals and contacts of GY-521 module

The MPU6050 communicates with the Arduino board through the I2C protocol via the serial clock (SCL) and serial data (SDA). 

VCC - The MPU6050 chip needs 3.3V but a voltage regulator on the GY-521 module allows you to give it up to 5V

GND - Power supply ground

SCL - I2C serial clock (SCL)

SDA - I2C serial data (SDA)

XDA - I2C master serial data, for connecting to external sensors

XCL - I2C Master serial clock, for connecting to external sensors

AD0 - I2C Slave Address LSB (AD0)

INT - Interrupt digital output (totem pole or open-drain)

The sketch we will be running here, also takes advantage of the Arduino’s interrupt pin.

Wiring

Sensor  Arduino

VCC -> 5V

GND -> GND

SCL -> SCL or A5

SDA -> SDA or A4

INT -> D2 (used for interrupt)

(the other pins do not need to be connected)

The Raw Codes

The accelerometer and gyro values are called the "raw" values. This is just as with other accelerometer and gyro sensors. A more sophisticated application is using the DMP to retrieve specific computed values from the sensor.

The Short example sketch is a very short sketch that shows all the raw values  (accelerometer, gyro and temperature). It should work on Arduino Uno, Nano, Leonardo, and also Due. Click "Get code" and copy it into a sketch. Change I2C address in this sketch to your address found with I2C Scanner sketch : const int MPU_addr=0x68;  // I2C address of the MPU-6050


Note: if the output is random characters, make sure your Serial monitor is set to 9600 bps in the Arduino IDE program.

The Example sketch (base code) is also just showing the raw values, but it is an attempt to be a complete base for more functions. The sketch is meant to be a base for interfacing the MPU-6050. The sketch uses the Arduino functions as much as possible. For now, it is just a simple and basic sketch to get the MPU-6050 working. The acceleration and gyro values of the sketch are raw values, which are not yet compensated for offset. The very first acceleration and gyro values after power up are sometimes not valid. The sketch is about 7kbyte, and will fit in a ATmega8. The I2C-address depends on the AD0 pin of the sensor. If it is connected to ground, the address is 0x68. If it is connected to VLOGIC (+3.3V) it is 0x69. There are a few sensor boards with the MPU-6050 sensor already soldered on it. Some of those boards have a pull-down resistor at AD0 (address = 0x68), others have a pull-up resistor (address = 0x69). Click "Get code" and copy it into a sketch. Search in the sketch for "MPU6050_I2C_ADDRESS" and set that to your own I2C address found with I2C Scanner sketch : #define MPU6050_I2C_ADDRESS 0x68


Note: if the output is random characters, make sure your Serial monitor is set to 9600 bps in the Arduino IDE program.

See more info about MPU6050 here.

Step by Step instruction

  1. Use I2C Scanner sketch to verify that the Arduino finds your GY-521 module. I2C scanner scans the I2C-bus for devices. If a device is found, it is reported to the Arduino serial monitor. This sketch is the first step to get the I2C communication working. The sketch shows the 7-bit addresses of the found devices as hexadecimal values. That value can be used for the "Wire.begin" function which uses the 7-bit address. Some datasheets use the 8-bit address and some example sketches use decimal addresses.
  2. Open a new sketch in Arduino IDE and copy this sketch into it. 
  3. Plug your Adruino Uno board into your PC and select the correct board and com port
    Open up serial monitor and set your baud to 9600 baud.
    Verify and upload the the sketch to your Adruino Uno.
    Plug your Adruino Uno board into your PC and select the correct board and com port
  4. Open up serial monitor and set your baud to 9600 baud.
  5. Verify and upload the the sketch to your Adruino Uno.
  6. Every found device on the I2C-bus is reported.You can change the wires, and plug-in I2C devices while the sketch is running.
  7. If the device is found write down the I2C address - in our case it is 0x68. The I2C address depends on the AD0 pin of the sensor. If it is connected to ground, the address is 0x68. If it is connected to VLOGIC (+3.3V) it is 0x69. There are a few sensor boards with the MPU-6050 sensor already soldered on it. Some of those boards have a pull-down resistor at AD0 (address = 0x68), others have a pull-up resistor (address = 0x69).If it doesn’t find the IMU at I2C address 0x68 or 0x69, there might be an issue with your wiring or the module. Please check the wiring again.
  8. Open MPU6050_DMP6 sketch in Arduino IDE. 
  9. Plug your Adruino Uno board into your PC and select the correct board and com port
    Open up serial monitor and set your baud to 9600 baud.
    Verify and upload the the sketch to your Adruino Uno.
    Plug your Adruino Uno board into your PC and select the correct board and com port
  10. Open up serial monitor and set your baud to 115200 baud.
  11. Verify and upload the the sketch to your Adruino Uno.
  12. Check if you see “Initializing I2C devices…” on the serial monitor. If you don’t, just press the reset button.  
  13. You will see a line “Send any character to begin DMP programming and demo.”, so you just need to type in any character on the serial monitor and send it and you should start seeing the yaw, pitch, and roll values coming in from the MPU 6050. DMP stands for Digital Motion Processing. The MPU 6050 has a built-in motion processor. It processes the values from the accelerometer and gyroscope to give us accurate 3D values. You will need to wait about 10 secs before you get accurate values from the Arduino MPU 6050. 
  14. If you run MPU6050_DMP6 sketch on the Arduino and Serial monitor shows the error message: "MPU6050 connection failed" - you need to solder the pins to the board to solve the connection issue.
  15. Set the code to run the teapot demo to show the 3D simulation. We have modified and saved this sketch for you or you can do the changes yourself : close the serial window, then find and comment the line in the Arduino MPU6050_DMP6 sketch code which says: #define OUTPUT_READABLE_YAWPITCHROLL  by  //#define OUTPUT_READABLE_YAWPITCHROLL and uncomment the line which says: //#define OUTPUT_TEAPOT  by  #define OUTPUT_TEAPOT, select "save as" and choose where you want to save the modified code. 
  16. Verify and upload the the sketch to your Adruino Uno. Don't open the serial monitor window this time. Keep Arduino IDE open we will come back to it later.
  17. Download and install the Processing IDE software, you will need it to view the 3D representation of the data from the MPU 6050. Processing is similar to Arduino, except for a couple of functions and is mainly used for visualizing data and rendering it in 2D/3D models.
  18. Download a Toxiclibs library file - toxiclibs-complete-0020.zip, which is necessary for our Arduino MPU 6050 processing example. Open Processing IDE, go to File->Preferences, notice the value of the field labeled "Sketchbook location:", open this path in a file browser, inside that folder should be another folder called "libraries". Make a folder toxiclibs-complete-0020 inside the libraries folder and extract the files from toxiclibs-complete-0020.zip in it. Restart Processing IDE. For example it should be: /home/myusername/sketchbook/libraries/toxiclibs-complete-0020. 
  19. Open the Processing example for the MPU 6050: Open Processing, then File –> Open, then navigate to the folder where you installed the MPU6050 library for Arduino. You can find the processing example in: MPU6050 –> Examples –> MPU6050_DMP6 –> Processing –> MPUTeapot -> MPUTeapot.pde.
  20. You need to check the serial port which is defined in this code - MPUTeapot.pde. By default the line that defines it for LinuxMmac users is: String portName = “/dev/ttyUSB1”; and for Windows users is: String portName = "COM4";. Uncomment necessary line and comment the line which is not in use, depending on your operating system (Linux or Windows). For Windows users, you need to comment the line that says: String portName = “/dev/ttyUSB1”;  by  //String portName = “/dev/ttyUSB1”; and uncomment the line that says: //String portName = “COM4”;  by  String portName = “COM4”;. Replace “COM4” with the COM port where your Arduino is connected (check this by going into Arduino IDE -> Tools –> Port -> Serial Port).
  21. Run the processing sketch code (MPUTeapot.pde) by pressing the button with the “play” symbol.
  22. You will see a small plane like object. Wait for about 10 seconds for the MPU 6050 values to stabilize. 
  23. This is the 3D model of your MPU 6050, which moves in accordance with the sensor.
  24. If you want to change the plane to another simple 3D object do change the lines 112-139 of code (MPUTeapot.pde) using the 3D programmation for Processing (here is the tutorial which you can use: https://processing.org/tutorials/p3d/ ). You can do simple 3D models likes boxes, cylinders, triangles and quadrilaterals with that.  

Summary

We have learnt how to connect MPU 6050 GY 521 sensor to Arduino board. We also used 3D modeling software to illustrate the operation of the sensor.

Libraries:

  • See attachments on the begining of this project description
  • MPU6050 and I2C dev libraries used for this project. Download, unzip and add to libraries in your PC, for example C:\Users\toshiba\Documents\Arduino\libraries or in C:\Program Files (x86)\Arduino\libraries. This link you can find in Preferences of Adruino IDE program which installed in your PC.

Sketch:

  • See attachments on the begining of this project description


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019

« Go back to category
Is this project fake? Report it!   
Recommend to a friend
Published at 29-08-2017
Viewed: 2916 times