Arduino Tutorial 31: Using Servo in a Simple Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Palma quarter from top tech boy dot-com and we are here today with lesson number 31 in our legendary new and improved Arduino tutorial series what I need you guys to do is pour yourself a nice big mug of iced coffee no sugar just strong black coffee pour it over us don't use any of that little sweetener packets no vodka out there alcohol just straight coffee over ice caffeine is what fuels the world of engineering I also need you to get out your most excellent eel egg Oh super starter kit if you don't have one check the link in the description $35 off of Amazon has the Arduino in a boatload of components I use this kit in all of the series of lessons and seriously it does make life a little easier if you have exactly the same hardware I have makes it easier to play along at home okay I gave you a homework assignment in lesson number 30 how many of you did it nobody nobody did the homework not one leave a comment down below and tell me that you did your homework but I'm gonna do it along with you and what the assignment was the assignment was to take the servo in the e Lego kit and to get the photo resistor and a 5 K resistor and to build a graphical like display that shows the conditions outside or the conditions around you and so what you want to do you want to have a little dowel and if it's very sunny you want the servo to point towards sunshine cloudy in the night and so this thing moves this thing moves based on how much light is around it see see why you need the photo sensor if you don't know how to use the photo resistor go back to lesson 25 and 26 where I cover it in most gory detail all right so I'm gonna go kind of fast because if you can't keep up you can go back to lesson number 25 or lesson number 26 so let's get going here first I'm going to do is hook up the servo and what we need is if you remember from lesson number 30 that the red wire let me get the red wire the red wire is going to go to five volts on the Arduino so I come up here to the five volt pen okay now I've got five volts now I want ground well the ground if I am not mistaken I always look so that I don't make a mistake ground is brown brown is ground so I'm going to come to the brown wire coming off of the servo the brown wire and that is going to go to my GND which is right there and now my control wire okay so I've got five volts and I've got ground on the red and the brown and now what do I need I need the control one the control one is the orange line okay so I got the control line and then that is going to come over to pin nine I think if I'm not mistaken you need to use a pin with a squiggly for this so make sure that you're using a suitable pin with a squiggly alright now we have the servo hooked up now we're going to build the photo sensor remember for the photo sensor to work it needs to be pointing towards the light I am going to put it between column v let's see I'm gonna put it in column between column five and column ten as such okay that's in there nice and neat now I have my five case series resistor in order to keep a 2 in order to create a voltage divider I will put that from 10 to 15 and notice that the photo resistor and the normal resistor share a common column and that connects them together now what I need is I need to take the top leg of the photo detector which I have in column 5 and I'm going to bring that over to oh my goodness oh my goodness I need five bolts on that and so what I'm going to have to do is since I've only got one 5 volt only one 5 volt connector there I'm going to have to bring the power from the Arduino over to row 2 on the PC board by bringing it to Row 2 on the PC board ok can you see that Row 2 that is going to create a positive real now anything that plugs into that will be connected to 5 volts and so now I will jumper from there jumper from there back over to the servo and so now once again the servo has power ok but the servo has power not directly from the Arduino but the the Arduino is powering up row 2 on the breadboard and now I can get power for the photo detector off of row 2 as well and I will do it like that okay so just to make sure that you see the left leg of the photo detector is connected to this blue wire which then goes to Row 2 so the photo detector should now have power I need to ground the photo detector so I will come over I mean I need to ground the resistor so the right leg of the resistor ok do you see that right leg of the resistor that is in pin 15 I'm going to bring it over to G in D G and D okay you see that G and D on the Arduino alright so I have my photo detector set up now I just need to read from the voltage divider which is this connecting pen okay so do you see that pin that connects the photo detector to the resistor there in a common column in column 10 well I'm also putting this white wire in that column 10 and then I am bringing it over to a four pin a four so I will read the photo detector from 10 a four so first thing we need to do is kind of figure out what is going on with the photo detector so my first coating my first coating will be centered around the photo detector so I need to come over here like this one I believe and get this set up so let's get the photo detector working remember in order to make this thing work we have to do math and yes we will do the math today but before we do the math we need to get some readings off the photo detector so how do we get the photo detector to work well we're going to read a light valve you know how strong the light is we're gonna read the light vowel off of this thing so we're going to need a variable light valve we don't put anything in that because we'll be reading that from this circuit and then we have a an int we have light pin that's the pin that it's connected to and I do believe that we connected light pin to a four so light pin is equal to a for like that is it really going to be that simple I think it might be that simple all right we come down here and we need to do turn on our serial monitor so we will do a serial dot begin baud rate trusty old 9600 now we can use the serial monitor and in our loop it is going to be relatively simple we're going to read that light pin so we are going to do why Val is going to be equal to a let's see how do we want to do this analog read so we're going to analog read from where like pin okay and what did we forget to do that would have ruined everything if I'm going to use a pin I have to do a pin mode pen mode what pin like pin and then what is it it's an input because I am going to read from it now I need to see what I have and so I'm going to do serial dot print L in print belt in and then I'm going to print white vowel like that and now I'm going to delay and I'm going to be good and delay by DT delay time and then I will come up here and create a variable for that in the late time and i'm gonna say equal to let's say 250 that should be good so let's go ahead and run this and see if this thing is going to work everyone please hold your breath oh it looks like it's gonna work I need to come over here where you will be able to see the serial monitor and I need to try to fire open that serial monitor where is that thing okay there it goes okay do you see that so let me turn all the lights on because these are the two values that we really have to have we have to have what is the value when the lights are all the way on and what you can see is let me get some fresh paper here and I'm going to kind of move this when because I'm going to start writing down some very important values okay the very important value that I'm going to write down is is that when the lights are fully on when I'm fully bright so the lights are fully on what am i reading well I was seeing like I think I'm gonna call it about 780 I'm reading a value of 780 so I need to know where the lights are all the way on is 780 and then where the lights are all the way off and I'm gonna have to turn the lights off pardon me starting with your overhead lights you see how I already went down okay and now turning off the studio lights okay sitting here in the dark now we are reading a very nicely dark I'm gonna call it like 150 okay all right and now I'm going to now that I have those two values we have to do our math so I'm gonna come back over here so you can watch guys understand that what you want is when it's dark you want it all the way to the left point yet something like a moon or darkness and then when it's all the way bright you want it coming over here pointing to sunshine and so in order for that to work you have to do your math and so this is what we have found we have found that when the lights are all the in this is going to be linear so we're going to need a line so to do a line we need how many points we need two points okay now the independent variable on the line let's go ahead and kind of draw what we are going to do for a line okay we're going to take the value that we read and we're going to turn it into the value that we write well what is it that we read what variable are we reading we're reading light Val light Val okay that's the number that is coming off the photo detector then what do we want to turn light Val into we want to turn light Val into an angle okay we want to turn light Val into an angle well it looks like if we were going to create a scale here white Val it looks like we go from 0 to 1023 so we would say like this would be 250 500 750 a thousand and then 10:23 would be right about there because this is an analog read in analog reads go from 0 to 1023 so this axis here would be 250 500 750 and 1000 why is that because analog read on the Arduino reads a value between 0 and 10 23 so that's our horizontal axis on angle what can we do with the servo we can go from 0 to 180 but if you remember on my sensor on my particular one it won't go to 180 it'll just go to 160 so if yours will go to 180 use 180 if yours only goes to 160 use 160 but the angle would go from 0 to let's say 160 so I'm gonna call this 100 and call this 200 and then 160 would be right about there all right now in order to make a line we need two points well what do we know we know that the brightest number that we saw okay or the the highest number that we saw when the lights were completely out that highest number that we read was 780 and that is the x-value because that's the value that you read that's the independent variable and so if we're all the way in the dark where do we want the sensor to read we want the sensor to read over here all the way to the left which is going to be 180 but for mine I'm gonna use 160 right because my servo can't go all the way to 180 it can just go to 160 so if I read 780 off the sensor I want to go to an angle of 160 I have how many points I have one point I need another point well what was the other point when the lights were all the way off it was 150 okay I think I did that backwards I hope you guys were screaming at me right that first one that we did was when the lights were all the way on it was 780 and in that case lights all the way on I want to be at an angle of zero and then lights all the way off was 150 and I want that to be at the angle 160 all right does that make sense and let me just explain it you're gonna read a number between 0 and 10 23 you want to change that into a number between 0 and 160 to do that you need two points from those two points you can create your line when the lights are all the way on I was reading 780 when the lights were all the way off I was reading 150 all right now we got to create the equation of the line the first thing that you have to do is calculate the slope M is equal to I guess I should say your dog could be ops that you could say 780 and 160 and then you could say 150 and 0 you could use different points but the performance just has to match the dial that you created so what is M M in math class is y2 minus y1 over x2 minus x1 do you remember that well we're not doing X's and Y's what are we doing we'll use our variables we are using angle 2 minus angle 1 over light fell to - like Val 1 okay now we can put our numbers in so here are two points which one is angle it's the second one okay so I'm going to say it's the second number in the pair of numbers so it's 160 minus 0 ok angle 2 minus angle 1 over what x2 which is 150 x2 minus x1 which is what 780 all right then this comes out to be a negative slope it's 160 over and I never do math in my head on live TV it's 780 minus 150 or 115 I know 780 it is minus 630 and so we can simplify this slope to minus 16 over 63 but I won't try to reduce that anymore we can put that in the Arduino so n is equal to -16 over 63 all right so I have the slope of the line and in fact that can kind of already draw the line but because I know that when I'm at 780 which would be right about here I want to have zero so this is the point 780 zero and then when I'm at 150 which would be right about here I want to be at 160 which would be right there so this is the point 150 160 and then I want to line between there like that okay so it's the point 150 160 it's a point 780 zero and now I have the slope of the line as minus 16 over 63 now what's the point-slope form of a line the point slope form of the line is y minus y1 is equal to M with parentheses X minus x1 so now what do we use will we use light Val so I'm going to have light Val I'll just abbreviate it like that white Val - light Val 1 is equal to M no that's angle angle is y okay sorry angle minus angle 1 is equal to M onto white Val - white bow one white bow - light that one now I can start putting the numbers in angle - angle one well what is angle one this is the first point angle 1 is 0 so that's kind of nice angle - 0 is equal to M what is in it was minus 16 over 63 minus 16 over 63 times it light Val which is still light Val - light val one which is 780 okay zero goes away so I'm left with angle is equal to -16 over 63 times light Val and this is the light Val that you're going to read - I'm just going to leave those I'm not going to try to come up with a number I'll just say it's a - in a - make a + and that's gonna be 16 times 780 over 63 so understand what's happening if I put this equation into Arduino I will measure a light Val which I call LV and then I will multiply it by minus 16 over 63 and then I will add 16 times 780 over 63 and what that will do is that will create the right angle the correct angle so that when it's very bright when it's very bright in the room the servo will point to the sunshine on the dial and when it's very dim in the room it will point to the moon and it will move smoothly in between guys I'm sorry I made a few mistakes in here but you just can't do something like this without understanding the math and so now let's go in and start programming this I will bring this back over here and now we will have to do some coding but I'm gonna keep this handy equation here where you can see it and now we're gonna start coding this thing up we want to really make sure that you see that equation okay now we're gonna start needing some additional variables right we are going to need some additional variables and the first thing that we are going to need to do is we're going to need to make the servo work and if you watch lesson 30 you know that you have to do a and include and you have to include your Sorbo library so that arduino knows how to parse your command so you have come on the open triangular bracket and you are not seeing me so sorry so sorry i will come over here to this one is pretty good okay i believe that one's going to work so you've got to include your servo library which is ser voh that way arduino will understand your servo commands load the library now you need to come down here what is that you need to come down here in your void setup and now you have a nother now you have to do a few more things I'm sorry before you do that up here above the void setup you still need to create your servo object again this is all explained in lesson number 30 so you could say servo my servo now you have a servo object okay what will you need to do you also have a position and what we called that was in our math we called that our angle alright and where are we gonna get angle from we're going to calculate it we're gonna read light Val right we're gonna read light now let me get further out of your way we are going to read light Val and we are going to calculate angle so we need angle do we need any other pins we do need to attach the servo so we've done the pin mode for the for the light pin for our read but we now need to attach the servo so we say my servo okay and then what do I want to do dot attach and then where am i attaching it I'm attaching it to servo in which I've not defined yet so I need to come up here and I need to say I need to have int and then servo pin and where did we connect the servo I have forgotten already we connected it to a four so this is going to be a for one of our analog pins and then if we have another pin we have to do another what we have to do another pin mode okay what is the pin servo pin and what is it it is an input okay oh my light pin I'm surprised this worked earlier that should have been uncomfortable okay why did I make light pin why on earth did I make light pin an input I am all messed up light pin is an input oh my I just had a glitch light coating is an input because we read from its servo pin we're writing to and so it is an output and oh man you guys should have been screaming at me I had a mental glitch there servo pin is where the servo is and that was nine okay better have a little more coffee here okay so now we've done our tip in modes we have our servo pin our servo pin is attached we have red down here from our earlier code we have red light Val well after we read light Val what do we want to do after we read light Val we want to take light Val and we want to turn it into angle based on the mat and so let me come back over here and so I need to now calculate what angle is so we're gonna say angle and let me get this really where you can see it we're going to say angle and I used a little a I believe and goal is equal to what is it - 16 and I am going to be using floating-point numbers here to make sure that we don't round to zero so you need to put - 16 point divided by 63 point okay close my parentheses and then times what times light Val which was the variable that we used up here light val and we just wrote it as lv 2 just kind of abbreviate but the variable we're using is light Val and then what do I do I add the number 16 point times 780 point divided by 63 point okay so that will take the light Val that we just read and it will change it to angle okay so then what we want to do is we have to send that to the servo so now we are going to say my servo got right and what do we want to write we want to right angle so that will then tell the servo to go to the number 8 - the position angle all right let's see if we can get this thing set up and in order to see it a little better I have made and let me show you I have made this slick little dial here so we will get the servo and we will put this little arrow on it like this okay and now we are going to get my most beautiful dial like here and actually it probably goes this way I hope it goes this way okay like that and I need to put something there to try to secure it a little better this thing isn't flapping around I don't want to tape it down because I don't know yet for sure that I'm oriented correctly okay so that looks pretty good okay so like that all right now what I need to do is I need to come back over here and I need to download this Kohl code as we all as we all hold our breath Oh what did I do my serve oh do you see this you guys were not holding your breath were you I did not put the semicolon there I hope you guys were yelling it did that let's try it again okay it downloaded all right and let's see if we can get this up here all right ah you know what I think I think this is going the wrong way okay maybe perhaps we should make it go this way so let's see where dark is okay dark is like that all right we have our orientation right now I think the frustrating thing with this is we are gonna very few things in life cannot be solved with tape so I think we know our orientation so I'm going to try to take this down okay so now let's look at this and I'm going to turn the studio lights back on let's see and just to kind of make sure I'm going to put a print statement in here so I can kind of see what the angle is that or I'm going to look at the oh I already put the light down oh okay there it is okay light Bal is 780 so I think my or patient needed to be that is why I did not want to take it down you see why I was in engineering and not art class okay so now let's put this back on like that okay so that is saying bright sunshiny day okay so now let's start turning the lights off all right so I turned that off oh look it's a little darker oh look it's becoming a cloudy day and then let's come over here and let's go like that what's becoming an even cloudy or day and look at that it comes all the way over and you can see that it is almost nighttime and then here I can get it to go all the way to nighttime so we have nighttime if I get a little bit of light from the monitor you can see it starts coming up if I turn the light on it starts coming to really bright because there's a light just right above it I think if I turn the other lights on okay so this shows a little bit more cloudy full white okay boom so you can see that the math actually worked ok the math actually worked and in fact let me print out angle just so that you can kind of see a little a little better let me take this control copy come down here control V instead of printing out light pal I'll print out angle I didn't do it them angle download it again okay so the thing that you see is is is that man I hope that it came across the that you have with the Arduino you're making readings from one set of devices like sensors and then mapping those read those values onto output devices and so when you read from an analog pin you're going to read between 0 and 10 23 for this case for our particular case we were reading between 1 between 150 and 780 and so what you've got to do is you got to go through the math to map 150 to 780 onto you know like zero to 160 in my case and so you can see that when it's Fulbright I have it at 10 degrees is what it is saying that it wants an angle of 10 degrees and then when I go to the darkness it's creating an angle of about 167 and that's really just pretty much exactly how we wanted it to behave and so it is mapping those readings from the photo sensor and it is mapping them onto the servo now if you're watching this you can see that this thing has been just a little bit glitchy right like the the motion is not real smooth it's a little bit glitchy again that's why if you use one of these better servo she'll get a lot better results I hope you guys made it through the math with me I made a few mistakes as I was going through it but I hope in the end it became clear of what we were doing let me know did anybody do the math before I showed you how to do the math did you guys do the math with me or when you saw that I was gonna do math did you just shut on did you just shut the video off because I know that a lot of people it's like they don't want to do the math and I you know the thing is at the end of the day somebody has to know how to do the math and the fact I know how to do it you know well I'm an old guy I'm not going to be around forever you guys need to learn how to use the math because these projects you really can't you really can't do things like this without without having that without having the math okay guys would really like to hear your comments down below would like to know did you do the project did you do it without watching or did you do it after you watch did you do it like me did you do it differently did you have a better way of doing it I really love to hear from you down below you also think about giving us a thumbs up think about subscribing to the channel pulmicort or from top tech boy comm I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 67,261
Rating: undefined out of 5
Keywords: Arduino, Servo, Algebra, Photoresistor
Id: 4cdFHZ_Z-_Q
Channel Id: undefined
Length: 37min 51sec (2271 seconds)
Published: Tue Oct 08 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.