Raspberry Pi Pico W LESSON 75: Using an Infrared IR Remote Control in MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is Paul mcquarter with toptechboy do.com and we're here today with episode number 75 in our incredible new tutorial Series where you are unleashing the power of your Raspberry Pi Pico W 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 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 and you can hop on over there and pick your kid 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 teach you is a new hardware component now in the last seven or eight Le lessons we've been working on building our programming skills BR building our software skills we've learned how to do functions we've learned learned how to do classes methods dictionaries we've learned how to build our own libraries we've learned about object-oriented programming and now we're ready to move forward with more advanced Hardware so what will we be doing today you ask well let me get out of your way and let's take a look here and what we're going to be learning is we're going to be learning how to use this Dandy infrared remote that is in your uh sunfounder kit so you will need to open up your kit and you will need to find the remote control which isn't too hard but you will also need to find the infrared receiver and the infrared receiver is in the kit and up on the upper level you will find this little goodie bag that has all types of little things in it and you will need to find the infrared receiver now it has three legs on it and it also sort of see it like has half of an LED now there's also a three-legged component in there that's a little smaller than this but it looks very similar it's a transistor you don't want to use that this one has this little like half of an LED here so you'll need to find that and then we need to go ahead and hook that up and to hook it up we will come over here it's very easy to hook up I will come over to this View and what you can see is you have three leads now when you have the LED when you have that semicircle pointed towards you the semicircle pointed toward you okay do you see the semicircle is pointed towards me when you have that configuration the pen on the left is the signal pen and we take that over to GPI pin 17 okay I'm hooked up to GPI pin 17 and then the center leg goes down to you didn't see that did you okay so the left leg goes to gpop pin 17 the center leg goes down to the ground Rail and the right leg goes to 3.3 volts and you can see here I'm creating the 3.3 volt rail from physical pin it looks like physical pin 36 physical pin 3 six is that right yeah I'm creating power from physical pin 36 and then from physical pin 38 I creating the ground rail okay and then I get this thing powered up so that is pretty easy to hook up so number one you got to get it hooked up and get your stuff ready to go boom we've done that now the next thing that you need to do is you need to find the library because you're going to have to install a library and it's not one that you can do from the program manager so you need to search on sunfounder Kepler kit libraries it is not the first result at least for me on Duck Dugo it's the one over on GitHub so it's the GitHub sunfounder Kepler kit like that you go there okay now the URL for this is github.com sunfounder Kepler kit then you can see there is all this neat material here we are going to come up to code and we are going to download the zip okay when you download the zip that should take the zip file to your download folder so now I will come over here and I will call up a file browser and I come over here and I am in my downloads folder where there is a bunch of stuff that I should probably delete that folder but I'm afraid if I do there might be something important in it but you're now looking for the zip you are looking for the zip it's the folder with the little zip on it and you'll see it is called Kepler kit main. zip you need to right Mouse click on that and you need to say extract all okay and then I'm extracting it all still in downloads okay and then the Kepler kit main so there'll be now an unzipped folder in downloads called Kepler kit main so then we do the extract okay you do that extract and then you look around and you should be able to find it'll take a second or two but you should be able to find then Kepler kit main okay you should find Kepler kit Main and then inside of Kepler kit main is Kepler kit Main again and then inside of that is Libs and then inside of that this is the library you want the folder that is icore RX but if you did that extraction now you should be able to Lo load it directly in th so we're going to come over here and I'm going to view my files okay and I should probably come all the way back up to the top okay so I've come all the way back up to the top and I'm on this computer I need to find downloads okay I need to find downloads and unfortunately this is a little bit of a pain to navigate to so I need to go to disk and then I need to go to users okay and then I am valued customer and then I am going to go to downloads okay and you could put it in a place place that's easier to find if you want but I'm now in downloads and then I come in and I find that Kepler kit Main and then in Kepler kit main I find Kepler kit main I open that okay and then there is Libs so I open that and then there is the magic folder iore RX now we want to upload that to the LI folder not the libs folder the LI folder and the reason we want to upload that to the lib folder is that's where micropython looks for libraries now if you don't have a lib folder you could right Mouse click and you could say new directory and you could create an Li folder but I have one so now I want to doubleclick on that so I'm in the lib folder remember our RGB Library we wrote and our Servo Library are there that's all good and so now I come up to iror x i right Mouse click and I say upload to lib why does it say lib because I'm in the lib folder okay you're probably going to need to pause and go watch that step by step again but unfortunately th doesn't have a very sweet uh navigation system so you got to start all the way from like your CIS and then go to users and then go down like I did but you should be able to find it if you just uh if you just uh watch again what I did so now look boom I have the library file there okay so that's good so now we're ready to come over here and I will turn off the files because what we have we we have what we want I'm going to go ahead and I am going to uh I am going to import time like that and then I'm going to from from machine import do I need to do that I don't think I need to import machine okay so I'm just going to import time and then I'm going to from iore rx. NEC I'm going to import neore 8 take my word for it that's the way you have to do it all right now let's see if that works I'm going to go ahead and run it what no uh from iore rx. NE C NEC import uppercase NEC 8 so take a look at that make sure you do it exactly the way I did it and then run it and boom it imported it without a error and then also in case we need to do some debugging from iror rx. print error we're going to import print error okay and so now let's see if we can load those libraries okay and they loaded okay so now we've got the libraries okay now what do I need to do I need to set up my IR object so my IR pin was what it was pin 17 like that okay now I need to create my IR object my IR you can call this my kitty litter box if you want to but I think for me my IR is more descriptive and we're going to do pin ah if I'm going to do pin I am going to from machine import pin I didn't think I had to do that but my IR is going to be pin and then what it is going to be IR pin and then that is going to be a pin. in like that so we have to set that up all right now what we have to do is we have to start we have to start that IR receiver I'm going to call it IR this is my IR receiver and it is equal to the method neore 8 and now what pin do we send it my IR which I just created above and then also I need to uh tell it what my call back is okay now this is something that we haven't done before this is I load a library and I create an object the IR object this this IR receiver here okay I'm creating this I receiver I'm calling it ir and that Library is going to create that object and then what is it going to want to do it's going to want to call one of my functions okay so it says what function do you want me to call and I'm saying call my call back function now this is going to be a little this is not something we've done before because now their Library wants to call my function so it has to know what am I going to name my function function and I'm going to name my function callback now what does their Library do it sits and watches this remote and when I press a button it grabs that button press and it sends it to callback so I've got to have a function call back that is sitting there waiting to be called by the library I never call call back the library call calls call back so to do the simple thing it's very very simple but then you're going to have to figure out how to make it useful okay so now if we are going to call call back I mean if they are going to call call back we have to Define call back and we have to Define call back before it sees this line of code because if you know python is an interpreter that goes line by line and if it looked at here and there wasn't any call back to find it was error so what I need to do is I need to Define my call back and again you can you can name this function whatever you want to but whatever you name it that's the one you have to tell it to call okay now what uh what in call back well I need to know uh I bit what is IR bit irbit is a parameter that the library sends to call back okay so I've got my I object it's running it sees a button push it does what it calls call back it calls call back and it passes call back a parameter and it sends it to the first location so I can call this whatever I want but the button push that any C8 gets it sends to callback and it sends it to the first parameter now it sends it two other parameters but I can't ever see that those do anything so I'm just going to call that X and Y or you know uh you know nothing comma nothing I have to have those there but it doesn't S seem to send anything back there so what are we interested in we are interested in ibit okay now what can we do well let's just print what it sent us I bit like that so this is sitting there waiting their library is sitting there waiting for a button push when it sees a button push what does it do it calls call back it calls call back and it sends call back I bit okay now it's up to us to decide what call back does but it calls call back it sends it the bit and then you're on your own okay you're on your own so now that is the program now let's go down here we've got to have something just to keep the program alive so I'm going to say try why because we want to use the accept keyboard so we can exit cleanly and then what I want to do while true when it's true true true is always true I've created an infinite Loop but I'm not going to do anything it's just going to sit there and loop to keep the program alive okay and if I do a try I need to do a what accept and an accept what keyboard interrupt okay like that and then what do I do I need to do an ir. close so I clean I kill that neatly and why I because that's the name of the object that I created and then what do I want to do I want to say print program terminated okay like that okay so let's run it and just see if it runs ah what is that nonsense Define call back okay no it's yeah Define call back IR bit nothing nothing that sure looks right argument name re refused okay maybe you actually have to make this something specific addr and Ctrl so those those might have to be named that and like I said if I print these two things out they don't seem to do anything so I'm not sure what they do but I'm going to run it and it says my IR is not defined in line 11 okay my IR sure looks like a ah uppercase R okay like that now let's run it okay so it's just sitting there doing absolutely nothing but let's see if we can exit cleanly with a contrl c okay program terminated which means it did the IR close without getting an error so so this is kind of confusing but let's go over it again first of all where do I have my IR detector connected to pin 17 now I activate pin 17 by calling it my IR an object which is pin IR pin which is a pin in so now my IR is pin 17 and it's an input pin okay very good now I've got to create my I our object which is necor 8 I pass it that input pen and I tell it where to call when it has data then when it has data it calls call back it passes it IR bit and then here are these two things that we absolutely don't understand but we learned they've got to be there exactly this way I have no idea I tried to figure that out but let's just say it's going to remain a mystery but if you do it this way it'll work okay and so now we were able to end the program cleanly now I'm going to come in and I'm going to run it okay it's running now I come over here okay do you see this do you see this I come over here and I press a button and I got an error I press the button and I got an error well I was trying to get a different error I put this in the wrong place but a pro tip you have to take this piece of plastic out okay you have to take that piece of plastic out okay or it's not going to work but I'd put the plastic in wrong but take the plastic out now what is my error here I see it already I see it already again I was being sloppy I was being sloppy when I Nam these things and it is I little B now I can change it to Little B here or I can change it to Big B here either way will work now I'm going to run it okay soft reboot now I need you to watch down here and I am just going to press the power button and if I press the power button I'll get this out of the way but if I press the power button okay boom I got a 69 and a minus one let me press the power button again 69 and minus one let me see if I can press press it quickly 69 69 69 so when I press the power button I end up up here in my call back I end up here in my callback with the number 69 so what do I know the number 69 corresponds to the power button okay now what is this nonsense what is this nonsense if I hold it down I got I get a 69 and a whole bunch of minus ones I get a 69 and a whole bunch of minus ones what is the minus one mean it just means that you're getting a repeated key now if I go quick press really quick press really quick press I get the 269 now what your job is going to be is to go in and make this do something useful you've got the raw material you need to do something useful but you're going to have to figure out how to do this and one thing is you're gonna have to figure out how to do this minus one business because it's not acceptable it is not acceptable to expect the user to just press the buttons really quick okay and I saw somebody on the toggle program had kind of done something like that rather than do the what you know if last time through the button was down and this time through the button is up then you do something they had just looked for a button press and put a specific timing in so it worked as long as you were pressing the button exactly right and so we want to do we want to do things better where whether whether they have a long button press or a quick button press it is always going to work now this is a little bit backwards now because again we're not calling their Library their library is calling us and we've got to figure out how to go in and make this do something useful so you see you're going to have to figure out how to deal with a long button press and then you're going to have to figure out how to make this useful because down in this program down in this program I've got to do something and while I'm doing something in this program at any time IR bit might have a number now you could imagine doing something really simple like if I pressed one have the LED uh turn red and if I press two have the LED turn green and if I press three have the LED turn blue and then if you pressed it real quickly then you could put something in there it would change the color of the LED but we've really got to have something more useful than that because we might want to put in like an angle for the servo so we might want to put in 135 well how would you process 135 I get a I get a one and a bunch of you I get the number corresponding to a one and a whole bunch of nonsense and I get a number two and a whole bunch of nonsense and a number three a whole bunch of nonsense okay so you see that's kind of that's kind of confusing so I've got to create a useful command well one thing that I'm going to need to do is I'm going to have to map these key things that give me arbitrary integers map them on to something useful so like if I come here I can press that that's a 69 then I get a 70 and I get a 71 then I get a 68 a 64 4 a 67 and then I get a 7 a 21 a 9 now they're always the same but I've got to somehow map these irbit numbers to something that makes more sense I've got to map those onto something that makes more sense also since my commands are getting something useful done it's not always going to be a clear single keystroke so I need to do something like if I hit power that tells the system that I'm going to enter a command and then I type in whatever keystrokes I want that that command or value is going to be and then I'm going to hit the EQ and that's like enter so if I want to send a command I want to press the red button saying okay I'm entering my command and then I'm going to put in my keystrokes and then I'm going to click EQ and then I'm going to capture that sequence of keystrokes but I don't want 69 9372 those arbitrary things I don't want those arbitrary things I want it to map to something like if this is pressed I want it to map to power or mode or the num the integer one so if I press one I don't want a 12 I want it to be the number one okay does that make sense now let me show you let me actually show you what your homework assignment is so I've shown you right we've got the hardware working right but now you're going to have to make it do something useful so I'm going to come over here and then I am going to come over here okay and then this is what I want you to do I want you to make this where it actually does something useful so I'm going to come over here like this and I'm not showing you my code but I'm am going to run this code okay now watch this I'm going to hit power and then I'm going to enter what my command would be one 2 three and then I'm going to hit the EQ saying my command has ended and then what do I end up with I end up with this array I end up with this array which starts out with the power key was entered indicating you've got a command coming and then the number one the number two and the number three were entered and then enter was entered indicating that that's the end of the command well I could do a different command I could go power okay and then four 5 6 and then EQ power 456 now what this I can do power and I forgot what I name these here but I can do the play button the rewind button and the forward button and then hit equal and it's power it's power play back I didn't get that last one let me try again okay so I do power I do play I do back and I do this ah I got an error what happened okay so I'm going to do power I'm going to do play I'm going to do rewind okay I've got an error in my code somewhere okay so probably I didn't enter things right but let me try it again where I don't hit that key because that one key seems to not like it so I'm going to do power and then I'm going to do one two three and then I'm going to do that power 1 2 3 now I'm going to do power four five six and then I'm going to do EQ power four five six I'm going to do power I'm going to do s 8 n and then I'm going to do enter power 7 89 enter now I'm going to do power and I'm going to do mode and the speaker off on the top top and then enter and I get power mode off enter so I just have an error in my program if I do power and then I do play and then I do rewind it doesn't like my rewind okay so I've got an error there but let's see if I do power and I do play and forward and then equal it does power play uh and back yeah so so that uh that is working so just in my definition program I got a little I've got a little problem but you can see that I've got this thing basically working here I've got this thing basically working now that I have this array down in my main program I could do things based on this nice command array I had so imagine you don't have to do this for homework but I could do something like I could do something like power 1 3 5 and equal and then what I could do is I could take the 135 and I could turn that into 135° and then I could move the servo to 135° or I could put in a color I could put in a value for a color I could put in all types of stuff because I've captured start the command the key strokes and end the command and since I've have that an array now I could make this little sucker do all types of interesting things so hopefully that makes sense now also notice that some of the key strokes I made integers and some of the keystrokes I made strings okay so the first thing you're going to have to do is set things up where you map those arbitrary numbers you map those arbitrary numbers coming off the remote into something that is more meaningful man I hope I did not confuse you guys I hope I didn't confuse you guys but that is your homework assignment your homework assignment is to get things to do what I just showed here but not have the error on that uh on that one key now this is the thing that's going to be a little bit maybe confusing to you I've taught you classes I've taught you methods I've taught you functions I've taught you dictionaries I've taught you uh threading I've taught you dual core I've taught you how to make libraries but but this is the thing and I noticed this when I was teaching students in the classroom typically you teach something and then you test you teach you test you teach you test so I teach you how to do linear equations what's the homework linear equations I teach you a quadratic what's the homework quadratics I teach you uh I teach you how to factor the homework or the test is on factoring so it's like whatever I taught you whatever problem I give you it requires you to do what I just taught you but now what I've done is I have given you a tool boox and I've put a bunch of tools in it now I've given you a problem what is the hardest thing the hardest thing is to figure out what tool you need to get out of the toolbox what I used to do to students kind of mean but what I used to do to them I would teach them linear equations quadratic factoring uh you know then you know teach them pols or something and then the homework wouldn't have to do what I taught them that day but something that was three days ago and they would sit there and they would just try to do it with the tool that I gave them that day instead of the tool that I gave them three days ago okay so part of the challenge do you hear that monkey alarm is going off I don't know if you can hear the alarm in the background but that means I've got a monkey that means I've got a monkey monkey in the uh Garden here okay don't see a monkey but maybe he did a quick grab and dash they come in and they get my corn alarm goes off and then they run off with my k with my corn don't know if you heard the alarm or not but that's what it was monkey alarm Okay so one of the largest challenges of this homework it's really pretty easy to do but one of the largest challenges that you're going to have is of the tools I taught you what tool are you going to need in order to do this particular homework assignment maybe it's classes maybe it's libraries maybe it's dictionaries maybe it's functions who knows what it is but of the tools you have you need to apply them to solve the homework assignment okay guys I hope you're having as much fun taking this class as I am making it remember when you make your homework assignment upload it upload a video to YouTube Don't upload it as a short and make sure the video is like at least a minute long and it won't automatically make it a short and then in your video description link to this lesson in the comments down below link to your homework solution and I look at every single homework solution that you guys post really like to see those because I see if you're understanding what I'm trying to teach you or not okay as always I want to give a shout out to you guys who are standing with me on patreon you patreon supporters are the ones that keep this great content coming thanks a lot you can also help me by giving me a thumbs up if you uh you can also help by leaving a comment down below and if you haven't already subscribed to the channel when you do be sure to ring that Bell so you'll get notifications When Future lessons drop and most importantly 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 [Music]
Info
Channel: Paul McWhorter
Views: 1,871
Rating: undefined out of 5
Keywords: STEM, LiveStream, TopTechBoy
Id: ddceKBDMKsE
Channel Id: undefined
Length: 35min 7sec (2107 seconds)
Published: Tue Jun 25 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.