MPU6050 Gyroscope Motion Controller for Raspberry Pi Pico/ Arduino Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is a Raspberry Pi Pico and on the end of it I have a very special sensor board [Music] meet the mpu 650 a six degrees of freedom sensor with a gyroscope and accelerometer built in it uses the I squared C Communications protocol this is branded from A to Z delivery I purchased a box of these for eight pound ninety nine making them very affordable inside the box I received five individual units it also came with some eBooks which I honestly didn't bother downloading each one comes pre-packaged in its own shielded container and it comes with two different rows of pins as you can see I 3D printed a block so I could neatly attach this all together while I'm testing this simply uses M2 hex nuts to hold everything together today I'm looking at the gyroscope the goal of this project is simply to detect if I'm tilting it forwards backwards left right or twisting it left or right along the z-axis this becomes your basic pitch roll and your controls the front of the unit is where the pins are lined so when I tilt this forward it should detect forward I use some simple if statements to indicate this using logic so now as I tilt the unit forward it reports forward and as I bring it back up it detects backwards if I tilt it to the right it detects it's rotating around the x-axis and if I tilt to the left it detects that it's tilting that way if I twist it right it detects and if I twist it left it detects as well you can download this project code from CK enthusiast.com simply go to our PI Pico projects and select the mpu 650 gyroscope detection program you will find three files inside this archive so these need to be loaded into thony and brought into our device before we proceed any further we of course need to wire this device to our Raspberry Pi the first two important pins are VCC and ground VCC the red wire here goes to pin 39. the ground can go to any of the ground contacts on the Raspberry Pi I have chosen pin 38 for the ground next we have SDA and scl you may be wondering what XDA and xcl these are auxiliaries of SDA and scl which means you can connect them to multiple devices Ado is a way of changing the address of the unit and I wouldn't worry too much about this now SDA is destined for pin one it is indicated by the yellow wire and simply plugged into the first pin of the board then s CL is destined for pin 2. as you can see from the orange wire even those there's four spare pins this is all we need to establish communication between the Raspberry Pi and the mpu6050 if you would like to download this block to 3D print yourself you can grab it from my website simply head over to 3D prints and click on it to download I'm going to quickly explain what's going on in this program this is using standard micro python we are going to load the IMU and import the mpu 6050 this is a separate.py file that's already been uploaded one of the dependencies for this particular file is Vector 3D this provides some mathematical functions that allow us to calculate our angles both Vector 3D Dot py and IMU dot p y must be present and uploaded to the Raspberry Pi in order for this program to work IMU stands for inertia measurement unit and this is directly responsibility for calculating the gyroscope when it's in action both these files originate from the micropython IMU micropython mpu 9x50 as you can see imupy is present as well as Vector 3D Dot py once that's been imported we then import time which allows the system to sleep we then from machine import pin and I squared C the communication protocols we'll be using pin references GP pins rather than physical pins this is worth noting because if you look at any pin out of a Raspberry Pi you'll see that the very first pin is referenced as one however gp0 is different this could be confirmed by looking at the Pico R3 revision where we got gp0 and gp1 but they are labeled physically on the board as one and two this can be quite confusing for new users so it's worth mentioning the first number here refers to the channel if you look on this diagram we have I to C 0 and I to C 0 here so this is Channel Zero and this will be channel one so if you wanted channel one you'd have to reference different pins to make things a little bit more confusing you can also reference it as part of gpu4 and 5 as well we then create an IMU object and reference it to the mpu 650i to C protocol what that means is it's referencing this class here and grabbing the data using it we're not going to go through all the details of how this works because this is becoming quite low level the point is this class is brought in and it's referenced we then print the temperature just because we can it's part of the IMU so we might as well throw it in there to read the accelerometer we need IMU dot Excel and to read the gyroscope we need IMU dot gyro IMU dot temperature will of course return the temperature and we're rounding it to two decimal points then we have a series of logic statements if the gyroscope is tilted around the x-axis by over 45 degrees then we register a rotation left when the gyroscope registers a positive value it is being rotated left whereas if it is representing a negative value it is rotating right in order to detect rotation backwards and rotation forwards we are rotating around the y axis to help visualize what we're doing we have a very simple diagram here this is the circuit board we're dealing with and along this front side is going to be the row of pins when we tilt it left or right we are rotating around the x axis when we are tilting forward and backwards we are tilting around the y-axis and when we're twisting our board it is rotating around the z-axis the number 45 represents the number of degrees that it must be tilted at any one moment in order to trigger this motion I set it to 45 degrees because it gives me quite a lot of control as to when I want to tilt it and when I don't want to tilt it it eliminates a lot of handshake if you want a more sensitive process you can reduce this number all the way down to one if you want to which case you'll get a lot of very rapid feedback just as an example of this let's try changing all these to one and we'll see the big amount of data we're about to get simply from our hand shaking and the device not moving very much foreign if we up the values to five we seem to be getting a lot of Rights and a lot of left by very slight movements I can trigger all sorts of results so this isn't really giving me the accuracy I want when I'm trying to deliberately get a result once I change it back to 45 it gives me very deliberate control of the unit in question meaning that I can get some twists when I want some twists I can get some rotations when I want some rotations and some tilts when I want some tilts to help slow down the program I use time.sleep 0.2 you can reduce this down if you want faster more responsive controls and this brings us to the end of our very simple program as you can see we now have some kind of motion control going on with the gyroscopic sensor this can be used for any number of things for example upon tilting forward you could start moving a mouse pointer upwards and when you tilt it backwards once it could stop the mouse and tilt it backwards again then it could continue going in the other direction you could even use twists left and right as Mouse clicks if you want it creating a very interesting air mouse design it can also be used as a monitoring sensor so if you have an object that should be very stationary most of the time but is moving at unknown times or unknown reasons then this could be a sensor that triggers some kind of warning that something's gone wrong the usage of this very cheap sensorboard is of course up to your imagination but now you know how to get data and make decisions with the gyroscope as to which direction things are being pointed you can certainly incorporate into one of your own projects thank you for taking the time to watch this video please like And subscribe and keep those videos coming from me many thanks again and I hope to see you all again soon [Music]
Info
Channel: Computer Knowledge Enthusiast
Views: 7,707
Rating: undefined out of 5
Keywords: raspberry pi, pi pico, rp2040, breakout, electronics, stem, learn, tutorial, diy, mpu6050, mpu-6050, GY-521, GY521, Gyroscope, Accel, accelerometer, sensors
Id: rlsr1mMCemk
Channel Id: undefined
Length: 12min 19sec (739 seconds)
Published: Wed Jan 18 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.