Raspberry Pi Pico W LESSON 77: Control a Servo with an Infrared IR Remote

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 77 in our incredible new tutorial Series where you're learning how to unleash the power of your Rasberry Pi poo W I will need you to 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 Kepler kit for Raspberry Pi Pico W 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 life are going to be a whole lot simpler 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 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 76 but I must start by asking were you able to do the homework if you were able to do the homework leave a comment down below I am cam Legend double chest bump and if you were not able to do the homework leave a comment down below I folded up like a cheap Walmart lawn chair but never fear I'm going to show you my solution to the homework assignment and what was the homework assignment the homework assignment was to control the position of a Servo using this Dandy little infrared remote control in your kit and so that is what I will be showing you today so let me get out of your way have a little sip of the old Go Juice here and then let's switch over and let me start by showing you how I have this thing hooked up now I should start by saying just ignore pretend that this neopixel array is not connected because that's actually the homework assignment that I will give you for next week and so we're not going to be using it in today's lesson but what we do have is we have a Servo and we have the infrared receiver on the infrared receiver when the little LED is pointed towards you the pin on the left is the control pin and we have that hooked up to GPO pin 17 and then the middle leg comes down to the ground Rail and the rightmost leg comes to the power rail let me get a little bit further out of your way you should already have this hooked up but if you don't I will go into it a little bit more it is physical pin 38 that I have coming down to create the ground and then physical pin 36 and let me look at this and make sure that I'm saying this right yes physical pin 36 is providing the power rail okay so I've got control to GPI pin 17 I have the center leg connected to ground and I have the right leg connected to power okay now as far as the servo goes I have the control line which is orange of the servo connected to GPO pin 16 okay and then I have the red wi the red wire power connected to GPI pin 40 and then I have brown is ground and brown is ground and that comes down to the ground rail okay so it's really pretty straightforward to hook this thing up now I must remind you that in using the servo I'm sort of assuming that you're taking the class but if you just jumped into this lesson I uh created a Servo library in lesson number 66 and if you've not created your Servo Library you need to go back to lesson number 66 and get that all hooked up okay so I'm assuming that you have that Servo library in your Raspberry Pi PCO W and then everything else should just sort of naturally follow from what I am uh going to show you in today's lesson okay so that is the way that we hook our circuit up and then I'm going to start coding I'm going to pick up where we left off in lesson number 77 and so let's get the code that we had developed in lesson number 76 and so what we can do is we can come over here to the most excellent www.top techboy do.com using the happy little Search tool here you need to search on something like using infrared IR remote control okay and then you'll come to this page and this is the page where we learned how to practically implement the uh the remote where we could get useful useful functions from it also I'm assuming that you've already installed these libraries which we did a couple of lessons ago the irx libraries and I showed you how to do that a couple of lessons ago so look we're going to grab this code and we are going to come over here and we are going to paste the code now we're just going to run it to make sure that it works correctly and that the universe is in proper working order and that I successfully uh got the code up on the website let's go so let's go ahead and run this we have the handy little remote let me come over here to this view so you can see both things and the way we have this remote set up is We Begin our Command we say that we're going to enter our Command with the power button up here then after pressing the power button we can press a series of buttons like one 2 3 and then to enter the command we press EQ and when we do this look we have this nice array that has our completed command it starts with power right and then we entered 1 two 3 those are our commands and then we ented we ended with the Enter key which I'm saying is the EQ key so using this I can press power and then I can press whatever set of keystrokes I want and then the EQ and then I get that in a nice usable form okay now down here in the while loop what I'm doing in the existing program is I'm just seeing that okay yes I have a new command the command is ready ready to go and then I print it and then I say okay I've used that command so I'm going to reset it to false all right and that sort of gets everything working but now what we want to do is we want to use that new command to move the servo so the way I would do that is I would say press the on press the uh begin key and then let's say I said 135 and then enter what you see I can get is I get the number 135 so what we would need to do in today's lesson is to take those numbers whether it's a single digit 5 or two digits 90 or three digits 135 and then move the servo to that angle well if we're going to do that we're going to have to do a few things and what I think I'll do is I'll come up here and after I have my IR all set up what I'm going to do is I'm going to set set up my Servo so Servo pin was what that was pin 16 we said and then I'm going to create my Servo object my Servo is equal to Servo uppercase the library. servo lowercase the method and then what do I do I do Servo pin that I just created now if I'm going to use the servo Library I need to up here import Servo uppercase and then we can come over here and see that it is there if I say View files all right and then I come down to the Raspberry Pi Pico W it's not showing me anything let me connect to it again okay and now I look in the lib folder and there is my Servo library now if you don't want to go back and watch that video what you could do is you could just create the servo library right here you could create the servo library and then you would save it in the lib folder and then you would be able to kind of follow along with this lesson but I digress let's come back over here okay and so now we have imported I should spell this right doesn't count as an error if you find it before you run it okay import Servo and then I create the servo object let's just run it to make sure we don't have any problems okay it runs so I haven't introduced any errors there now what I need to do is I need to come down and I need to come down let's uh turn this view off I'm going to turn the files view off and then right here what I did in my original one I said okay if command is ready that means that it has seen the power it has seen a series of keystrokes and then it has seen the enter key or the EQ key so at this point I have a command now what am I going to do well I'm going to start by just putting a little time doleep just to give it uh no I don't think I need a Time dos sleep okay but what I do need is I need to create a string okay and let me let me let me run this just so we have something down here okay so what I'm going to do is I'm going to say let's say I do uh let me come back to this view let's say I do power and then I do four five and then I do EQ you can see that I see this is a list or an array the first element is always going to be power the last element is always going to be enter or EQ because that is what is necessary in order to generate this string and then the stuff in the middle the stuff in the middle is going to be the data that I need to create an angle to apply to the servo if that makes sense now I guess what I could do is I could there's several ways to do this but you see I don't know if it's going to have one digit 5° or two digits 45° or three digits 135° something like that so I've got to create a usable number from the data and this is the way I did it there's probably different ways to do it but I think the easiest thing is to create a string and so I'm going to call it my angle string so let's make a string that would be like five or 45 or 135 let's start with that but I've got to start by making it just a blank so I've created this empty string okay and then what I want to do is I want to I'm going to go ahead and continue to print the command I'm going to continue to print the command okay but then I'm going to say for I in New command okay what is the new command this is going to be the new command when I get here for I in New command what will that do it will step through Power four five and enter for this case so I'm going to say if I is not equal to power because power is going to be there but I don't want to use it and I is not equal to what's the other one that I want to ignore the the enter like that okay if it's not one of those two then what do I want to do angle string is equal to angle angle string like that angle string is equal to angle string plus string of I now why do I make I a string because four is a number I want to change it to a string five is a number so I want to change it to a string and this plus is concatenation so what would this do it would start with an empty angle string and then it would concatenate the four and it would con concatenate the five well what if there was no four here well it's going to ignore power it's going to ignore enter an angle string is going to be the string five so now I have a what I have a string representing the angle that I want okay and I think I'll print angle string just to make sure help me debug in case I need to do a little debugging now after it does that and it goes through all of the commands in the new command all of the elements in the new command then what do I want to do I want to say that angle I want to change it back to an INT angle is equal to int of angle string so I turn that angle string which in this case would be the string 4 five it turns that into an integer angle if that makes sense and then let's go ahead and let's print the angle now what you could try to do is you could try to do this like a 4 * 10 plus a 5 Time 1 but that seems to get more confusing because the first digit as you step through this thing the first thing you see is a four and you haven't gone all the way through it so you don't know if that's going to be a four followed by something else or a four by itself if if it was a four by itself it would just be four * 1 if it's a four followed by another digit it would be four time 10 if it was a four followed by two more digit it would be four time 100 and that starts getting a lot more confusing so I think it's easier just to do it I think it's easier just to do it with a uh use do it concatenating the string okay and I think that is the best way to do it now what do I do my Servo was the object that I created P the method of the angle like that okay now after I do all of this I do need to set command ready to false because I got the new command I've have processed it so now I need to set it to false so then this upper part of the program up here this call back knows that uh it's ready to start listening for a new command okay it's ready to start listening for a new command because command ready would be passed up here and then what's it looking for it's looking for to see if it got another Power okay could it really be this easy all right now I just remind you that we do this IR close and we terminate cleanly and you know what I think I will do is let's always reset it so I'm going to say my Servo dop position is going to always go back to zero so I start in that zero position after the program terminate so let's run this thing okay now I'm going to put star and and uh I'm going to put let's contrl C I'm just going to contrl C contrl C and why I wanted to always start with this at the zero position all right now let's run it again and what I'm going to do is I'm going to say power okay four five and equals boom wow almost fell out of my chair on that one okay look at that and so it created the string four the string 45 and then the angle 45 and then it applied it so let's keep going let's say uh let's say power and let's say n90 0 and then the enter key boom 90 let's go power and then let's go 1 3 5 and enter boom 135° let's go power let's go equal let's woo oh what happened there I uh I hit power and enter without giving a command and then it didn't find anything in here and then in line 48 it was defining an angle that didn't exist and so let's see if we can do a little uh let's see if we can do a little error correcting there I would say here just angle is equal to zero just to get started so that I don't have that problem okay so now let's see if I do that I hit power and equal and it still crashes in line 49 angle string oh it doesn't know what angle string is so I'm I'm going to say I'll set this angle string to zero just to get started okay and angle to zero now it shouldn't crash I think if I did that so I'm going to say power and then enter and then nothing happened okay power and enter it doesn't fail now I'm going to go power 4 five enter 45 let's try a single digit power five enter boom it goes back to 5° power 1 8 0 enter and look at that it goes to 180 I can go power 9 0 enter and this little remote works pretty good like I don't have I'm putting it there so you can see it but I can have it back here power 45 and enter and boom you see it's working really really well and now let's do the contrl C and the control C program terminated all right now this was pretty easy because what was the hard thing the hard thing was what we did last week the hard thing was what we did last week which was to gener erate this code that allows you to have a user or programmer friendly way to interact with this remote and you can see the example that we have done with the servo the example that we have done with the servo it's going to be really easy to do anything that you want with this remote now we would probably need to do some other error checking so that if somebody came in here and they entered uh something that was not a number like if we entered one of these uh one of these Keys up here that are strings that we would that we would check with that we would check that and ignore key strokes that were not integers there's other error checking that you could do in here that would be uh that would be really good but how neat is that right I've just got this thing and I can back over here I can set it to something and then I can do that uh let's see ah I terminated the program let's run it again so I can power 45 from way back over here and boom that is pretty darn slick isn't it do you see how powerful this thing is it's powerful because we what we created a very elegant way to interact with it now all we've got to do is we've got to come down here have while true that's our main Loop and we only do things if we have a new command so we always are going to have if command ready is equal to true and then after we execute the command we have to remember to set it to false so that it's not sitting there using a partial command or using a uh using an old command so if you do that then in between there you can control kind of like anything that you can hook up to the Pico how cool is that I just think this is really really neat kind of a short lesson but hopefully I'm hoping most of you guys were able to get it it's time for us to talk about the homework for next week okay now again I am assuming that you've been taking this class and I am assuming that you know how to use the neopixel so what you can see is next week for the homework you won't be using the servo but you will go back and you will hook the neopixel up okay you will go back and you will hook the neopixel up and what your homework assignment is is to now control the neopixel using the remote okay you control the neopixel using the remote so how am I going to do that well I'm going to come in and press power and then what do I want I want neopixel let's say what I've done is I've now just labeled one two 3 and the various number keys to be certain specific functions that the neopixel would do so I press power and then I'm going to press I want neopixel program 3 and then I'm going to press equal ah hold on hold on I was still on the uh Servo program okay so now I'm going to press power and I want neopixel program 3 and then I'm going to press equal and I've got the running Green Man the green is running back back and forth and back and forth that's always kind of like been one of my favorites but let let's not forget uh let's not forget our other favorite which is power program two and then equal and then look at that what do we have we have the rainbow we have the rainbow all of the pixels are running the rainbow now I'm going to do power and I'm going to do one and then I'm going to do equal and now what do I have I have the running rainbow where the each pixel is doing the rainbow but it's doing it 45 degrees apart from each other we did this in an earlier lesson so you should know how to do that also I can do power Z and then equal and then let's see if that's going to take yeah there it is boom that turned it off power three and we've got the Running Red Man and then if I come over here to the main program if I came come over to the main program and hit contrl C to exit cleanly it turns all the pixels off okay so what your assignment is is to go in and control your neopixel using the remote and I'm going to be interested in seeing who can come up I just did three just to kind of show you but I want to see who can come up with the most bodacious the most Bodacious set of neopixel uh programs that are controlled by the remote now do you see in this homework assignment that I gave you we are really getting to something that looks a whole lot more like something that would be a real product right I come in here I turn on my running rainbow ah I terminated the program okay let me run the program again and now I come in and I turn on my running rainbow and boom I've got my running rainbow also one of the things that you have to think a little bit about that running rainbow is a for Loop inside of a for Loop and so it takes a long time for that to run through one for Loop but notice if I hit power and then zero and then come in and press the equal it very quickly terminates it doesn't run through the whole you know 45 seconds that it breaks in the middle and that's a nice feature to put in on your program okay guys this was a pretty quick lesson though but I am just so I'm just so jazzed about this thing to be able to have all the different things that we can control with a Raspberry Pi Pico W and to be able to control those objects through a remote and then you could imagine even like connecting this thing up to your battery and you could have a stand alone own little uh very uh very neat little product based on this remote okay guys I hope that you are having as much fun taking these lessons as I am making them when you do your neopixel homework make sure that you post it to YouTube when you post it to YouTube leave a link in the comments below pointing us over to your homework solution so we can see your homework solutions and then in your homework video in the description link back to this video so people will know what you know what you're talking about or know that it's the the the solution for this specific thing and and then also people can easily find your uh your video so make sure that you do that guys as always I want to give a big shout out to you all who are standing with me on patreon it is your support ENC and encouragement that keeps these great lessons coming also you can help me by giving me a thumbs up and by leaving a comment down below both those things will help me with the old YouTube juice and if you haven't already make sure you subscribe to the channel when you do ring that Bell so that you'll get notifications When Future lessons drop and as always share this video with other people because the world needs more people doing engineering and fewer people sitting around watching silly cat videos Paul mcarter with toptechboy do.com I will talk to you guys later
Info
Channel: Paul McWhorter
Views: 1,589
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: w7rJK9FKs8I
Channel Id: undefined
Length: 27min 28sec (1648 seconds)
Published: Tue Jul 09 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.