Arduino Tutorial 30: Understanding and Using Servos in Projects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 30 in our new our improved our legendary Arduino series of tutorials what I'm going to need you to do is pour yourself a nice big mug of iced coffee ahh refreshing that would be dark black coffee poured directly onto ice no sugar no sweetener especially not those sweetener packets had some guy leave a message the other day saying he was having trouble following along my lessons we'll come to find out he had been putting vodka in his coffee and I had not issued a warning about not doing that okay no vodka no sugar no sweetener packets nothing in the coffee except strong black coffee poured over ice ok need you to get that I need you to get your fabulous a Lego kit out if you haven't picked one up yet it is the super starter kit look down in the description you can click on the link to get one it's got a boatload of components and all the lessons that we are doing in this series is with components from the e Lego kit it does make it a little bit easier if we are using the same hardware ok what we are going to be doing today is we are going to be learning how to use a servo what is a servo a servo could be thought of like a little motor where you can set its position you can send a position command to it and it will move a little arm to that position well you can think of it like a little motor a normal servo doesn't keep spinning it typically can go from about 0 to about 180 and that is the range of motion it has the good news is it's very easy to use and I will be showing you how to use one today so if you get out your e Lego kit I will need you to go ahead and I will need you to go ahead and get your servo out you can see it in your kit maybe I could turn it sideways and you can see a little better what it looks like it's that little blue thing let me also say that you know I really like the value that we get in these elia Lego Kitt's I like just all the boatload of components but when you sell a big batch of components for $35 when it comes to something like the servo you are not getting the top-of-the-line servo so it really works well for learning it'll do everything that you need in order to learn and you can use it in this lesson but if you're really going to be into the Arduino I'm gonna recommend that you get one of these high-tech get a couple of them I've got links down in the description these are much smoother much stronger much better operating servos so you can go ahead and get those and kind of just put them in your in your tool kit the one other thing that I've got to say about servos is is that this servo that comes with the kit and this high tech that I am recommending they can be powered absolutely fine from the Arduino but if you're going to use a different servo particularly as you start using bigger ones you have to look up on the internet put the output current capability of the pin that you're using on the Arduino that you're using and then you'll have to look at the current requirements of the servo and make sure that you can power the servo from the Arduino pin that you're using bigger servos you cannot power from the Arduino and therefore you have to have an external power supply no problem you you you power it from the external power supply but you still control it from the Arduino but it's just you do not want to you certainly do not want to try to drive a big servo from the Arduino so consider yourself warned there all right so let's jump right in and start hooking this thing up so I guess I should put it here where you can see it you can see that for this particular one and many many servos have a very similar color code and I will kind of go over that with you and I am looking for my wires here as we speak alright so the first line that we're going to connect is the red line okay and that is power so I'm going to bring my red wire and I'm going to put it into that center pin that's connected to red and then I am bringing that over here to the Arduino and I am looking for five volts and I okay I'm gonna be cognizant of trying to keep this in your field of view so that is connected to five volts alright we'll get to what these other components are in a minute that we're just going to jump we're going to jump right in here okay now I'm going to need a black wire and on on this particular servo what we have is is that the brown is ground Brown is ground and so with how I am holding this it's brown in the top so brown is ground and I'm going to come over to the ground wire alright and now orange is for control so I'm going to come over that is not going in very well okay the orange wire is on the bottom so I'm gonna go ahead and use an orange wire okay and then I do believe that today I will be using ten nine I will be using ten nine alright so now we have a little servo hooked up what I will also do is I will also take this little horn that is also in your kit and I'm going to go ahead and plug it in so you can see things moving and you can unplug it and plug it back in at your leisure but we are going to now switch over we've got it hooked up we're gonna switch over and see if we can start programming this thing so I need to find a nice view that would allow you to watch me write code okay so where is man I don't have a void set up here how do you like that void setup open I've got it closed what you guys would do is you would just go ahead and open up a bare minimum right but I've got to kind of to get it to sync with my studio I've got to use this particular window alright so let's get in here enough of this nonsense well one thing that you can see is is that we've got the Arduino connected to the servo so we've got to kind of have a pin right so so what would we call it we would call it an integer create an integer variable and servo pin and that's where we have connected where do we connect we connected in nine so we will come over here and say servo pin equal nine okay let's think of some other variables that we want well we're going to want to put the servo in a position so we better have an integer and we will say servo position and I'm going to start that out just at zero okay so at zero degrees and then what else do we need that is probably a pretty good start but ah what we will need to do now is start working with the start working with the servo so if we are going to work with the servo the commands to use the servo are not part of the normal Arduino package because the servo is not used very much and so there is a library that allows us to work with the servo and so what we need to do is we need to load that library in what I like to do is I like to load my libraries at the very at the very start and so to include a library you hit the pound ok and then you say include that's telling Arduino hey go out and load the library then you have to tell it what library inside of these triangular brackets what library do we want servo what's the good news it recognizes that and then dot H and then close it this is one of the kind of unique commands that you don't end with semicolon and so just note I'm not deliberately making an error here in fact you don't put a semicolon on the end of a include you're loading a library you are not actually doing a command alright then our variables are set up now what we have to do is if you think about this in the real world the servo is an object you have to kind of create a virtual object in your Arduino environment so that you can interact with that so I need to create an object in the command is servo okay because that's coming from the servo library so you're using the servo command and now you give your servo a name you can name it whatever you want as far as like normal variable names I'm gonna call mine my servo okay my servo so now I have an object called my servo hope that makes sense so when I want to interact with the servo I'm gonna issue the commands through my servo now let's think what we need to do in the void setup well I always like to start the serial monitor because you never know when it will be useful so serial dot begin 9600 in case you need to get some input or print something out so serial dot begin also in the physical world I have attached my servo to the arduino in the arduino world i need to attach it so how do i do that well if i interact with the object what's my object it's mine servo and then what do i want to do i want to attach and i gotta tell it where it's attached words where is it attached it's attached to servo and okay and remember servo pin is nine so now i have my stuff set up there that is looking pretty good so now I just want to issue a command to the cut to the servo to go to a position and so what is my object called what am i interacting with my servo I want to send it to command so I'm going to say dot right and then I got to tell it where I want to go which is servo position which in this case will be none so I mean zero so what we need to do now is I'm going to take a sip of coffee this is a big one we're gonna download the code I need everyone to hold their breath this is new hardware and new software ok I hope it works boom ok and look at that it went to zero ok it went to zero now you might not see that real well and so I will put this here like this so that you can see it a little better and also I'm going to move it where it is a little bit more in the physical world lined up with where I would like to think 0 is and I'm trying to get this ok that looks good maybe I can visit here a little bit that looks ok we all have to tweak this a little bit but now I've got zero in our dueƱo world hooked up with zero in the physical world so now I'm going to come back to servo position I'm gonna say I want it to go to Nani and I'm gonna redownload the code ok and that's kind of like Nani as we would expect and then what I'm gonna do is I'm gonna go to 180 and then I'm gonna download that and see if it moves over there see this is pretty straightforward isn't it ok now I don't know if you can hear that humming but what you see is is that the are doing the thing did not go all the way to 180 and the other thing it's kind of sitting there it's being over driven this particular server I told you is the kind of cheap ones can't go all the way to 180 you don't want to overdrive it so I'm gonna I'm gonna go to 170 now yours maybe you can go to 180 or maybe yours can go to a lower number but you need to kind of figure out what is that value that's low enough that it's not sitting there and in shaking let me turn that off sorry for that nonsense okay so let's try 170 and see what happens okay it is still kind of shaking so let's try like 165 I'm trying to see how far it can go without overdriving it okay let me try I'm still hearing a little buzz let me try 160 okay I think 165 on mine is pretty good so we go back to 165 okay that looks pretty good now let's go back and alright so you see I'm having to kind of go go go trying things what I'm gonna do down here is I'm going to get the angle from the user okay so to get info from the user from the serial monitor what do I need to do I need to ask for it I need to wait for it and I need to read it so I'll ask for it by saying serial dot print L in and then what am I going to print we're okay what angle or the servo question I asked then what do I do I wait sauce eight while serial dot available equal equals zero so what am i doing as long as there's nothing on the serial monitor I'm just going to sit in wait so I just loop doing absolutely nothing and then what do I do I read so I will do a servo position I'm going to read the data into the variable servo position so I'm going to do a serial dot parse int okay and then that's gonna read it and then I will ride it okay let's see if this will work oh I was sure that would right that would work today I think I put an extra close curly that's what I hate about this new IDE it when I create an open curly it automatically puts the closed curly click closing curly and then I add it to so then I end up with two and that's probably what happened okay this thing is kind of spinning on us so let me see alright so let's see what happens I need to come over here where you guys can see the serial monitor which might be this one which might be I have to find it now let me pop it open like this where is that okay there it is okay what angle for the servo well let's start with zero okay zero that's great now let's go nany okay I need to hold this down let's go forty-five that is really absolutely amazing absolutely amazing okay I have got a homework assignment for you guys alright I have got a homework assignment for you guys and what the homework assignment is is to take your series resistor I'm using a 5k resistor in your photo detector and then build a circuit that will sort of show you what the weather conditions are kind of like thinking is it dark like nighttime is it sunshiny or is it kind of cloudy so what I want this thing to do is I want it to be a visual dial that indicates what the light conditions are now this is homework if you have not already done it you need to go back into lesson 25 and 26 where we really learned how to use the photo detector but if you're taking these things in order you would know how to do that so let me give you a hint you have to use math right you have to turn your lights off and see what kind of value you're getting off of your circuit and then you have to turn the lights all the way on and see what value you're getting and then you have to do the math okay we will come back do it on your own guys please do it on your own and then leave me a note I wish someone someone would leave me a note and said I did the homework I did it on my own and it worked alright so do it on your own make it work and then come back in lesson number 31 and we will do it together okay this is polemic order from top tech boy comm and I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 215,582
Rating: undefined out of 5
Keywords: Arduino, Servo, Project
Id: aFHu65LiFok
Channel Id: undefined
Length: 18min 5sec (1085 seconds)
Published: Tue Oct 01 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.