The video below goes through the details, underneath the video you will find the sample code, a circuit diagram and an affiliate parts list.
The MPU6050 is a versatile unit that can measure its orientation and it’s acceleration in all three dimensions. However did have difficulty in finding a user friendly working library. The one I eventually settled on (after trying around 4 others) was the MPU_6050_light library. This is available from within the Arduino IDE – shown in the video.
Affiliate Parts List
Note: You do not need the OLED display for basic checking, the results can be sent through the serial monitor of you wish. I use it as it makes things easier to see rather than a scrolling list of numbers on the serial monitor. The Arduino can be any, doesn’t need to be a Nano. I’ve listed three options.
MPU6050 : https://amzn.to/31UhdBU
Arduino Nano : https://amzn.to/37Q6E6s
Arduino Uno (Official) : https://amzn.to/31Ur36B
Arduino Uno (clone) : https://amzn.to/35JjW23
OLED (SSD1306) : https://amzn.to/2TyZfjB
Bread Board : https://amzn.to/35Kd0lc
Wires : https://amzn.to/3jMePmV
The Circuit Diagram
Note, as mentioned you do not need the OLED if you do not need it.
Demo Code Shown
To use this demo code with the screen you will need two additional libraries, these are both available through the “manage libraries” feature of the Arduino IDE and they are both from Adafruit. You will need “Adafruit SSD1306” and “Adafruit gfx” library. If you do use the OLED
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
/* Get tilt angles on X and Y, and rotation angle on Z * Angles are given in degrees, displays on SSD1306 OLED * * License: MIT */ #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <MPU6050_light.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire); MPU6050 mpu(Wire); unsigned long timer = 0; void setup() { Serial.begin(115200); // Ensure serial monitor set to this value also if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) // Address 0x3C for most of these displays, if doesn't work try 0x3D { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } display.setTextSize(1); display.setTextColor(SSD1306_WHITE); // Draw white text display.clearDisplay(); Wire.begin(); mpu.begin(); display.println(F("Calculating gyro offset, do not move MPU6050")); display.display(); mpu.calcGyroOffsets(); // This does the calibration display.setTextSize(2); } void loop() { mpu.update(); if((millis()-timer)>10) // print data every 10ms { display.clearDisplay(); // clear screen display.setCursor(0,0); display.print("P : "); display.println(mpu.getAngleX()); display.print("R : "); display.println(mpu.getAngleY()); display.print("Y : "); display.print(mpu.getAngleZ()); display.display(); // display data timer = millis(); } } |