Arduino Tutorial 64: Understanding and Using the Infrared (IR) Remote to Control a Project

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys this is polemic order from top tech boy comm and we are here today with lesson number 64 in our legendary epic series of Arduino tutorials so I will need you to pour yourself a nice large mug of iced coffee I will have to admit on the way in today I dropped my most fabulous mug of coffee that I made at home with beans I roast myself and grind myself and import myself I dropped it and spilled it so today I'm drinking gas station coffee I had to go across the street and buy gas station coffee and it is not pretty okay but I digress let's move on what are we going to do today we are going to learn how to control an Arduino project with an infrared remote so I will need you to get out your most excellent ela goo and Lee GU super starter kit if you don't have one look in the link down below look in the description down below there's a link you can pick up the kit with all the accessories I'm using in this series of tutorials for $35 from Amazon it does help if we are using the identical hardware and particularly in a day like today where we will be using a remote it really helps if we have identical hardware all right I've told you a little bit earlier on that ah you know the super starter kit comes with the Arduino Uno and you can do all of these projects I'm doing with the uno that's included in the starter kit but as our projects are getting more complicated I am moving over to the Arduino Nano you program it just exactly like the Arduino Uno it's just the Nano can be plugged directly into your breadboard so as you're getting more complicated builds and more complicated projects you can keep them neater also on the theme of keeping things neater I'm using these little straight jumper wires instead of these big loopy male-to-male wires you can get both the Nano and you can get these jumper wires in the description down below I've got links and if you don't have a way of affording those right now you can do everything that I'm doing with your you know okay but enough of this nonsense chatter let's go ahead and get started so first thing that you want to do is you want to get out your Arduino hopefully you have the Nano you want to get out the remote control the first time you use the remote control it has a little tab in the back a little plastic tab that you pull out just a little clear tab pull it out and that will activate the battery now the module itself which you can see here is relatively simple it has three wires G on the left is ground let me see if I can move to a different view here G on the left is ground and let's try to get a focus here yeah there you go G on the left is ground R is plus five volts and Y is the control pin I do indeed believe that the control pin needs to go to a squiggly digital so you probably need to keep that in mind that you will need to go to a squiggly and also this is just going to be the start of several lessons that when we get the remote working we're then going to add a project to it so we need to kind of be mindful of our build area so I'm starting with the Arduino I've got it as far over to the edge as possible I'm gonna want to put this nearby now I need a five volts and I need a ground which makes me want to hook it up here but the digital pin on the top is pin 13 which doesn't have a squiggly or PWM so I'm gonna want to build down here and I think the easiest way for me to do it is to establish the ground rail from this ground then bring like let's say pin 11 are one of those straight across and then I will need to establish the ground rail up here on the top row and then jumper that down to the ground rail here because I know that you get bored as I'm telling you this stuff but you've really got to start thinking through your builds so that you don't get yourself in a situation where you're gonna have something really really ugly so let me start by establishing my ground rail and I will do that you see the nice big GND there that is not right and okay that is looking good I'm gonna also unplug during this bill just because if I hook something up wrong I can check it and not just blow something out all right now I am going to need to establish the top row up here as five volts because that's where my 5 volt pin is and that is not right ok there all right you see that's got my 5 volt reel now I'm gonna need to jump or that 5 volts down into here and so I've got a little red wire here somewhere that is already meant for just that job if you will be patient let me get it okay these little straight jumper wires are kind of precious and so they're kind of precious and so I like to try to not bend them and so this one is one that I pre bent for doing the jumper at least I think I have that is in fact not the one it is this one and so I can put this like that and then bring that over like that ok not absolutely perfectly neat by established the 5 volts up there then jumper it down to down here so both of the rails top is 5 volts ground and then 5 volts ground so we have we have nominally the Arduino establishing the rails now need to look at our little sensor and what we see is on the Left we have ground and so I need to connect that to ground and that would be one of these so we'll come in and okay I had my ground pen established I'm trying to get better I am getting better at getting this thing to focus I don't know if you notice that or not the next one over will be to the five volt rail the middle pin will be to the five volt rail okay do you see that little pin to the five volt rail what I'm finding is if I work with the target in the middle of the screen believe it or not the camera it likes to focus in the middle of the screen and so that works a little better now this Y pin needs to go to like pin 11 but I don't think it matters which one it goes to it just needs to go to one of the squiggly so I'm going to just kind of try to find a convenient sized wire and that seems to reach to pin 9 maybe and then I'll look over here to see if 9 has a squiggly 9 indeed has a squiggly so if I connect my control pin to pin 9 as such ok what I'm hoping you guys are starting to see is I'm hoping you're starting to see how to do a really neat build maybe I should get a little further out of your way okay so as we start adding more things do you see how neat that is it's very easy to debug now I'm gonna look ground goes to ground the ground rail is right the middle R goes to 5 volts 5 volts comes from up here comes over here comes down that looks good and then the Y goes to pin 9 I do believe yes pin 9 okay so I check everything because it's much easier to check when you build it then to try to come back and debug it later so now I'm gonna power it up and it looks like I had a little happy light blink on the IR sensor so that is good all right so what this project is going to come down to today is we are gonna try to get that thing turned on where when I press a button I can read it from the module and then I'm gonna create sort of you know kind of have a unique identifier knowing which button was created so let's see if we can get you a code view here I really hope that looks like a pretty good code view but perhaps let's see perhaps I am going to move this back over here and we need to start with the bare minimum which would be getting rid of that I should have had the bare minimum set up before I turn this on but you know a little sluggish today with a little coffee disaster and you know you're just not on your a-game when you are drinking throw shot drinking gas station coffee ok so let's get that like that okay that's a pretty good view where you can see what's going on then we're coming over here and we are getting rid of all of the nonsense okay and we got to be a little careful with our coding here because this remote and if you've ever used like some of the older remotes you notice how like you know they kind of don't operate exactly perfectly so so we got to be a little careful in the way we code this because it's not like the most robust system but if we're a little bit careful we can get this thing to work pretty darn well so what do we need to do what do we need to do well the first thing that we need to do and let me come over here just real quickly give me just a second okay first thing that we are going to need to do is we're gonna need to do the library right we are going to need to do the library so the thing is the Arduino does not come with the library so we will need to go get the library which means you need a nice full screen view like this and what I search on is Arduino remote library so I search on Arduino remote library and then you see my second result is IR remote Arduino libraries and that is at the website wwhen old libraries info slash libraries /i - our remote I - our REM OTE and then we're going to get the latest zip file by the time you view this video it might already be a different one but they should work the same so I'm going to click to download the zip file when I click to download the zip file the zip file if you're on Chrome shows up down here if you are on something else go look in your downloads folder open the zip okay open the zip now you want to put the contents of the zip folder the contents of the zip folder into your libraries folder how do you know where your libraries folder is well you can come over to your Arduino come up to file and then go down to preferences and then it says sketchbook location okay I'm going to take the sketchbook location location I'm going to ctrl copy that I'm now going to come to a file explorer alright I'm going to paste that address into the address bar there you see that control V and if you guys are smart and just know how to do this I'm sorry that I'm just slowing you up but now I've gone to that sketchbook locate that we found from the preferences we have a folder called libraries this is where we put our libraries you do not put the zip folder in here right this is the zip folder you do not put the zip folder in there you put the contents of the zip folder all right so the contents of the zip folder for me you see is IR remote - two point two point three I grab that and drag that over here and of course since I've done this before it's already there but I'll just say replace ok so now I have the library loaded one or I have the library installed very very important I need you to listen to me so I'm going to make myself big I need you listen when you put a new library in you have to kill your Arduino IDE you have to close it and then you have to reopen it because when it opens is when it finds the libraries and if you just drop the library in and then try to start coding with an IDE that's already open it's not going to find the library okay now let's come over here and let's see if we can get you a code view now that is a pretty darn good code view now how do we start talking to this remote well number one we've got to use the library maybe I should get a little bit smaller okay now we need to load the library so the first thing that we do is pound include and then open triangular the libraries call capital a capital our remote capital a capital our remote dot H and then close that up and then what we've got to do is we have to tell it what pin we are using remember that pin and so we got to say int and then I'm just going to put border I'll call it I our pin like that and what did we hook that to do you remember we hooked it to it looks like it looks like you see if I can show it to you it looks like n9 okay so it is ten nine and so I our pen is equal to nine now we have to create the ir object and we do that with the command i our-our ec v ir r e cv and then you give your object a name i'm going to call it ir that's simple right and then where is it connected it's connected to ir pin all right now you could have just come in here and put nine but we're using variables so ir pin is connected to 9 ir received we are creating the object ir where is ir it is connected to ir pin as such okay let's see what else we need to do here we need to tell it where to put the results so that is the command decode results okay and that decode results that's a command that comes from the library it's not an arduino command it's a command associated with that library okay now where do we want to put that so this is when it goes out and reads from this when this module gets a signal where do you want to put that in which variable well I think I am going to call it C and D for command ok I'm going to have a command now we need to we need to what else do we need to do that might be all that we need to do right there now we need to come down and in our void set up a couple of things we need to always turn on our serial monitor serial dot begin in the old trusty 9600 and now we need to enable that module so all right we created the module now it's like we need to enable it or start it and so that is IR dot in naval are in IR and okay so this is kind of tricky this is kind of tricky on the capitalization IR the first one is uppercase because that's how we defined it when we created the object up here so that's IR the command is enable all lowercase enable capital i capital r capital i lowercase in so that is something very very easy to not get right so we do that and then the open closed so now we have started the serial monitor we have started the module at this point okay now what we've got to do is we've got to come down to the void loop and what we're gonna do is we're gonna sit and wait for data so you just want to sit and there's going to be no data till I mash the button so you're gonna sit and wait for data so what I will do is I will say while and then to look for data I'm gonna look at the module ir and then i'm gonna dot decode okay ir dot d code and then what is the variable that i want to bring the data back into well it is at CMD okay you remember how we set it up as CMD up above will that the and I'd sit at but it's and the and CMD what that does is that's getting a value back from the function so I our decode is calling a function and then you're getting that value back by address and so go back and look at my lesson on Arduino functions if you don't understand and want to understand or and just know that when you call this particular function IRD code you need to put the and in front of the variable that you are bringing the value back into okay while that is equal equals zero that st. Paul there's nothing there while there's no data there what do you want to do absolutely not you want to just sit and loop in an empty loop so we're gonna sit there and wait until somebody presses a button then after someone presses a button then what we should have is we should have that value that value should be in CMD dot value okay so let's try that ah yeah okay let's try that so we're going to say serial dot println and then CMD dot value and then print it out as a hex so once we get it then we want to print it out okay and then we want to delay fifteen hundred so one of the problems is if you go too fast when you press the button it's going to read that value like a hundred times and so you want to put a delay in there so that you don't start reading again now after you put the delay and now you got to tell it okay now go back and start reading again and that would be IR dot resume like that okay and so now let's try to download this okay this actually looks relatively happy did that go yeah that went I guess oh okay I thought something wrong I must not have hit it okay oh did you guys see that what did I forget the semicolon okay this time please oh please oh I our dot resume okay serial print line delay command at the end of input let's see I our reason why does it not like I our resume that seems so simple the object is called IR therefore I would I our resume I've got a void oh you know what I don't have I don't have like this matches that but I didn't have an end to the void loop I do believe so let's put the in bracket there so now that bracket matches that bracket please work this time I think it's gonna work okay that works all right so now let me get up my serial monitor here and let's hope that you've got a view of the serial monitor I think okay you see the remote I'm going to point it and I'm going to press a zero and absolutely nothing happens ah okay look at that alright maybe I just didn't press it good let me try again ah look at that okay so what's happening is is that if I press a zero I'm getting a hex value back now why is it CMD value because I told it that the variable was going to be CMD and it returns the value into CMD value okay and then I printed out as hex and if you don't understand hex go back go back my friend and look at the earlier lesson that I did on hexadecimal so let's look let's see if I can give you a view here so I have the remote and I'm going to press zero and when I press zero I get a 6-8-9-7 FF 6-8-9-7 what if I press a one I get something different FF 3 0 C F okay do you see how that's unique do you see how that's unique I'll go back to zero okay and then let's try two right let's try a three let's try it for okay the four like I got a double button press or I press too soon so the four let's try it again okay if you hold the button too long or if you do it too quickly after the last one you can get a flaky result okay so it looks like each one of these that I'm pressing got another little glitchy one there you can't see those all right perhaps I need to pull this down I think I've run off the screen of what you can see let me fix this where you can see this just give me a second here I need to give you a better view or you can see the whole thing okay so I think this will work so now I will press seven and I get F F for to be D I press 8 I get something that press 9 I get something do you see each one of those is unique if I press a power I get a unique one volume okay each one of these gives you a unique value all right now with that with this little bit of code here now you have got a variable and that variable is CMD dot value and now what you can do is you can create a new variable and that new variable up here is going to be the new variable is going to be a string and that is going to be my command okay string and my command so now I'm gonna say my column just because when you have to do a lot of time typing sometimes shorter variables are better so now what you have to do is you have to like try to map all these crazy numbers into something that you could work with so you got to kind of create a mapping and so after your I our resume I could say like let me come in here and let me see like I'm gonna figure out what the zero is going to press a zero here and I'm going to press zero and the zero generated an F F the zero generated an F F 6-8-9-7 so I am then here going to say if C in fact I need to do that before I need to do all this before I start reading again I think probably not I'll do it afterwards okay so I will say if C and D dot value equal equal and then CMD dot value is hex so let's see if it'll do it as a string let's see if it'll do it as a string F you see I'm not you see this is what the little bit confused about it's returning CMD dot value but I'm not sure what CMD dot value is so if CMD dot value is equal to F F 6-8-9-7 let's see if it'll recognize it as a string and what do you want to do you want to Satan cereal cereal belt in cereal belt and why a cereal box you're in and then see what I would do is say you pressed zero like that okay and so let's see if this will work we will download this forgot my trailing quote let's try it again I'll probably want some parentheses around that yeah such I spent the last few days programming in Python so then I get out of Arduino mode okay so let's look at this let's see if it will recognize that as a string so if I press lets try pressing 0 here and nothing is happening what did I mess up okay there it is okay so it did FF 6-8-9-7 but it doesn't recognize it as a hex so command value I'm doing an equal with a string where let's look at that and see if it likes that doesn't recognize it so let's say like 0x see if we can tell it that it's hexadecimal if it would like that okay it looks like it's gonna download this time ok let's try you press 0 ok do you see what happened there alright so let me explain what I did what you have to understand is these things that it's printing out CMD value is hexadecimal it's not a string so I have to tell it the number coming is hexadecimal 0x is just telling Arduino that I am dealing with hexadecimal so it's comparing the variable CMD dot value to the hexadecimal number FF 6-8-9-7 and so that work alright so now let's try that again and we only printed there I need to print the I just need to print out also what it is so I'll say serial dot L in and then what I can do is I can say serial dot print L in and just print command dot value just so I see what it is all right and then if it is this then I press I'll show you where I'm getting it with this because I'm gonna give you guys a homework assignment here pretty soon okay so now let me press zero again okay so it oh and then when I do this print I gotta tell it again that it's hex why is it not printing it up there to begin with I wonder I'm not sure let's try this okay I've got this print so it should have been printing it up there and maybe it was and I was just scroll down yeah it was and I wasn't seeing it so I do not need this in fact it was printing it it's just my serial monitor I'd scroll down where it didn't show so this will look for data when there is data it will print it it'll wait it will then start reading again and then it will print what it had gotten so let's look at this now do that okay that looks good so I'm going to come in and I'm going to press the zero and then it recognizes that and it then tells me that I pressed a zero now if I press a one okay it does this but it doesn't say you pressed a one right because I don't have that if statement in there so now what I can do is I can still print you pressed a zero but what I want to do now is yeah I'm going to do it a little bit differently here and you'll see where I'm going with this so I'm going to say that my command is gonna be a string okay because strings are easier to work with in my mind so my command if command value is zero X F F 6-8-9-7 my command now is equal to and I'll put these things lower lower case zero all right and then I will print my command so you see that I've got a variable my command that I can deal with okay now I've got a variable my command that I can deal with ooh what is that serial print line my command okay put in my semi couldn't call and come back over here okay and so now I will come in with my zero press let's see again zero and then it prints zero okay now one is going to print the code but it's not going to print one because I haven't done that yet so what is needed now is what you need to do is you need to map every key on here to it an intuitive command okay so like the zero through nine I'm just going to write the number in lowercase so like zero1 I'm gonna make it the string Oh in ET wo and then on these make them short like this button I would make PWR so let me do one more for you so I'll come up here I will copy this and then I'm gonna paste it and now what do I need to know I need to know what code the PWR is going to give me so I'm going to press PWR and it's this it's f f/a 225 d so I'm going to copy that and I'm going to come over here and command value if it's equal to that okay which I know is the power then my command becomes not 0 but what PWR alright so now let's download that okay so now if I press zero gotta give it time look if I press zero it tells me it was a zero if I come up here and press power it tells me that it was PWR okay does that make sense alright so now I could do things like if I press zero now now that I know what zero is I could come down here and I could start controlling things all right but now what I want to do is I want you to go and I want you to finish this program for your homework where each one of these buttons like zero through nine you can give zero through nine you can give like lowercase C ero or lowercase o and E string values but this one like that this would be very smart to call it PWR Volume+ would be very good to call like v + v - you know like RR for rewind FF for fast-forward this could be up this could be up this could be down you know down as long just call it like u P or D N and then what I want you to do is when you come in here with this I want you to then not print the codes out anymore and if you press zero all you get is 0 if you press power all you get this PWR and then you've got the basis to where you could go in and you could control whatever you want however you want because you got the basic thing working okay guys I hope you enjoyed this video it was a pretty simple concept but it's going to be very powerful because it's going to allow you to do a lot of different things would love to hear your comments down below come back and leave me a comment hey I did the homework you know if you did the homework put down there I am Legend okay if you get the homework done without watching the next video put the comment down there I am Legend all right Palmer quarter from top tech boy calm if you like these videos give us a thumbs up hey man comment comment comment give me some feedback is anybody even watching these things and then also you guys think about sharing this with other people we've got a really nice long series of tutorials here share it with other people because you can see you go through these things just about anybody can start programming Palma quarter with top tech boy comm I am going to talk to you guys later
Info
Channel: Paul McWhorter
Views: 47,542
Rating: 4.9539523 out of 5
Keywords: IR, Remote, Arduino, Tutorial
Id: Y2HC3bdFoPw
Channel Id: undefined
Length: 37min 53sec (2273 seconds)
Published: Tue Jun 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.