Measure Angles Easily with MPU6050 and ESP32: Part 1 - Library Walkthrough

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's going on guys welcome back to the channel and today's part one of the mpu6050 angle series with the esp32 we'll be talking about how to get accurate angle measurements with the mp6050 using the esp32 microcontroller as you could see here and by the end of this video you will be able to get values using the Arduino programming language to actually get accurate angle representation of your mp650 in space whether you're just doing Simple till applications or you're actually using a real life robot by the end of this video you will be able to get accurate values and the way we're able to do that is we're going to use a library called i2c devlib which is a really powerful library that uses a sophisticated form of Sensor Fusion that is it combines information from the gyro and the accelerometer on the MPV 6050 with some form of filtering such as Kalman filtering or complementary refiltering and it deals with the offsets for us in order to give us accurate angle values so if you have been researching this problem before online you'll realize that a lot of people are using these simple formulas and typically those formulas don't work well in application because they only work well when you have a very static application and you're just trying to measure the tilts however by the end of this you will be able to get accurate angles even when your robot or drone or whatever application you are using is moving in free space so that's pretty powerful so let's just jump straight into it here and if you have any questions about the fee or anything let me know in the comment section down below it is actually a pretty complicated problem and there there are Engineers out there who specialize in these sorts of things called Sensor Fusion so it's really sophisticated if you want to get into it we're not going to go into that in this video but maybe later on in the series we'll talk about how that works I certainly won't dive into the the library code itself we're just going to show you how to get it working in this part and we're just going to jump straight into it with the physical diagram here so I would imagine you already have an mpu 6050 with an esp32 microcontroller and we just need four jumper wires to connect the two uh microcontroller and the mp6050 now the esp32 module I'm using I'll just show you the one I bought off Amazon is actually this one right here the high let go esp32 room 32d so it's pretty cheap it's 10 bucks I bought off Amazon I do not work for high let go I just like some of their products and this one was really useful and really easy to use so this is the one I'm using some esp32s use a different pin output so if you're using a different one it will probably be similar just make sure you get the pins correct and don't mix up the the the Power Pin and the ground pin because that can actually fry your sensor so if you are using another esp32 just search the pin diagram online and try to follow along it should be pretty similar and the outcome should be the same so yeah so we have that and we have a pre-soldered MP 6050 I'll link the one I'm using in the description down below and we just want to make the connections as follows for part one and of course plug it into the power now that we have the physical connection with our mp6250 and the esp32 let's jump into vs code and platform IO which is the IDE we'll be using to set up our project with the library and get these angular values in real time okay guys we're jumping into vs code so what we want to do in vs code is we're going to be using an extension called platform i o let me move this to the right there and platform IO is just a nice extension vs code that allows us to easily manage our Arduino projects and I just prefer over using the Arduino IDE many beginners use the Arduino IDE I just think platform I O is just much easier to work with so if you want to use platform Io if you are watching this video you can quickly set that up by just going to the extensions here and a searching platform i o and just downloading the extension restarting vs code and once you do you just want to click this little alien on the side and he wants to click projects and configuration and we just want to create a new project I already had a project before which I was running let me just exit out of that and what we're going to do is we're going to create a new project so we we can see we had some quick projects I set up before but let's just do one from scratch and we're just going to call it mpu 6050 angles again because I'm doing this for the third time and then I'm going to search the board we're using so esp32 and there's many esp32 boards so that can be a little overwhelming so let's just do the esp32 dev module that's fine for the one we're using off Amazon and then we're just going to use default location that is fine as well and of course the framework is Arduino you can select this other framework which I never heard of and let's just go ahead and click finish so it takes a few moments there and yes we want to give it access there so we could see that it sets up our workspace and we have some other projects in the same workspace so the one I'm working with is MP 6050 angles again I should probably delete these but it's fine for now and it gives us a really basic project where we have our lib which is where our library code is going to go and our source and I just really prefer this because it makes everything straightforward with working with platform IO and their website is here as well so I'll also link this resource down below platform i o for vs code lightweight but powerful source code editor and once again you can find tutorials really easy to set that up online so we're in this platform i o project and so the first thing they have is this main CPP file that we're it's essentially skeleton code so if we go ahead and run this as a sanity check and send it to our esp32 to make sure everything is working fine it's not going to do anything but it's just going to show that it's uploaded properly and we're ready to start sending code to the esp32 so awesome success the next thing we're going to do is once we have that is we're just going to go to this i2c devlip and I'll link this down in the description below as well and this is the library we'll be using there's a lot of code here so don't be overwhelmed we won't use the majority of this code there's many things you can do with this repo and it has some instructions here and the instructions we'll be following is download zip Archive of the repo and move the relevant core and device drivers into our project which is what we'll be doing restart IDE if necessary we don't have to restart the IDE so let's go ahead and download this download zip and let's go ahead and open that and we can see that we can just drag it here and I already have it because I've been doing this before the video so I don't mess up when I'm making the video so i2c develop master and we're just going to drag the respective code in the folder we need so let me just snap this on the right there because I'm using a Mac so I'll get a snap it on the right convenience and let's just go to mpu 6050 there and the first thing we're going to do is we're going to drag the mpu6050 library into our lib here and we're also going to drag the ITC Dev which we need with the mpd6050 library so let's just drag that and that's all you have to do in terms of the library code in terms of getting it from GitHub and moving it here which is awesome so now that we have this what's nice about this library is they have some perfect examples which give us the angles without us having to do anything and there's much code here we're not going to go into much specifics of this code because that would be hours of time potentially so we're just going to go to examples and MP 6050 DMP 6. okay so I think that stands for something motion processing so we're just going to click the Ino file we're just going to copy that and we're going to put it in our main file because that's the one it takes when we upload it so let's just put it in our main CPP file so let's replace all this code do that and let's just start from the top here there's not really much we have to change the only thing we want to change is the initially is the bowed rates so we can see there's a bunch going on here in terms of initializing the variables let's just change the bound rate to 9600 and what's going to do is it's going to initialize the mpu and it's going to set the offset so we actually want to get offsets and offsets are a way we can get more accurate values from our sensor because a lot of or pretty much all sensors even really expensive sensors you can buy for hundreds of dollars come with some inherent offset that is they have some just intrinsic error and we want to subtract that error that we get from Those sensors from the values we're actually measuring in real time I actually have a video on actually calibrating the mp6050 with offsets I'll link that right here I did that with the mpg6050 on the Raspberry Pi Pico W but the concept is the same and what's nice about this is the library does that whole process in that video for us except in the Arduino framework so you can go watch that if you're interested we're gonna have to change these values in a sec here we're not going to do that yet and we can get those calibration values by running this code the first time and I'll show you how that works and so we go down there's a bunch of if statements and and loops and all other sorts of things but the main thing we want to focus on is we're just going to be printing this every Loop and actually let's add a delay here or else we'll get an overwhelming amount of values so let's just say delay let's delay every two seconds or every one second so we're just going to delay it and we're just going to add a semicolon there and what's going to happen is it's going to print the yaw pitch in the roll which is the three angles we need to get the orientation of the mpu 6050 in space so ypr yaw pitch and roll so it's doing all the magic for us so we don't have to worry about anything in terms of the theory of getting the the yaw pitching the roll for our applications because this Library covers most of the use cases you will need or many of the use cases you'll need in prototyping to get accurate angle representation so now that we have that set we said the bad way we can just go ahead and upload this now hopefully we did everything right once again we we did the library we set up the battery and let's just give it a sec here so I just want to show you my setup actually so my setup here is on a breadboard so I suggest you guys use a breadboard because it just makes things way easier in terms of calibration and stability and in part two we're going to be showing the orientation of this as a as a 3D representation on the screen with some software so that'll be pretty cool and having it set up on a breadboard just makes that a lot easier so it looks like it uploaded successfully so I'm just going to go ahead and go to the serial monitor there so it's saying send any character to begin BMP programming and demo so make sure to try to keep this flat when you first start this off so it's Flats and I'm just going to click any character's okay that's the first character that popped up to my mind and you can see it printed the offsets here so these are the three offsets in the in the X Y and Z and then the GX gy gz so we can see that it's getting the ypr here but what's happening right now is the reason they're close to zero is because I haven't even moved my accelerometer at all so let's go ahead and tilt it at an angle so let me just show you guys just to show you that I'm not fooling you or anything so we have this thing we can see initially that it was pointed this way so that means the yaw is going to be zero so y'all is when we rotate about I believe the z-axis so like this so we're yawing right now as you could see We're Young we're just twisting it okay so that looks pretty accurate to me and if I keep going it should get to 180 or negative 180 on the yaw which is awesome let's go back to the standard orientation I had here so it's going to be close to zero zero zero and let's start to change this angle so we can see we're rolling we're rolling now and we're going up to 90 degrees which is cool and let's go down to negative 90 degrees on the roll there so we can see about negative 90. so it is pretty darn accurate and what's cool is if I start moving this thing up and down if I start moving up and down we don't get major fluctuations in terms of the yaw pitching the roll which you'll see with the other the other equations you see online trying to get the angle for this device so this thing can actually move through space and still maintain accurate angle representations as other equations simple equations cannot seem to do because they're not using Sensor Fusion so one last thing we want to do in this video is we want to go back and just add these offsets in so this is the offset in the Z direction we're just going to plug that back in there we're just going to paste that in there I don't know why he doesn't use the the offset in the X and the Y but that's fine and so this is the GX offset so X dial offset we're going to paste that here and then we have the the Y jar offset and then we have the the Z gyro offset and we're just going to go ahead and run this code again and we should get more accurate representations of the angle so once again it's flat I'm letting it calibrate as it's flat okay so send any character once again and we're just going to get the same thing but it should be more accurate I have no way to prove that but typically it is more accurate when you include these offsets in so once again it is measuring the ypr and I'm just going to start spinning it on my table we can see that the yaw is changing as expected and once again if I start if I start pitching it so we could see that the pitch is changing so we could see that this is this represents the pitch that's changing let's go once again we're changing the pitch opposite way okay and that's pretty much it for this sensor tutorial so if you guys haven't already please be sure to subscribe to the channel because once again there are many cool sensor tutorials not just regarding the mp6050 but other sensors such as the BMP 280 um and other pressure sensors and other distance sensors and also there's many full stack content videos that we do regarding uh react native regarding react regarding node so if you're interested just in general software engineering there are many sources for you on this channel let me know what you guys think about this in the comment section down below if you're finding this useful in practice I know a lot of people on Reddit found this Library particularly useful and they can vouch for its accuracy there are some cases where this Library fails to maybe provide accurate values and that is people are complaining that it doesn't work in rocket applications because of the orientation of the rocket so let me know what you think in the comment section down below for sure and the part two of this video what we'll be showing is will be showing that 3D diagram and how to visualize this on the screen using something called processing which is another software we'll be using with toxic Libs and we'll be able to get a cool 3D representation on the screen to prove that this is working as expected and it's just a cool thing to show so stay tuned for part two of releasing that in the next coming days once again subscribe stay tuned to the channel like the video and thanks for watching everyone [Music]
Info
Channel: Shilleh
Views: 8,889
Rating: undefined out of 5
Keywords: esp32, mpu6050, arduino framework, angle measurement mpu6050, sensor fusion, dmp, mpu6050 angle calculation, dynamic angle measurement, arduino mpu6050, i2cdevlib angle measurement, 6 DOF accelerometer, platformIO, IDE, yaw pitch roll mpu6050, drones, robotics, tilt measurement mpu6050, tilt measurement, c++, arduino, vscode, vscode platformIO
Id: C93SoHYeuz0
Channel Id: undefined
Length: 15min 16sec (916 seconds)
Published: Sun Sep 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.