Difference between revisions of "MPU 6050"

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
The module is connected via the I2C bus.
 
The module is connected via the I2C bus.
  
[[File:Mpu-hi1.JPG]]
+
[[File:Mpu-hi1.JPG|x300px]]
  
 
more details for advanced users:
 
more details for advanced users:

Revision as of 11:48, 31 August 2020

Description

The MPU-6050 is a sensor that combines a 3-axis gyroscope and 3-axis accelerometer. Here it is used as the GY-521 module that can be directly connected to the ESP32/ES8266. The module is connected via the I2C bus.

Mpu-hi1.JPG

more details for advanced users:

How to connect it electrically

The MPU 6050 is connected to the I2C bus. This is the same as where the display is connected.

  • Pin15 is SCL (OLED_SCL)
  • Pin4 is SDA (OLED_SDA)

Example connection to the ESP32 This shows all values of the MPU6050 on the OLED Display. It assumes the following connections:

  • GND on GY521/MPU6050 to GND on ESP32
  • VCC on GY521/MPU6050 to 3.3V on ESP32
  • SCL on GY521/MPU6050 to OLED_SCL/Pin15/SCL on ESP32
  • SDA on GY521/MPU6050 to OLED_SDA/Pin4/SDA on ESP32

Mpu-elec.JPG

Required Module and Files

There are other libraries available, e.g. https://github.com/adamjezek98/MPU6050-ESP8266-MicroPython


How to control it in MicroPython

 1 from machine import I2C, Pin 
 2 from imu import MPU6050
 3 
 4 # Pins according the schematic https://heltec.org/project/wifi-kit-32/
 5 i2c = I2C(-1, scl=Pin(15), sda=Pin(4))
 6 
 7 imu = MPU6050(i2c)
 8 
 9 # print all values
10 print(imu.accel.xyz)
11 print(imu.gyro.xyz)
12 print(imu.temperature)
13 
14 #print a single value, e.g. x value of acceleration
15 print(imu.accel.x)



Related Tutorial Videos


Program: Showing all Values on the Display

This shows all values of the MPU6050 on the OLED Display. It assumes the following connections:

  • GND on GY521/MPU6050 to GND on ESP32
  • VCC on GY521/MPU6050 to 3.3V on ESP32
  • SCL on GY521/MPU6050 to OLED_SCL/Pin15/SCL on ESP32
  • SDA on GY521/MPU6050 to OLED_SDA/Pin4/SDA on ESP32

Mpu-highres.JPG

 1 from machine import I2C, Pin 
 2 import ssd1306
 3 from imu import MPU6050
 4 from time import sleep
 5 
 6 # ESP32 reset pin for display must be 1 - this is pin16 
 7 # should be done in ssd1306.py - if not uncommend the next 2 lines
 8 #pin16 = Pin(16, Pin.OUT)
 9 #pin16.value(1)
10 
11 
12 # Pins according the schematic https://heltec.org/project/wifi-kit-32/
13 i2c = I2C(-1, scl=Pin(15), sda=Pin(4))
14 
15 #display size
16 oled_width = 128
17 oled_height = 64
18 oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
19 
20 #link IMU to the i2C bus
21 imu = MPU6050(i2c)
22 
23 while True:
24   # read in analog value in v
25   x = imu.accel.x
26   y = imu.accel.y
27   z = imu.accel.z
28   xg = imu.gyro.x
29   yg = imu.gyro.y
30   zg = imu.gyro.z
31   t = imu.temperature
32   
33   # print to serial line
34   print("x:", x, "y: ", y, "z:", z)
35   # empty display
36   oled.fill(0)
37   oled.show()
38   # write v converted to a string onto the display at (0,0)
39   oled.text("x:"+str(x), 0, 0)
40   oled.text("y:"+str(y), 0, 9)
41   oled.text("z:"+str(z), 0, 18)
42   oled.text("xg:"+str(xg), 0, 27)
43   oled.text("yg:"+str(yg), 0, 36)
44   oled.text("zg:"+str(zg), 0, 45)
45   oled.text("t:"+str(t), 0, 54)
46   oled.show()
47   sleep(0.3)