Build an Electronic Level with MPU-6050 and Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop we'll be working with the MP you six zero five zero an inertial measurement unit we'll see how the MTU six zero five zero works and how to use it with an Arduino we'll also build an electronic level it's a well balanced episode today so welcome to the workshop [Music] hello and welcome to the workshop today we are going to be working with the MP u 6 0 5 0 which is an IMU with six degrees of freedom now if none of that makes any sense to you don't worry I will be explaining that in a few moments but essentially this is a device that contains both a gyroscope and an accelerometer and it can be used to measure inertia it can be used to measure position rotation etc you'll find devices like this used in things like self-balancing robots and robot arms you'll also find it in quad copters and other devices like that you'll even find a device like this in a hard disk because it's used to sense whether the hard disk is being dropped toward the floor now this is a very sensitive and very complex device yet it is quite inexpensive and this video by the way is something that many of you have requested that I do and the way that this was requested was by using the form that I've provided for doing that the survey form and you will find the length of that survey form at the bottom of every one of my newsletters as well as in the welcoming letter for the newsletter so if you haven't signed up for the newsletter please do so especially if you want to suggest content for videos because I'm not taking suggestions via email or by a YouTube or website comments anymore simply through the survey the survey allows me to find out just how popular your suggestion is so that I know what videos I can make that he will appreciate the most so back to the mpu 6 0 5 0 what we're going to do today is we're going to learn a little bit about how it works and what some of these terms like IMU and degrees of freedom actually mean and then we're going to do a little test of it using a library or a couple of libraries actually that you can download directly from the article that accompanies this video on the drone ba workshop comm website and then after that we're going to build a little project one that's going to replace one of the tools I have right behind me this particular tool is the one we're going to replace this of course you'll recognize level so we are going to build a digital level so as always there is a lot to cover today so let's learn a little bit about the MP u 6 0 5 0 let's take a look at the MP u 60 50 IMU the FPU 6050 is a low-cost and accurate I am you an I am you is an inertial measurement unit the MP you 6050 consists of both an accelerometer and a gyroscope devices like this are used in UAVs like quad copters and helicopters I am user used in robotics applications such as positioning robot arms or keeping self balanced robots balanced they're also used in phones games and they are used in hard drives to lock the heads when they detect that the hard drive is being dropped now let's take a look at the accelerometer function as the name would imply an accelerometer is something that measures acceleration let's imagine a box that has six sides like any box label the sides X plus X minus Z or Z plus Z minus y - and we're looking down at the box right now from the Y plus now let's place our measurement device in the center of the box our measurement device will remain stationary but the box is free to move right now everything is still so the x y&z values are all zeros now we accelerate the box in the direction of the X plus the acceleration is in that direction while the inertial force is in the opposite direction now the x value will be 1 the Y value 0 and the Z value will be 0 let's bring our box back to the stationary position and x y&z are now all 0 again now we will accelerate downward our initial force again is in the opposite direction our x value is 0 Y value is 0 and the Z value is negative 1 because we're going in the downward direction now let's accelerate in the direction of the X plus and the Z minus our x-value will now be 0.707 our y value will be zero and the z value will be negative zero point seven zero seven a gyroscope is a device that can measure rotational movement on three different axes the x y and z axis the gyroscopes used in the NP use 6 0 5 0 is called a MEMS gyroscope MEMS is an abbreviation for micro electromechanical system now MEMS sensors produce a voltage when the perspective axis is rotated these voltages are sampled with a 16-bit analog to digital converter inside the MTU 6 0 5 0 the mpu 6 0 5 0 also has a temperature sensor in addition it has a digital motion processor or DMP this DMP correlates the data from the accelerometer and the gyroscope you can also use external third-party sensors with the DMP such as a magnetometer these external sensors will connect using a second eye to see bus now here's the MPU 6:05 zero module that I'm using note that there are many different styles of module but this is a very common one the first pin is VCC which for this module is 5 volts DC note that the mpu 6 0 5 0 is actually a 3.3 volt device but there's a voltage regulator and some resistors to do voltage level conversion on this module the next pin is simply a ground pin the SCL pin is the i2c clock pin and the SDA pin is the i2c data pin the XDA pin is the external I - C data pin this is what would be used if you're connecting something like a magnetometer or other third-party sensor to the MP u 6 0 5 0 XC L is the external I - C clock the a d0 is an i2c address bit selector you can use this to change the i2c address of the module in actual fact the module has 2 i2c addresses the int pin is an interrupt output pin and the module also has markings to illustrate the X&Y orientation of the sensor so now let's start working with the MP u 6 0 5 0 so here's a look at the MP u 6 0 5 0 module that I am using now it's a rather tiny little device this is a typical MP u 6 0 5 0 module although there are other ones as well we'll just bring it so you can sort of see it all the connections are up on one side over here and the actual MP u 6 0 5 0 is right here on the module itself and there really isn't anything on the back of the modules so it's a very basic little module has a couple of mounting holes in it and as I showed you earlier it's also got some markings here for the orientation so that you can tell which way the X and the y-axis are on the module now in order to use this module as you'll see in my demos I'm gonna to be mounted onto a small little wooden stick to make it a little easier to manipulate but there you are a tiny and inexpensive mpu six zero five zero module now one of the specifications that I didn't cover in the presentation you just saw about the mpu six zero five zero is that it is advertised as having six degrees of freedom which may be a bit confusing because most of us know what a degree of freedom is they represent the ability to move in the three different dimensions height width and depth so how can this have six degrees of freedom well the answer is it's the combination of the three degrees of freedom that the gyroscope has and the three degree of freedom that the accelerometer has and you'll see other I am use advertised as having nine degrees of freedom and those ones typically have in addition to the gyroscope and accelerometer they have a magnetometer in them as well and that would provide an additional three degrees of freedom so that's what the term six degrees of freedom means just in case you're a bit confused about that now on to our first experiment what we are going to do is we're going to hook up the MPU six zero five zero to an Arduino and we're going to use a couple of libraries first which are quite complex but they will show you how the device works and deed it'll tell us how we can test the device I'm gonna mount my device on a small piece of wood in order to make it a little easier to manipulate one thing that some of you who are very observant might realize when I start getting into the demos is that I normally work on a Linux machine and today we're working with Windows and you might wonder why that is well we had a little bit of a crisis here in the workshop earlier this week the SSD drive on the workshop computer which is a Linux computer just up and died which was quite a surprise to me because the drive is only four months old however it is what it is so I brought a Windows computer in in order to be able to create the video on that I do have a new hard drive to a sort of SSD drive to replace the one that had died and I will be getting that fixed up so Windows is only a temporary arrangement over here and for any of you who might be concerned no I didn't lose any data I don't keep my data on my computers I keep it on a network storage device which in turn is backed up to a cloud so that was safe it was just really an annoyance so having explained the Windows Sagat to you let's go and see how we can hook an MP you 6 0 5 0 up to an Arduino so here's how we'll hook up our first experiment you're going to need an Arduino Uno and then PU 6 0 5 0 module now your module may be slightly different than mine but it should have similar pin outs we'll start by connecting the VCC on the module to the 5 volt output on the Arduino will connect the modules ground to the Arduino ground the SCL which is the i2c clock line is connected to analog input a 5 on the arduino now some arduino also have an independent SCL and SDA input you can use those instead in a similar fashion the SDA on the module is connected to analog pin a4 or the dedicated SDA pin on your Arduino and finally the interrupt pin on the module is connected to digital i/o pin number 2 on the Arduino Uno so now let's take a look at some code we can use to work with our MPU 6 0 5 0 now for our first test with the MTU 6 0 5 0 we're going to use a couple of libraries that are provided by Jeff Roberge and that are up on github now Jeff has written an entire set of libraries for developing with the i2c bus and these libraries are not just for the Arduino they're also for a number of different microcontroller units now you may find it difficult to pick out the two libraries that we are going to need and so what I've done is I've created zip files of them and I've placed them on the article that accompanies this video on the drone bot workshop comm website and of course you'll find the link to that article right below the video so download both of the zip the next step will be to install them into your arduino ide so go into the arduino ide and go up to sketch and go to include library and go to add zip library navigate to where you've placed your libraries minor in my downloads folder and these are the two you want to install i to see dev and NP u 6 0 5 0 now you'll need to do this operation twice once for each of the libraries once you've installed this into your arduino ide which i won't do because i've already done it you can now go and open the example file to come with it so go into examples and scroll down until examples from custom libraries and you'll see MP u 6 0 5 0 go into examples and we're going to look at this first one MP u 6 0 5 0 d MP 6 this will be one that illustrates the use of the digital motion processor that is contained in the MP u 6 0 5 0 now this is a very extensive piece of code and i'm certainly not going to go through it line by line quite frankly i don't particularly understand all of it myself however there are a few facts that you should know about it and some things that you may need to change to get your code working now at the beginning of course you can see it includes the two libraries which is downloaded and also includes the built-in arduino wire library for talking to i to see now the default for this class is to use hexadecimal 6 8 as the address for the MP u 6 0 5 0 now that is the default address that my module is using so i don't need to change anything however you can use a statement over here and change it to a different address if for example you've used that address 0 pin on the MP u 60 52 change your address you may need to change the address over here another thing to note is down in the setup and we'll get down to the setup over here and we will look for the serial monitor now it is set to use of very high moderate on the serial monitor now there are notes here about using arduino pro mini zat 3.3 volts and eight megahertz and having to drop that also for the demonstration we're using you could change this if you wish because this is really used for the teapot demo which is an external demo that uses the processing program and we aren't earning that one today you'll find several examples of that by the way up on YouTube but I'm leaving this at the high baud rate but just remember to change your serial monitor to that rate otherwise as I said this code is very well commented and so you can go through the code to find out exactly what it does it does a lot of things that are well beyond what most of us would have to do unless we're building things like quad copters or something for example this one here output readable quantanium well what exactly does that mean well I have vac you look that up myself and in mathematics quantanium czar a number system that extends the complex numbers first described by Irish mathematician William Rowan Hamilton in 1843 and applied the mechanics in three-dimensional space which is quite something now this outputs it in the easy matrix form of WXYZ well what does that mean well a quantonium with a complex number with a W that W is the real part in the x y&z are imaginary parts and I'm reading this by the way if a quantanium represents a rotation then W equals cosine of theta over 2 where theta is a rotation angle around the axis of the quantonium so this is pretty heavy-duty stuff and is not something we're going to go through today what we are going to do actually is go down and get this output over here and this is what our demo is going to give us the yaw pitch and roll values those of you who work with quad copters and other flying devices will be familiar with yaw pitch and roll representing the different degrees of freedom that your quadcopter or other flying device can go in so that's what we're going to be presenting with this serial monitor today but again this code is very well commented and so if you would like to do something very complex with the NP you six zero five zero this indeed is the code to use so what I'm going to do right now is load this onto our Arduino with the NP you six zero five zero and do a quick demonstration with the serial monitor just to see the results we get free yaw pitch and roll okay so I've got my demo over here you can see my MCU sixty fifty mounted on its piece of wood which I'm using just so that it's a little easier to see what direction I'm holding it in while I manipulate it and we let the serial monitor on and it starts off saying initializing I to see devices it's tested the connection and it's connected successfully now what you need to do is you need to use the send line in the serial monitor to send any character you want in order to get this started so I'm just going to send a character to it it initializes and we're getting yaw pitch and roll values and so watch what happens as I move my sensor around and as you can see it is indeed manipulating those yaw pitch and roll values on the monitor and so this pretty well proves out that our m pu 6 0 5 0 is functioning correctly so now let's move on and look at another sketch this one does not use these libraries alright now we've seen how the NP use 6 0 5 0 works with the two libraries and as you can see it is capable of giving back a wealth of information regarding the position of the device the acceleration of device the movement of the device etc but for our next project we don't need all of that information so we aren't going to be using those libraries instead we are going to work directly with the chip just using the Arduino wire library now our project is as I said at the beginning of the video a digital level and this is actually a practical project now I'm doing it on a breadboard but you could easily put this into a case and use it as an actual tool so you might want to take a look at building one of these in a permanent version perhaps using something like an arduino nano or a pro mini instead of the Arduino Uno in order to reduce its size but at any rate what we're going to do right now is take a look at the hardware we're going to need to attach to the Arduino in addition to the mpu 6 0 5 0 and then of course we will look at the sketch and then finally do a demo of our digital level now to build our electronic level will require the following components an Arduino Uno or another similar Arduino if you want to make a smaller version of the level you could use an Arduino Nano or a 16 megahertz Arduino Pro Mini will also of course need the MPU 6 0 5 0 IMU and a liquid crystal display with an i2c connector will also be using 5 LEDs I use two red LEDs two yellow LEDs and a green LED but of course you could substitute different colors or even use an LED bar graph assembly if you wish and finally we require dropping resistors for all of the LED I'm using five 220 ohm resistors but any value from 150 to 470 ohms should work fine we'll begin by connecting the five volt output of the Arduino to the VCC connection on both the MPU six zero five zero and the liquid crystal display next we'll connect the ground to the ground connection on the MPU six zero five zero and the LCD display will also connect the ground to the cathodes of all of our LEDs analog input a4 is also the SDA signal connect this to the SDA connection on both the MPU six zero five zero and on the LCD analog pin a5 is the SCL signal connect this to the SCL on the mpu six zero five zero and on the liquid crystal display and finally we need to connect the anodes of all of our LEDs to pin 9 10 11 12 and 13 each will run through a dropping resistor and then head to the respective LED now that you have it hooked up let's take a look at the sketch we'll use to build our electronic level meter now before you run the sketch you're going to need to install yet another library into your Arduino IDE now this is for the liquid crystal display using i2c and it's the new liquid crystal library you can get it on bitbucket and you'll find the link in the article associated with this video once you've downloaded the zip file move into your arduino ide and install it in a similar fashion to how you installed the other two libraries and the sketch we looked at earlier now let's take a look at this sketch we start off by including the wire library which is the built-in library for working with i2c unlike the last sketch we're going to work directly with the MP u 6 0 5 0 through i2c then we also need to install the library we just installed the liquid crystal eye to see library we define the connections to the LCD these are acting the connections on the adapter on the LCD the i2c adapter and we need to define the address for the LCD so if you have a different address you'll need to change that after that we define the LEDs that we are going to be using for our bar graph display and so we do that over here next we set up a number of variables for our gyroscope and these are all a number of different floats Long's and boolean's that we are going to be using to do the calculations in order to determine the pitch and the roll if is what we're trying to do today we also set up a couple of timers and a counter that we're going to use on our display this is because we're going to be bringing the data back very fast and we cannot display everything on the LCD display it is simply too fast for it now in the setup will start the i2c and we'll set up the LCD is a sixteen by two character LCD will also define all of the LEDs as being outputs then we call a routine that we'll show you in a moment that setup MPU six zero five zero registers this is the one that's manually going to go and do what we did with a library earlier now in this second over here we actually need to read the gyro data and the accelerometer a thousand times and this is in order to stabilize a device so there's going to be a delay when we start off and so you will expect when you first start this project that nothing will happen probably for close to seven or eight seconds we divided all the results we've gotten by a thousand in order to get the average will start the serial monitor now I have this remarked out right now because the actual project doesn't use the serial monitor but I've put it in there in case you need to do some troubleshooting note the speed of the serial monitor it has to be that fast in order to display all the data this is why we can't display all in a liquid crystal at one shot we also set a timer up and we use the micros command now what that function does is it counts number of microseconds since the Arduino has been powered up or rebooted you might remember some of our other sketches use the Millis command which counted the milliseconds this counts microseconds now before we look at the loop let's quickly go down and take a look at the functions that we have at the bottom over here one called set up MTU six five zero registers and in this one we use the wire library to manually set a number of the registers for both the accelerometer and for the gyroscope there's also one to read the mpu six zero five zero data and this reads the raw data from the gyroscope and the accelerometer and again we are just simply doing this while not simply put we're doing this with the wire library directly on I to see now these statements over here you may have not seen before with what looks like two less than signs and an eight what that actually does is it shifts things by eight bits and this allows us to take two 8-bit values and turn it into one 16-bit value which is what we'll need to do in order to read the data because it will not fit into an 8-bit value now let's go back up into our loop now there's a lot of mathematics over here and we're not going to go right into that but we'll start off by getting the data from the MPU 6050 using the function we saw before and we'll calculate the offset for the gyro after that again a number of calculations in order to do the roll and the yaw angle then the accelerometer calculations again I'm not going to go over all the math over here and many of these functions are in the public domain I certainly didn't write these all myself when we finally get to the bottom of this we are going to take the average of everything and what we are going to get is the pitch and the roll output and that is what we're looking for it's actually the pitch output to determines the level but we're also going to display the roll on our display now our display can only handle so much data so what I've done is I've divided it and taken only every 100 counts now you can play with this number if you want to make it more responsive but if you go too low with this number the display is not going to be able to keep up with the data so we use a counter over here and once it is gone over 100 will go and print on our display so we first clear the liquid-crystal and then we go and print on the first row of the LCD so we set the cursor position and we print pitch and then we actually print the actual pitch and then we move the cursor to the next row and we print roll and print the actual roll and now we go into our LEDs and the LEDs are looking at the pitch output and it's looking at what range there and in determining which LED should be turned on so for example if we want to go on to the level LED the one that's in the middle will go and look at this one over here so if the pitch angle is within one degree of zero then we'll turn on the middle LED to say everything is level now you can adjust this if you want to make this more fine-grained you might make it only half a degree or something like that and similar to all the other LEDs over here keep in mind too that you could also use a sketch like this in order to light an LED when something comes to a specific level it doesn't actually have to be zero look I'm using it over here and you might have an application where you want to trigger something when the sensor is at a specific level and you could use that and so after that we'll set the display count back to zero and go out of there and then we use a timer over here we've use of micros in order to make a small delay we reset the timer and we finished the loop and started over again and so there's a lot to this sketch a lot of it is mathematics but it's all very well commented over here if you want to read the map the sketch can understand what the math is actually doing and so now that we have seen the sketch let's load it up to our project and take a look at it in action all right here's our demonstration all hooked up I'm going to plug the power into it right now and when you first start off remember it has to calibrate itself so you'll see nothing on the display at the beginning and there we go we have a display and also our center LED is lit because everything's pretty flat now let me lift up the end a bit and as we go up the yellow one and the red one and notice the pitch values there are negative I'll bring it back down to the center and move it up over here and they're positive and of course as we go above it just stays with the red LED because we've exceeded 2 degrees and we'll get back down into the center you can also see the roll values when if I rotate it you'll see that the roll values are changing as well and so there you go it seems to work very well and you could actually use this as a permanent project if you wanted to put it into an enclosure and maybe use a smaller Arduino with it and so our electronic level meter seems to work quite well ok well this brings us to the conclusion of the video and we've done quite a bit today we've learned about the MPU 6:05 zero and we've built a little electronic level which can actually as I said become a practical project if you want to put it into a different case and maybe use a smaller Arduino with it and I'd like to thank you for having joined me today now if you need any of the code or if you need the two libraries that I used in the initial demonstration you will find those all on the article that accompanies this video and that's on the workshop comm website there is a link to that article right below the video and while you are on the website please sign up for the newsletter if you have not already remember this video was something that was suggested by my newsletter subscribers and so if you'd like to suggest a video you'll find a link in the sign-up letter as well as the bottom of all of the newsletters going forward that you can use to go to the survey to input your suggestion and also vote on some of the other suggestions for topics for videos and articles and on the subject of subscribing if you have not done so already please subscribe to the YouTube channel I would be very appreciative of that so until next time please take care of yourselves I hope none of your SSD drives fail and I hope to see you very very soon here in the workshop good bye for now [Music]
Info
Channel: DroneBot Workshop
Views: 183,412
Rating: undefined out of 5
Keywords: mpu 6050, arduino project, electronic level, mpu-6050 arduino, mpu6050 tutorial
Id: XCyRXMvVSCw
Channel Id: undefined
Length: 33min 36sec (2016 seconds)
Published: Sat Apr 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.