2025-Mar-19, Wednesday

The MPU-6050 is an accelerometer and gyroscope (with added thermometer), that contains a Digital Motion Processor (DMP) to handle a lot of the heavy maths lifting of turning the raw sensor values into useful motion values before they reach the microcontroller. It seems (caveat; I'm new to this area) that the original code is only available as a binary, or as some demo code. But some very determined people have pulled a library together.

/* Source is from the InvenSense MotionApps v2 demo code. Original source is
 * unavailable, unless you happen to be amazing as decompiling binary by
 * hand (in which case, please contact me, and I'm totally serious).
 *
 * Also, I'd like to offer many, many thanks to Noah Zerkin for all of the
 * DMP reverse-engineering he did to help make this bit of wizardry
 * possible.
 */


Well, actually, it seems quite a lot of people have pulled libraries together, to different levels of functionality, with very similar names. But the one most people seem to use is this one. Which includes the MotionApps files. This is important, not just for the right function calls, but because you need other libraries from this collection as well, particularly the I2Cdev one. These libraries, although for Arduino, are not findable inside the Arduino IDE's Library Manager. You need to side-load them, old school style. Which means downloading and extracting the .zip file. Then copying the specific libraries (being the folders that contain an 'Examples' folder as a first level child) for MPU6050 and I2Cdev into your Arduino > libraries folder. And restarting the IDE.

(I've also, after the fact, found what seems to be a copy of the MPU6050 library in an ElectronicCats repo, that is in the Library Manager, and seems to have rolled the I2Cdev code into itself. But the oldest change here seems to be 6 years ago. Whereas the i2cdevlib seems to be about 13 years ago. It does seem to have more recent updates than i2cdevlib though. So the choice is yours. I'm telling you, it's a nightmare finding what's what, especially when the code linking to it won't even compile with it.)

To help us get a feel for how the boards movements are interpreted, there's a nice demo we can try out. You can find the MPUTeapot Processing project inside the MPU6050 Arduino library; MPU6050 > examples > MPU6050_DMP6 > Processing > MPUTeapot.

First step is to install Processing. I'm using P4. Copy that MPUTeapot.pde file into a new sketch (or the whole folder to wherever you save your sketches). This sketch requires another stack of libraries, the Toxi/c libraries, which we need to side-load again. Download the newest release, extract the .zip file, and copy the libraries (being the folders that contain an 'examples' folder as a first level child) into your Processing > Libraries folder. Restart Processing.

Illustration of Arduino Mega board wired up to MPU6050 board as described below.

MPU6050 Circuit
Made with Fritzing



Wire up your MPU6050 to your Arduino (I'm using the Mega 2560). You need to connect the Interrupt pin to Digital pin 02 (on most Arduino boards). SDA -> SDA. SCL -> SCL. VCC -> 5V. GND -> GND.

Open the MPU6050 > MPU6050_DMP6 example, and load it onto your Arduino. By default it runs in readable yaw-pitch-roll mode. Open the Serial Monitor at 115200 baud. After finding and initialising your MPU6050 it will await char input. Send any char, and you'll get a lot of lines like;
DMP ready! Waiting for first interrupt...
ypr	0.01	-0.01	0.44
ypr	0.01	-0.01	0.44
ypr	0.01	-0.01	0.44
ypr	0.01	-0.01	0.43

Which are your yaw-pitch-roll Euler angles in degrees.

If you comment out the #define OUTPUT_READABLE_YAWPITCHROLL line, and uncomment the #define OUTPUT_TEAPOT line you'll prepare the code for use with the Processing demo. Instead of nice readable yaw-pitch-roll values the info is packed into a 42-byte FIFO packet buffer; [quat w][][quat x][][quat y][][quat z][][gyro x][][gyro y][][gyro z][][accl x][][accl y][][accl z][][]. Upload to your Arduino.

Moving over to Processing now, open the MPUTeapot demo. I find it nostalgic that this is running in OpenGL mode. If you want to increase the window size, go ahead and do that. Otherwise you just need to set the correct portName. For me that meant commenting out line 72, and uncommenting line 75, while updating line 75 to say "COM7". The demo is straight-forward, handling the drawing of the virtual plane, reading and unpacking the packets back to quaternion w,x,y,z values, and handing off the complications to the Toxi/c libraries. But when it works it's a lot of fun to manipulate the virtual plane by moving your MPU6050. And really helps you connect the board movement to what you want your project to detect/react to, in your mind.

Animation of a block plane (red board, green wings and tail fin, blue nose cone) rotating in 3D space.

MPUTeapot demo
Gif by [personal profile] chebe

Compare and contrast getting normalized accelerometer values, where the raw value is -13248.00. (A.k.a. Please, make it make sense.)

Library 1; jarzebski's Arduino-MPU6050 - https://github.com/jarzebski/Arduino-MPU6050/
Library 2; Adafruit's Adafruit_MPU6050 - https://github.com/adafruit/Adafruit_MPU6050
Library 3; i2cdevlib/ElectronicCats' mpu6050 - https://github.com/ElectronicCats/mpu6050


Library 1;


code )

Formula for normalized acceleration value at MPU6050_RANGE_2G;
raw * rangePerDigit * gravityConst
(raw * (1.0/16384)) * gravityConst
== (raw / 16384) * gravityConst
(-13248.00 * .000061) * 9.80665 = -7.92502845
== (-13248.00 / 16384) * 9.80665 = -7.92959589


Library 2;


code )

Formula for normalized acceleration value at MPU6050_RANGE_2_G;
(raw / accel_scale) * gravityConst
(-13248.00 / 16384) * 9.80665 = -7.92959589


Library 3;


code )

No normalized functions in MPU6050.cpp. Can use MotionApps files, but they go through Quaternions.
code )

Formula for normalized acceleration value at MPU6050_ACCEL_FS_2;
// jarzebski way;
raw * accelerationResolution * gravityConst
-13248.00 * 0.000122 * 9.80665 = -15.850057

// adafruit way;
// (raw * (1.0/16384)) * gravityConst
// (raw * (2.0/16384)) * gravityConst
(-13248.00 / (2.0*16384)) * 9.80665 = -15.85919
((-13248.00*2.0) / 16384) * 9.80665 = -15.85919

Result is twice that of the others. accelerationResolution itself seems to be twice the others, so half it? Not sure if this is a mistake, or I'm missing something about MPU6050_ACCEL_FS_2 vs MPU6050_RANGE_2_G.
raw * (accelerationResolution/2) * gravityConst
-13248.00 * (accelerationResolution/2) * 9.80665 = -7.9250

accelerationResolution and get_acce_resolution() only exist in the ElectronicCats version, they're not accessible in the i2cdevlibs version. That version seems to really want you to use the Quaternion path. But you can always use the range values from the other versions yourself on the raw values.

Profile

chebe: (Default)
chebe

June 2025

M T W T F S S
      1
23 45678
9101112131415
16171819202122
23242526272829
30      

Expand Cut Tags

No cut tags

Style Credit