Arduino Uno R4 WiFi LESSON 22: Control a Servo With Your Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy do.com and we're here today with episode number 22 in our incredible nude tutorial Series where you're learning how to think like an engineer using the Arduino Uno R4 WiFi what I will need you to do is pour yourself a nice tall glass of ice cold coffee that would be straight up black coffee poured over ice no sh sugar no sweeteners none needed and as you're pouring your coffee as always I want to give a shout out to our friends over at sunfounder sunfounder is actually sponsoring this most excellent series of video lessons and in this class we will use the sunfounder elite Explorer kit now hopefully most of you guys already have your gear but if you don't look down in the description there is a link over to Amazon you can hop on over there and pick your kit up and believe me your life and my my life are going to be a whole lot easier if we are working on identical Hardware but enough of this Shameless self-promotion let's jump in and talk about what I am going to teach you today and we have an exciting lesson for you today and we're going to be taking things up to that next level because so far what have we been doing in this class we've been turning on LEDs we've been turning off LEDs we've been blinking LEDs we've been changing the color of the LEDs but today for the first time we're going to move something I'm going to show you how to create motion using a Servo sound good I hope it does so let's buckle up and let's get ready to go I will need you to get your kit out and I need you to find your Servo in the kit and for me and probably for you if you haven't messed your kit up up here in the corner is this little bag with this little blue motor looking thing in it take that little bag out carefully and neatly close your kit up and set it aside and then what we're going to do is we're going to get the little blue Servo out of the bag okay you're going to get the little blue Servo out of the bag and what we are going to do is we are going to get me out of your way and we are going to hook it up well what I want you to see is there are three wires red is power that will go to 5 volts Brown is ground Brown is ground and then orange is control now different servos might have a different color scheme but usually Brown is ground red is uh Power and then orange is control now the one thing I have to warn you is what I'm showing you today is for this little Servo the sg9 this is a low power Servo and you can run this Servo off the ardu if you are using a different Servo or a bigger Servo you need to check and make sure that that Servo isn't drawing more current than you can get out of the Arduino because if you try to run a bigger Servo or a big Servo off the Arduino you can burn it out but what do we have we have power red we have ground is brown and then we have orange is control now if you did want to run a bigger Servo you can still control from the Arduino but that red wire you would need to connect to an external voltage Supply a 5V DC voltage Supply that has enough current drive to drive your bigger Servo okay now if you do that you have to connect all your grounds together the ground of the Arduino has to be connected to the ground of the servo has to be connected to the ground of the external power supply okay but for this case and this small Servo it is okay to run it to power it and control it off the Arduino so I am going to get a red an orange and a black why because I like ground to be black and so we're going to go that way with it and so let's hook this thing up so black is going to go to ground Brown is ground so I'm going to put black lined up with that brown pen and then the next one the the middle one is red well I'll use red to Red here and so I'll come in with my red wire and I'll put that lined up with the red and now I have what I have orange I have orange and I will put orange to Orange some of the servos it seems like the control pin is sometimes yellow and if you're not sure look on the specs if you're using a different Servo than this now let's get this thing hooked up so we're going to go the red wire goes to power which is that 5vt pin and then orange is control and the thing is you can use whatever pen you want what whatever digital pin you want but it needs to be one that can produce pwm so it needs to be one that has the squiggly so I'm going to use pin 9 like that you see orange is connected to pin n and now what I'm going to do is black is going to go the black wire is going to go to ground g& D okay so I got the thing hooked up I'm going to see if hopefully I can kind of take this Servo down so it isn't flopping all over man and those wires are driving me crazy look at these wires everywhere there is no way to get that neater so I'm just going to have to power on through and live with a little bit of a Rat's Nest there but now that we have it hooked up now we're ready to start programming it so let's come over to our code view here and what I will tell you is the Arduino by itself does not know how to go in and control a Servo it needs a helper program what do we call those helper programs we call them libraries and we're going to have to use the libraries well in order to use a library in order to load a library you have to install the library so this is the first time we've seen this I'm going to show you how to install the library over here on your Arduino IDE you see these little books if you click on them that is your library manager you can also get to library manager by coming over here and you can come down to manage libraries like that okay and then you get to the same place click click all right now what library do we want to install well we want to install a Servo library now you can see it comes up with like a hundred different Servo libraries but we want this Arduino one that's called Servo by Michael Margolis comma Arduino we want to install that now notice I am installing 1.2.1 and as of the time that I'm making this video that is the latest library but if you're coming in later the best thing for you to do is use the identical library that I'm installing that way if you use the same Library I'm installing if you use the same version I'm installing you should get the same result and I am using 1.2.1 and now you just click install it goes out it gets that software it brings it down in into your IDE you see that I have successfully installed it now I have the library but if I'm going to use the library and you only have to do that once if you come back later you've still got the library but if I want to use the library I have to include the library in the program so I do a pound include you notice when it sees include it turns it that kind of yucky PE green color but that just means it recognizes it now I've got a tell it the library by an open triangular bracket the library is Servo theh is important close that triangular bracket now a little bit surprising and unusual on these include Library commands you do not end in a semicolon so that's one of the few places that you don't use a semicolon in Arduino now we've got to set up our pin we are going to have a Servo pin and that was connected to what that was connected to pin 9 and semicolon now we also the way we're going to move this is we're going to send it an angle we we could send it an angle of 0o or 45 or 90 or 1 2 3 4 5 6 any number between 0 and 180 now you've got to understand this is not a motor that will just Spin and spin and spin it will go from 0 to 180 and that is it you can 0 to 180 180 to zero but we send it an angle if we're going to send it an angle we're going to need a variable for that so we're going to say int Servo angle and we'll just initialize that to zero like that now we might want to talk to the serial monitor so let's go ahead and set up a b rate BR is going to be 115200 like that okay and now this is something that is a little bit new and that is that in the software if I want to talk to this physical software if I want to talk to this physical Servo I need to set up a Servo object in the software that I can talk to and what do I do I call the servo library right this is the library so you have to type this exactly like this Servo and then what do you want your Servo object to be named I think I could call it blue Servo like that that would be good but it'll be a little shorter I'm just going to call it my Servo okay now you could call it X if you wanted to but if you came back and looked at your code later it'd be hard to understand what you were doing so if I call it my Servo it's kind of clear what it is so I like that okay I have those things set up now we will come down here let's go ahead and turn the serial monitor on just in case we want to use it uh serial. begin and BR and sometimes you think oh I don't need the serial monitor for this program but what if you have an error and then you want to print out some things to try to do some debugging I usually turn it on now you might be thinking that we are going to do a pin mode and if you're thinking that you would be wrong because what we're not going to do is we're not going to do a pin mode but what we are going to do is we are going to attach the servo to that pin well how will we how will we do that well what is the object what is the Sur Servo called my Servo what do we want to do we want to dot attach it what do we want to dot attach it to Servo pin like that and so now that Servo Library knows that it's talking to pin n which is Servo pin hopefully that makes sense now we're going to come down here also I'm probably going to need a delay time so I'm going to just put int DT and I am going to put like let's say 50 milliseconds now why do we do that because you can't just sit and Hammer that Servo because it'll get confused because the servo gets your command it moves to that position and that physical motion takes a little time so you can't just sit and give it a bunch of Servo commands because it takes a little time to move something physically in the real world I think 50 milliseconds should be okay now how do we move the Sur Servo I'm going to do a my Servo do write and what do I want to write to it the servo angle like that okay now what did I set my Servo angle to I set it to zero all right and then I set it to zero and so now I'm going to do a delay of DT so it's not just sitting there talking to it too quickly now the first time you run it don't have any of these little horns on it cuz I want to set it to the zero position and after I set it to the zero position then I will put the horn on it so we're going to try to run this thing let's see what happens looking pretty good none of that ugly orange nonsense and it is going going going and boom I don't know if you could hear it but when I ran that I heard a little noise so this was not at the zero position now it's at the zero position if you did not hear a little Buzz just a very short little buzz probably you were already at the zero position now I'm going to get one of these horns out so there is a bag inside the bag and then I'm going to get this horn that is just the simplest one that's just kind of straight up straight across this one okay that's the one I want to use I don't use these screws maybe if you were doing something perfect you know if you were doing something more permanent you would screw it on but I don't want to fool with those screws it holds on there pretty snugly without that now what is in math zero angle is pointing along the positive x-axis so this would be the zero angle and so I want to look at this and I want to point it along the positive x-axis because that in fact would be 0 de and so now this is oriented kind of like with an imaginary coordinate axis like that okay so now let's come in and let's say if my Servo angle was 90° now let's run it and see what happens all eyes on the servo all eyes on that little horn and it compiled it is downloading and and boom it went to 90° now you see it doesn't look exactly 90° but when I started you know with with the way those gear teeth worked it was a little bit further over here so it did move 90° and then what if we said we wanted to go to 180 and it's to Absolute position it isn't I'm moving it 180 further I'm moving it absolutely to0 and so let's try that okay we're moving stuff guys this is pretty exciting and boom it came over here to 180 now what is the one thing thing that's kind of awkward about our program you don't want to run the program again every single time that you want to move the serot so what I'm going to do is up here I'm going to just declare that variable and now I'm going to ask the user what angle they want to be at and I can read that into Servo angle but I'm going to have to prompt them so I'm going to have to create a string like that and that is going to be promp T and what is the promp T it's going to be equal to the string the string what angle do you desire like that okay and then semicolon what angle do you desire that is my prompt now if I want to get that from the user down here what three things do I have to do I have to ask I have to wait and I have to read so how do I ask I ask nicely by doing a Serial do per per print print Ln and then promp like that always check my variables Pro yep that's right okay so I've now I've asked now what do I do I need to wait how do I wait while serial. available open close equal equal Z that is my condition and while that is true what do I want to do absolutely nothing in this F Loop it just Loops until it gets something from the user and once it gets something what does it want to do it wants to read it and so Servo angle I am going to read and I do that as a serial do parse int because we had it set up as an INT and then like that and so now I should have that angle and if I'm thinking about this right this should work so let's run it okay none of that ugly orange nonsense and it is downloading and we're going going going going going and boom look at that look at that now I am not sure why it started up and down okay it it it's like it started kind of in a strange thing maybe I should have like done a Servo angle set it up here and then set it to a specific location but what you can also see is you can see that it's not asking me because the serial monitor wasn't open when it ran so I'm going to go ahead and start it again and now it's saying what angle do you want I'm going to say zero and man I sure hope it goes to zero and boom it went to zero now let's say 90 and boom it goes to 90 let's say 180 and it goes to 180 who's your friend 45 and then when we do 45 it goes to 45 who's your other friend 13 35 and it goes to 135 and then what if we had 89 okay and then boom it goes to 89 okay man do you guys see that we are controlling this thing that is that is really fun okay I can't I can't I can't help myself I shouldn't do these things on the fly but what I'm going to do is instead of asking the user I'm just going to put it through its Paces okay I'm just going to put it through its Paces so here in the loop I'm going to say four I is equal to zero I'm going to start at zero and then remember it is a semicolon in there and then I'm going to keep going as long as I is less than or equal to 180 okay less than or equal to one 180 like that and then uh I is equal to I + 1 so I've created a four Loop Loop now I've got to open the for Loop CL clause and close it so now all the code in here will be in it well what do I want to do I want to do a Servo Dot and it's not Servo it is my Servo right we called it my Servo so I'm going to do a my serv. write and then what do I want to write well I want to write this number that I'm going that I'm taking from 0 to 180 so that's going to be I okay and now I'm going to delay by DT like that and we got to remember our semicolons now if I'm going to use I down here I better declare it up here and I don't have to give it a value because that's taken care of in the for Loop but I just got to tell it that I is an INT and then uh I'm just going to go from 0 to 180 and now let's try that and see what happens so okay we're going to run it and what did we do wrong there for ah it should be a semicolon and what did I use I used a comma different languages use different uh formats like that and sometimes I get a little rusty on it okay look at that look at that look at it go it went from zero we to 90 and it's stepping through stepping through stepping through and it's going to come to 180 and then it goes back look at that that thing is stepping through there it is stepping through there okay what do I not like uh I don't like how it goes back all at once and so what I want to do is I want to step it up and then I want to step it back so I'm going to take this and then just as I stepped it up now this time where am I going to start I'm going to start at 180 okay and this time as long as I am greater than or equal to 0 and each time I is going to be equal to I minus one so let's think through that here I start at 0 and I go to 180 but when I get done where is it it's at 180 so this time I want to go from 180 back to zero okay and then each time I'm going to go down one 180 179 down to zero and then it'll fall out and then it should go back and forth now I'm going to put uh let's let's make this I hope this isn't too painful but let's make it a 100 it's kind of a little bit not exactly what I wanted to see there so let's play with that value a little bit here it [Music] comes okay now yeah now that is a little bit too slow but we'll let it go through what I would probably do is I would probably come back and I would make it a little bit faster because that is a little bit too slow now what we want to see now is we want to see it turn yeah you see it's turning around and going back now so that is doing exactly what we wanted it to do now what is cool about this if we had a temperature sensor then we could put an arrow on here and we could have a dial that sort of points to what temperature we are at based on the temperature sensor or we could give some sort of other indicator on here or we could have a little box that we open or closed maybe as it saw something happen the the box would open or the box would close you see we're able now incorporate motion into our programs which is a pretty cool type of thing now my friend it is time for your homework assignment what your homework assignment is is to write a program where you have the servo and you have the potentiometer and if the potent and listen because sometimes you guys don't listen to me it needs to be exactly what I am saying when your Servo is turned all the way when your potentiometer is turned all the way to the left your Servo needs to be pointing to the left and then as you turn the servo to the right the potenti when you turn the potentiometer to the right the servo goes to the right so you're setting the position of the servo based on the potentiometer and don't do it backwards when you're all the way to the right on the potentiometer and your Servo is all the way to the left no have it where as I move the as I move the potentiometer the servo moves along with it and it tracks it it tracks it the servo tracks the potentiometer one other quick note I got to give you don't use your Servo as a fidget toy what what used to bug me when I had real students in the classroom they would sit there with their Servo and they would just turn it like they would sh turn it by hand it is very easy to damage these servos there's these little plastic gears in there and if you spin this thing by hand you'll damage those gears in there so don't sit and fidget if you want to fidget get a pencil and go like this with it or maybe get a straw I used to give kids straw so they could sit and they could drum with the straw and it wouldn't it wouldn't bother other students but don't there's all types of fidget toys you can use or make or think about but don't use your Servo as a fidget toy so never turn the position of the servo with your fingers by spinning that thing important little tip there okay I am going to come over here so that is going to be your homework assignment and guys I hope you're enjoying taking these lessons as much as I am making them as always I want to give a shout out to you guys who are supporting me over at patreon you are the guys that keep this channel going and I really appreciate it and I thank you for standing with me you can also help me by giving me a thumbs up or leaving a comment down below if you have not already subscribed to the channel when you do ring that Bell so you get notifications When Future lessons drop and as always most importantly share this video with other people because the world needs more people thinking like an engineer and fewer people sitting around watching silly cat videos Paul mcquarter with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 2,557
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: mDyQkTIzDsc
Channel Id: undefined
Length: 25min 45sec (1545 seconds)
Published: Thu Jun 20 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.