Arduino Uno R4 WiFi LESSON 23: Control a Servo with a Potentiometer

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcarter with toptechboy dcom and we're here today with episode number 23 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 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 be using the sunfounder elite the elite explor kit 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 on and you can hop on over there and pick a kid up and believe me your life and 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 talk about what I am going to teach you today and what I'm going to do is I'm going to show you my solution to the homework assignment that I gave you in lesson number 22 and that was to connect the potentiometer and the servo up to the Arduino and to control the position of the servo based on the position of the potentiometer so I must ask how many of you were successful if you were successful leave a comment down below I Am Legend double chest bump and if you are not successful leave a comment down below I fold it up like a cheap Walmart lawn chair okay hopefully most of you guys got it because real really I taught you how to do the servo I taught you how to do the potentiometer and you had to just kind of combine those two things what am I trying to do I'm trying to reinforce the things that you learned earlier on I want you to have a toolbox and I want you to practice using those tools as we put them in the toolbox so as lessons go on you're going to get more and more Tools in your toolbox and we want to go back and practice on the ones that we learned earlier now the one thing that will go in with this is is that you are going to have to do a little math but I taught you the math several times how to use a line to solve problems so hopefully you recognized it this time but never fear I'm going to go and I'm going to show you how to do it so let's come over here and let's get this thing hooked up okay so you can see that we have got the servo and I've already got the little arm on the Sero the horn and we remember that this I I Orient Ed it correctly last time if you haven't learned how to use the servo yet go back and watch last week's lesson because I kind of explained it in detail but I've got the servo and then I've got the potentiometer now you notice the left leg of the potentiometer I've already connected to the power rail all of these holes all the way across are connected together in that second to the bottom row and then on the bottom row the ground rail I have connected that to the right pin so the left pin is connected to the power Rail and the right pin of the potentiometer is connected to the ground rail now for that to work I have to make this the power Rail and I have to make this the ground rail so how are we going to do that well let's come over and let's hook up a ground so I'm going to take the gnd d okay do you see that gnd D pin there I'm going to take that and I'm going to run it over to the bottom row and now what happens every hole in this entire bottom row all the way across will give you a ground because I've made it ground now I need to make the second to the bottom row I need to make that five volts so I'm going to come I'm going to come down here to the five volt pin on the Arduino okay the five volt pin on the Arduino that's not five volts that's being in the 5vt pin on the Arduino you see how that says 5V that's the 5volt pin on the Arduino and then I'm going to take that to the second to the bottom row okay so you see the second to the bottom row becomes 5 volts the bottom row becomes ground and so this potentiometer the left leg is hooked to 5 volts and the right leg is hooked to ground very good now what does the center leg needs to go to the center leg is what I would read from so I will come over and I will connect to the center leg I will connect the center leg and I need to make an analog reading from that so I'm going to come over and I'm going to go into analog pin to right there so now I've got that I've got that potentiometer hooked up now I don't like it what I don't like is you see I have it too close to this wire so I'm going to try to move the potentiometer up a little bit and when I'm doing that what I'm going to show you is you see I have well no you can't see okay you see I have that center pin pointed down okay I'm going to leave the center pin pointed down but I'm just going to move it up some okay like that and now I can get I can get my fingers in here to turn it easier so the potentiometer is hooked up now let's get the servo hooked up what did we learn last uh last week in the last lesson we learned that you can control and power these little servos that are in the sunfounder kit from the Arduino if you're going to use a bigger Servo you have to power it from an external power supply you can still control it on the Arduino with the orange wire but You' got to power the red wire with an external power supply 5 volts DC with enough current to drive your particular Servo okay but this one will run off of the Arduino power and so we're going to come in and let's see oh how I wish I had a how I wish I had a red wire but I don't so I'm just going to have to not get to OCD over this and I'm going to have to where the red wire is in the center I'm going to use a green wire okay in the center green wire is hooked a red wire and then that is going to come down to this power rail the positive the second to the bottom now I'm also going to need a what I'm also going to need a ground and what is ground Brown is ground so I'm going to come in with a black wire into brown brown is ground I need to bring that where I need to bring that down here to my ground rail which is the bottom row now what do I have I need to control and so what is that that is the orange wire and so I will use an orange wire there like that and then I will come down to what do I want to control I need a pwm m pen and so that is one of these squiggly digital pins it looks like pin N9 would work nicely so I have the servo control pin connected to pin N9 now as you see there you see the servos start moving because basically the Arduino still had the program it still had the program that I had showed you how to write last week so let's see if we can get this a little bit neater anyway and so now I'll put this here and we have that all set up that looks pretty good I think we have it set up and at least we know that the servo is working because it turned on when I hooked it up with last week's program so let's get that as Pop perfectly straight as we can as perfectly straight As we can can you imagine if I just sent for spent 45 minutes here trying to get it just set next degree of perfection I won't do that to you but I am tempted that big mass of wires is really bothering me there but I'm going to power on through I'm going to power on through I'm not going I'm going to come over here to the code view I'm going to come over here to the code view I'm not going to think about that Rat's Nest of the wires I'm not going to look at it I'm not going to think about it I'm going to think about my coding here I am not I am not thinking about those ugly wires I not thinking about those ugly wires okay so what do we have to do if we're going to use the servo we have to include the servo library and so I'm going to do pound in pound in in in cude pound include and what do I want Servo do Servo Doh now last week I showed you how to install the library if you didn't do that last week you need to go back and look at lesson number 2 too because you do have to install the library before you can load it and I showed you how to do that last week again unusual the include commands about the only one that you don't put a semicolon with so we'll go to the next line now I'm going to need an INT I'm going to need a Servo pin and what was our Servo pin it was nine that's what I wanted it to be but I'm going to look over here and confirm yes it is nine and then I'm going to do an INT I'm going to need a Servo angle right that is going to be the angle that I will put the servo at and just for fun I'll start it at zero like that now I might need to use the serial monitor so I'm going to give myself a b rate BR for B rate is going to be equal 115 to 100 like that now I'm also going to need a read Val okay I'm going to need a read Val or I'll call it pot Val I'm going to get the pot Val that I read from the potentiomet or so int pot Val and on pot Val I really don't need to give it a value because I'm going to read that from the potentiometer now we learned last week if you're going to use the servo you're going to have to create a Servo object in your program and I'm going to call it it's Servo because that is the library that we are calling and then what are we going to do we're going to create an object I'm going to call mine my Servo you could call yours Betty's Servo you can call it whatever you want but it needs to be something that will let you know that you are talking to the servo so it's going to be Servo is equal to my Servo like that so now I have a Servo object I think we're ready to come down to the void setup so we might need to use that serial monitor so we're going to fire it up serial do begin and that will be BR bod rate that we just set up now we're going to want to do the p mode on this I guess I better I better set up another I said Servo pin but I better also make a pot pin and what was pot pin pot pin was A2 A2 like that and I'm going to look a little bit closer yep it is A2 so I've got a pot pin now what I'm going to need to do I am going to need to uh I did that on the wrong one okay let me let me set it up over here int I'm going to do a pot pin and pot pin is going to be equal to A2 right so that's all good now in my uh in my void setup I've done the serial. begin now I need to do my pin mode I'm going to do pin mode on who on pot pin and then what is that that's going to be an input okay now you might think that I would do a pot you I would do a pin mode on the servo pin but no we don't what do we do we attach it so I'm going to go my Servo that's the servo object that I created dot attach and I want to attach my Servo to what the servo pin like that make sure always checking my variables yes that looks good now we're going to come down to the void Loop and what am I going to do well I'm going to read I am going to read my pot Val I'm going to read my pot Val so what is it pot Val is equal to analog read of what pot pin all right now what I'm going to want to do is I'm going to do want to do a a my Servo do write but I don't want to write pot Val because pot Val is a number between 0 and 1023 and the numbers that I can write the servo angle have to go from 0 to 180 so I have a number what do I have a number between 0 and 1023 what do I want what do I need a number between 0er and 80 who is your friend math is your friend so we have to do a little math and this will require us to do the equation of a line so let's let's come over here and let's think about this okay what do we have okay what do we have we have pot Val and that goes between zero to 10 23 that's what we have what do we need we need Servo that was terrible Servo angle and that's going to go from what from 0 to 180 so now we got to figure out a line that will take what we have and turn it into what we need and so let's create a point well let's think if the pot valve reads zero if the pot valve reads zero that means I'm all the way to the left I would want to apply what angle okay if I'm all the way to the left I would want the servo all the way to the left and that would be what that would be an angle of 180° now if my pot Val is 1023 that means I've turned it all the way to the right and if I've turned it all the way to the right that would be an angle of what that would be an angle of zero so if I uh if my pot Val is 10 23 that means it's all the way to the right then what do I want I want the servo all the way to the right and that would be an angle of zero all right so now I've got to come up with an equation of a line that goes through those two points what do you do to start you calculate the slope m is equal to Y2 minus y1 over X2 - X1 like like that okay what's Y2 that's Y2 so Y2 is going to be 0 minus 180 divided X2 which is 10 23 minus X1 which is 0o like that okay now what is this equal to the slope is- 180 over 102 23 now that is the slope what is the equation of the line well it is the point slope form where we have Yus y1 is equal to M onto x - X1 what is y y is y so y - y1 what is y1 180 y - 180 is equal to M which is - 180 over 1023 onto parentheses x minus and what is X1 X1 is zero okay so what do I end up with here I have Yus 180 is equal to - 180 over 1023 time x now what do I need to do I need to move I need to add 180 to both sides so I move the 180 over and I end up with Y by itself so Y is equal to - 180 over 1023 - 180 over 1023 * X all right now let's put it in terms of our real variables in the problem what you have is the x value what you need is the yvalue what did I have I had pot Val what did I need I need Servo angle so what we end up with is SVO angle because that was our Y is equal to -80 over 1023 time x what was our X that's what we have which is pot Val and then what do we do then ah what did I forgot down here I didn't add 180 to both sides okay hopefully you caught that right that 180 when I add it to both sides and it goes away on the left and it comes over to the right and so then I have plus [Music] 180 okay so that is the equation that takes the pot value and turns it into the servo angle that we want to write and so have that in front of you and then what we're going to do is we're going to come back over here we're going to come back over here and understand you're never quite sure how these things work because it depends on how you've plugged them in and so if this thing goes backwards of what I want I'll just switch the uh I'll switch the positive and the ground wires the center one will always be the center ones but I'll just flip them and it'll work and I think we had that happen a time or two so I'm ready to write well what am I going to write I'm going to write Servo angle like that but I got to figure out what Servo angle is and I get it by doing a calculation on pot Val and what did we just see we saw that Servo angle with that beautiful math that we did would be equal to minus 180 divided by 1023 okay times pot Val plus 180 like that okay is everybody happy I put the equation is everyone happy yes yes yes who's does anyone want to say no they're not happy okay if you're in the live stream tell me why you're not happy Okay the reason you're not happy is 18 180 is an integer 1023 is an integer 180 integer divided 1023 integer is going to give you a zero and this whole thing is going to be 0 * pot Val plus 180 it's going to sit there at 180 and I like to make that mistake and then figure it out with you but I'm not going to slow us down this time we 180 point is a floating Point 1023 point is a floating point and now we should get a good answer and so now we're going to run this thing and we kind of hope we kind of hope that it is in the right direction but if it's going the opposite direction then we'll just switch this yellow wire and this brown wire and then it'll work okay let's see here how many mistakes did I make how many mistakes did I make let's go here look at that oh oh I got confident too soon missing Terminator character ah the Terminator character is not the single quote it is the semicolon and those are those are close on the keyboard so that would be a you could understand that mistake okay let's run [Music] it it's downloading and boom there it is okay now I come over here and I move this and nothing happens someone tell me why nothing happens okay well maybe what you would say is maybe you would say well we'll print out Servo angle and let's see if you're actually calculating it right Servo angle like that let's see we will do a serial. print line let's see if we're getting a good Sero or serial.println and then what I'm going to print is Servo angle and let's see if I'm getting a good angle let's see if I'm getting a good angle okay and we will come over here and turn the serial monitor on okay and look at that 126 126 that's a good Angle now let me come back here and look at that it's going down to it's going down to zero and then as I turn turn the potentiometer I'm yeah as I turn the potentiometer it goes from 0 to 180 boom what's the good news I'm calculating the angle what's the bad news it's not moving the servo so let's see my Servo is on pin nine and I come up here I have that set to pin nine the ground let's see here the ground orange or brown is ground I've got that in the ground okay and then red goes to the 5 Vols the 5 volts is active orange goes to pin n it all looks good somebody tell me what is wrong somebody tell me what is wrong I hope you're applying what you learned in the earlier lessons to today's lesson this is a physical object it's a motor can I just sit and pound it a million times a second telling it to move somewhere else no we have to be patient and so what do I need I need an INT and I need a DT for delay time like that okay and that delay time let's say is let's make it 50 okay let's make it 50 milliseconds okay now we're going to come down here and we're going to delay by 50 milliseconds that was really strange how I didn't get that okay but delay no no no no what do we do DT like that okay does that look good now let's run it this time and see what happens looking good looking good okay it says it's done says it's at 180 so let's come over here where you can see it better okay now let's turn this and see what happens okay now it's turning what do we like it's turning turning but what do we not like it's turning opposite when I'm going to the right it's going to the left and when I'm all the way to the left it's all the way to the right now I told you that could happen and what do we do well I'm going to take this orange out to get it out of the way we just that is not good I better put that back okay it was reading a floating number there because that wire wasn't hooked anything but I'm going to to take this out the brown wire out and I'm going to take the yellow wire out and then the same thing happens again and I lost my yellow wire you guys see what happened to my yellow wire oh it came all the way over there so now this time what I'm going to do is the yellow wire is going to go to the positive over on the right so the right the right the right pin is at 5 volts and now the left pin is going to be ground so I come in here and now the left pin is going to be ground okay now let's look at this the left pin comes down to ground the right pin goes to 5 volts and this blue blue wire is on the center pin and the blue wire still comes over here the blue wire still comes over here to A2 okay so it's just these potentiometers I never know how to predict what they're going to do now I am going to put it a little further out of the way like that now when I am all the way to the left the servo is all the way to the left as I start turning to the right look at that it is tracking me it is tracking me when I am straight up and down the servo is straight up and down as I move the potentiometer to the right the servo goes to the right so do you see I'm controlling the servo from the potentiometer now this is guys this is how you would do this is like the first little picture of how you would do robotics I could sit and I could control the position of something connected to the servo with the potential with the position of the potentiometer how cool is that all right how cool is that we got this thing done control a Servo with a potentiometer you see we are at lesson 23 and you guys have learned a lot already you know how to do digital wres you know how to do analog wres you know how to do analog reads you know how to do delays you know how to do if statements with conditionals you know how to do compound condition conditionals you know how to do for Loops you know how to do while loops and now what are the hardware components you know the LEDs you know the red l or you know the LEDs you know the RGB LEDs you know the potentiometer and now you know the servo okay and guys as we move forward in uh as we move forward in these lessons what we're going to be doing is we're going to be learning a little more coding and then we're going to learn be learning a little more hardware and a little more coding and a little more hardware and so as we go through it our projects and our homeworks are going to get more and more and more advanced but you see this is pretty far along I want to congratulate you guys you've got one component of potentiometer controlling another component which is the servo and so that is pretty darn good so guys this time your homework is just show me that you've done the project even though I sort of showed you how to do it go ahead and show me that you've done the project if you haven't already maybe you struggled doing it on your own but now you've seen how to do it so post your homework to YouTube uh in your description in your description of your of your video link back to this video and then down below put a link in the comments over to your homework solution I'll go over and look at it I am still thinking about what I'm going to do next week uh I'm not sure if we'll do a little more software or if we might dig into our kit a little bit more and figure out another another one of the compon components to play with but I'm still kind of figuring that out but uh as always uh I want to I want to you know I want to thank you guys that are with the channel especially want to thank you patreons who are standing with me on patreon you guys that are supporting me on patreon you're the ones that keep these videos coming you're the ones that allow me to stay in the game you can also help me by giving me a thumbs up and leaving a comment down below if you haven't already subscribe to the channel when you do ring that Bell and uh that way you'll make sure that you'll get notifications When Future lessons drop and most importantly most importantly share this video with other people because the world needs more people thinking like an engineer and few are people sitting around watching silly cat videos Paul mcarter with toptechboy tocom I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 1,948
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: R1qE2MCA7Tc
Channel Id: undefined
Length: 30min 27sec (1827 seconds)
Published: Thu Jun 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.