Team4 gyro and acceleration sensor

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search

MPU-6050 gyroscope and acceleration sensor[edit]

The MPU-6050 is a MEMS-based sensorchip for motion tracking built into the GY-521 breakoutboard
Thanks to its 3-axis gyroscope and 3-axis acceleration sensors, it can recognize 6 DOF (degrees of freedom) simultaneously.
That means it can measure its orientation/rotation and acceleration in a 3D space (6 DOF). In addition to that, the environment temperature can be measured as well.
GY521 - MPU 6050

Core Features[edit]

   * Chip: MPU-6050 (InvenSense)
   * 16-Bit AD-Converter
   * Acceleration sensor range: ±2, ±4, ±8, ±16g
   * Gyro range: ±250°, 500°, 1000°, 2000°/s
   * Voltage range: 3,3V - 5V 

Technical Specifications[edit]

The module has its own I²C bus, which allows for an easy connection with microcontrollers such as the ESP32. Ideally, 5V should be used to guarantee a sufficient power supply. Arrows on the board indicate the x and y axis for convenient usage.
The built in Digital motion Prozessor (DMP) processes complex algorithms directly on the board, for example to procces the raw sensor data to stable position data.

Pinout[edit]

Pinout

VCC
First one is the VCC Pin, which is used to power the sensor, 3V - 5V can be applied
GND
Ground Pin, which should be connected to one of the microcontrollers ground pins
SCL
Serial Clock (I²C). Connected to an SCL-Pin of the microcontroller, a clock pulse will be emitted which is used in I²C communication
The clock source is provided by the microcontroller
SDA
Serial Data (I²C). SDA is used to transfer data to a microcontroller, connected to the microcontrollers SDA-Pin
XDA
Auxiliary Serial Data. Can be used to connect external I²C modules, like a magnetometer
The use of it is totally optional.(Not covered in this wikipage)
XCL
Auxiliary Serial Clock. Is used for communication with external I²C modules
The use of it is totally optional.(Not covered in this wikipage)
AD0
Address Select Pin. Its a slave address. If you connect more than one MPU6050 with your microcontroller,
the I²C bus of the microcontroller can distinguish the modules thanks to the unique address
low -> 0x68; high 0x69
INT
Interrupt. Which is an interrupt digital output pin
Is used to give the microcontroller indication that data is available to read. Use is optional

Advantages / Disadvantages[edit]

Advantages[edit]

  • Easy to use
  • Orientation indicators
  • Small and light
  • DMP

Disadvantages[edit]

  • Some GY521 use 2,2kΩ, which can be a problem if other sonsormodules are used too. A additional extern pullup resistor can help in these cases
  • Some GY521 have a wrong or damaged capacitor which can lead to noise in the sensordata

Get it to Work[edit]

This Step-by-Step Guide shows how to connect the NodeMCU ESP32 with the GY521 - MPU6050 in the Arduino IDE.

IDE Setup[edit]

  1. Install latest Arduino IDE
  2. Update/install the CP210X USB-UART driver
  3. Setup ESP32 in Arduino IDE
    1. Open Arduino IDE
    2. File>Preferences enter in "Additional Boards Manager URLs" the following URL - "https://dl.espressif.com/dl/package_esp32_index.json"
    3. Click OK
    4. Tools>Board>Boards Manager...
    5. Install "esp32" by espressif Systems
    6. Click Tools>Board>ESP32 DEV Module
      (Your choice depends on the Board you are using. For NodeMCU ESP32 choose "ESP32 DEV Module")
    7. Tools>Manage Libraries search for"Adafruit mpu6050"
    8. Install the Library and all its dependencies

Wireing[edit]

Wireing Schematic Connect like following (GY521(MPU6050) -> ESP32):

  • VCC -> 3V3
  • GND -> GND
  • SCL -> D22 (GPIO22 I²C SCL)
  • SDA -> D21 (GPIO21 I²C SDA)

ESP32 Setup[edit]

  1. Connect your ESP32 via USB to your machine
  2. In the IDE make sure you selected the right Port.
    Tools>Port>"Port of the connected ESP32"
  3. In Arduino IDE File>Examples>Adafruit MPU6050>plotter
  4. In the newly opened window with the Plotter-Code, click "Upload" and hold down the "Boot" button on the ESP32
  5. when the upload is completed, press the reset button "EN" on your ESP32

Data Readings[edit]

  1. Tools>Serial Monitor -> this will show you the Plotter-Code output.
  2. Tools>Serial Plotter -> this will show you the outputdata plotted as a Graph.

Other IDE's[edit]

You can connect the GY251(MPU6050) in many ways with ESP32.
So here is a more generic guide with mycrophython.

  1. Create a new file on your machine
  2. Copy the following code and paste it in your new file -> mpu6050.py(Mycropython I²C API for ESP32 and ESP8266)
  3. Save the file as "mpu6050.py"
  4. Now upload the file to your ESP32
  5. Create a new file named main.py
  6. Copy the Code Example below in your "main.py" file
  7. Upload "main.py" to your ESP32
  8. Read the output in your preferred IDE's shell

Code Example[edit]

 1 from machine import I2C
 2 from machine import Pin
 3 from machine import sleep
 4 import mpu6050
 5 
 6 i2c = I2C(scl=Pin(22), sda=Pin(21))     #initializing the I2C method for ESP32
 7 #i2c = I2C(scl=Pin(5), sda=Pin(4))       #initializing the I2C method for ESP8266
 8 mpu= mpu6050.accel(i2c)
 9 
10 while True:
11  mpu.get_values()
12  print(mpu.get_values())
13  sleep(500)

Video[edit]

Notable links[edit]

MPU6050 Data Sheet