Using IR Remote Controls with the Arduino

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today in the workshop will be working with infrared remotes we'll learn how these devices work and how we can decode them with our Arduino we'll also learn how we can use an Arduino to make our own custom infrared remotes and how to use these devices in our projects so don't change channels and welcome to the workshop [Music] hey welcome to the workshop today we're going to be working with infrared remote controls those devices that I'm sure you have all over your home now infrared remotes are very useful as evident because they come with just about every piece of audio and video equipment that you'll ever purchase but there is a lot that we can do with an infrared remote especially when we couple it with an Arduino so we're going to cover quite a few things today first we're going to start by explaining how infrared remotes actually work because I think it's always important to know how something works before we started playing with it next we're going to see what we can do with some of these remote controls that we have around the home we're going to use an Arduino to decode the output of these and we'll see if we can repurpose them for example I've got a remote here for a television that has features for a DVD or blu-ray player that I never use I could repurpose some of these buttons to let's say turn off lights or fans or something in the same room as the television to make the device more useful we're going to look at doing things the other way to taking an Arduino and emulating an infrared remote so that we can make our own custom remotes maybe one remote control for all the devices in our room or perhaps a simpler remote one that only has the buttons that we really use rather than 20 or 30 buttons that we never use and is kind of confusing and then we're going to look at using one of these inexpensive infrared remotes that you can pick up on eBay these things are really cheap and they can be used for all kinds of projects so we're going to put together in our dueƱo project that uses this so there is a lot to cover today and so let's get started and learn how our remotes actually work ir remote controls use light to transmit data between the remote control and the receiving source the light they use is infrared light infrared light is light below the visible spectrum of light an advantage of IR remote controls is that IR LEDs are inexpensive a disadvantage is that IR is subject to interference common sources of interference to IR remote controls include sunlight another common source is the lighting in your home although today's modern LED lights admit far less IR than old-fashioned incandescent lights did now IR remote controls work by sending pulses of infrared light to the receiving source to prevent interference these pulses are modulated usually at a frequency of 38 kilohertz each button on your IR remote control sends a unique code to the receiving source these codes differ between manufacturers now one unfortunate fact of life is that when you're building a project sometimes it doesn't work the way you want it to Murphy's lodges sometimes works against you and you have to start troubleshooting now when you're troubleshooting a project that's based upon an infrared remote you've got an additional problem you can't actually see the light with your eye but fortunately there is a way that you can see infrared light and that's to use a camera that does not have an infrared filter you probably have one and you may not even realize it the camera in your phone or in your tablet is usually capable of seeing infrared light so I want to show you that right now I've got a tablet over here I've got the camera on and I've got one of my infrared remotes and so I'm gonna get up close and personal for a second here and flash and aim the remote at the tablet and as you can see you see a flashing violet light that's actually the infrared LED being pulsed as I'm pressing the button so you can use that technique if you need to to determine that your infrared remote is indeed transmitting and if things aren't working you can start looking at the receiving side instead alright so now that we've seen how IR remotes function let's take a look at how one of the sensors actually works now I'm going to be using a very common three pin sensor it's based around the device called an 1838 IR sensor and the module I'm using is hooked up very simply it's got a VCC a ground and a signal connection so the VCC is connected to five volts the ground is connected to ground of course and the signal output is what we connect in to our Arduino or other microcontroller so I've got one of these devices here on the breadboard and I'm just powering it up with my five volt power supply I just want to show you how this works now I've got a logic probe we use the logic probe in a previous video and this basically is just a device two consents whether a signal is at a digital zero or a digital one and it does so by both flashing a light producing a rather irritating sound and so when I connect the output of my Center to my logic probe we see that it is that a digital high right now indicated by the red light and the high-pitched sound so I'm going to fire a remote control of this and as you can see it creates a number of pulses every time so this device is actually D modulating the modulated signal and producing pulses which will then decode and determine which key on the remote that I've pressed so now that we've done this let's take a look at our first Arduino circuit that we're going to use with this device now the first demonstration we're going to do with nerd we know is very simple here's how I've connected the sensor up to the Arduino I've taken the 5 volts from the Arduino and connected it to the VCC on the sensor I've connected the ground on the Arduino to the ground pin on the sensor I've taken the sensors output and connected it to digital pin 4 on the Arduino now the digital pin is not critical you could use another one if you wish and you'll just have to change the sketch accordingly so now that I've done that let's take a look at the sketch we're going to be running the sketch makes use of an IR remote library which is written by a fellow named Ken sheriff and you can find that in your library manager so go into your library manager go include library manage libraries and look for IR remote now there's this robot IR remote which comes installed with the Arduino this is not the one we're looking for continue to scroll down until you see I our remote by Sheriff if yours isn't installed click the button after you click more info and install the latest version of the library now that you have the library installed let's go through the sketch first of all of course we include the library next we'll define the pin that we've connected the sensor to I've called it received pin and I've defined a constant for it and it sets number 4 because I've used pin 4 again if you use a different pin just change the 4 accordingly then we need to setup an object for both the results and the receiver itself so first of all we set up an object called IR receive and we set it to the receive pin that we've defined earlier and then we set something simply called results and this will store the results of our IR receiving then we go into our setup routine now we're going to use the serial monitor to display the codes that come from the IR remote so we'll start off our serial monitor at 9600 baud you can use a different speed if you wish and then we'll simply enable the receiver by taking a receiver object and using the enable IR in command in order to enable that then we go into the loop and the loop is very simple if we actually decode and get results in the result object then we'll this simply print the result and we're going to print it in hexadecimal because this is the customary way of printing the results and then after that we will resume listening again and this continues on and on the loop so now we'll upload back to our Arduino and then we'll open our serial monitor to take a look at the results so I'm going to use a remote control right now and just aim it at the sensor and see what kind of results we get and as you see we get a number of numbers whenever I press buttons they'll probably notice a number of them say F F F F F F and actually what that is is that's the repeat signal that most remote controls give so when you hold the key down for a period of time it simply sends that out to indicate that you're repeating the key that you held before and we'll take a look at how to use that a little bit later on now as I mentioned earlier different manufacturers use different sets of code to their remote controls and some manufacturers share the same sets of codes between different remotes and so it's very helpful sometimes to figure out what set of control codes your remote is using this can explain sometimes why two remote controls will interfere with each other when they're used in the same room so the next sketch we're going to look at will display the manufacturer of the codes that your remote is using so let's take a look at it right now this uses the exact same hookup as our previous sketch so there's no change to the Arduino hookup at all now in this particular sketch we start off exactly the same as the last one will include the IR remote library of game we've got our sensor hooked up to pin number four we'll define objects for both the receiver and the results and we go into setup and we do the same thing we're going to use our serial monitor and we're going to enable our IR receiver and then we go into our loop once again if we decode some results we will print the value out in hex but then after that we're going to also use the decode type property of the results object and we go into a number of different case statements so if it's an EC we print out NEC if it's a sony we print out sony RC v RC v etc etc etc and that's basically the entire loop once again we resume after doing that and then we go back through the loop again and continue on and on and on so let's upload back to our Arduino and take a look at it so now that it's uploaded let's open up our search monitor and try this out with a few remotes okay I'm going to start off with this RCA remote that I've used before this is her a television that I have and let's just aim that at it and as you can see it's using an NEC command set now let's take a look at this little remote control that we're going to use a little bit later this is the one that comes with that little kit that you can get on eBay or Amazon and we find out it's also using an NEC control set I've actually found the NEC set is the most popular among the remotes I have now here's a Sony remote control and as one might expect it's using a Sony command set and I've got a Panasonic over here so lo and behold it's using a Panasonic set I also get a few unknowns on this particular one and that's because this is a multi remote and it has some codes that if this doesn't seem to be in the common library so you'll find that as well you'll also find that some remotes for example my Sony over here I think sometimes gives me yeah sometimes give me some sanyo remote control codes on it so it's interesting how these get mixed up on it's on the same remote but at any rate that will display the manufacturer type so let's go on and do something a little practical with our remote control you saw earlier that I had this RCA remote control and it's for a television I have now the thing about this remote is that I only own the television yet the remote has a number of extra buttons on it that are for a DVD or a blu-ray player that I don't actually own and these buttons are totally useless to me so what we're going to do in our next sketch is we're going to repurpose these buttons so that I can do something else with them so the two buttons I've decided to repurpose on my remote control are this red button at the bottom here and the yellow button because again they're for functions that my television doesn't actually use and so in order to do so I've added a few extra components to my Arduino circuit specifically or a telly and a yellow LED along with a couple of drop-in resistors now here's how I've hooked this up I've taken pins eight of the Arduino and hooked it up to a 330 ohm dropping resistor and then hooked the other end of that resistor to the anode of the red LED that I've taken pin seven hooked it up to a 330 ohm resistor and taken the other end of that 330 ohm resistor and hooked it up to the anode of the yellow LED then I've tied the two cathodes together and hooked them up to the ground line on the Arduino so now that we've seen how this is wired up let's take a look at the sketch I'm going to be using in order to accomplish this now the sketch starts off in a similar fashion as the previous one we include the IR remote library by Ken Sherriff and we define the receive pin as number 4 but now I'm defining a couple of other constants as well specifically red pin which is number eight and that's for the red LED and yellow pin which is pin seven for the yellow LED now again if you wish to use different pins just change the eight and a seven accordingly i've also defined another integer called toggle state because i want one of my leds to be a push on push off and so the toggle state is going to remember the last state I'm going to initialize it with a value of 0 which means the LED is off then I'm going to define the IR receiver and results object exactly as we have in the previous sketches then we go into the setup I'm going to enable my IR receiver and I'm not using a serial monitor this time so there's no commands for the serial monitor and then I set the LEDs as outputs using the Arduino pin mode command so I set both the red pin and yellow pin is being outputs now let's go into the loop once again we take a look and see if we've actually decoded some results from our IR remote control and we're going to use a switch encase function over here so what I've done is I've captured what the value of both the red and the yellow keypad are using the first sketch that we tried so I've got this value for my red keypad and this value for my yellow key button now of course if you're doing this you're going to have to change these values to match the ones on the remote control that you're using so under LED I'm going to have it turn on for two seconds and then turn off every time the button is pressed so what I do is I do a digital right to the red pin and they send it hi delay for two seconds and then I send it low again and then I need a break statement because that ends this case statement over here now in this case over here with the yellow button is pressed I want the toggle this either on or off so in this case I'm going to do things a bit differently I've got an if statement over here so I examine the toggle state if the toggle state is 0 that means the LED was off so I want to turn it on so I'm going to turn the LED on and then I'm going to change my toggle state to be a 1 if that's not the case I'm going to do the opposite thing I'm going to turn the LED off and change my toggle state to be a 0 and again a break and this particular case statement then once again we resume listening and just go through the loop so let's take a look at how that actually works so over here I've got my remote control as you can see with the red and the yellow buttons and I've got my LED so if I hit the red you see the LED comes on and then 2 seconds later it goes off so I can just continue to toggle that briefly on and then it comes off whereas the yellow one as a toggle so if I press this the LED goes on and it stays on and it'll stay on until I press the yellow button again and I turn it off and as you can see that functions quite well now our last demonstration Illustrated how I repurposed two of the buttons the red and the yellow one at the bottom of my remote control and made the control a couple of LEDs and that's great for a demonstration but obviously for something practical you would probably want to control other devices such as a lamp or a fan in your room that would actually be a proper use for something like this now if you're going to be doing that that means you're going to have to be controlling line voltage and in order to control line voltage the best way of doing that is with the relay I've got a couple of relay modules over here that I like using this one has two relays on it and this one has eight relays on it what I like about these modules is that they're already pre-wired so you don't have to actually do any wiring directly to the relays and they've got opto isolator x' on them so that they isolate the are high voltages on the relay from the Arduino so even if something were to go wrong with the relay you still wouldn't pass any high voltages to the Arduino now after using these modules you should power them with their own 5 volt power supply not the one in the Arduino for one the Arduino probably doesn't have enough current in order to drive a couple of relays but secondly that's an additional form of safety by isolating the power supplies between the two devices and on the subject of safety if you are going to be experimenting with high voltage devices be very very careful never experiment with high voltage on the workbench always use things like LEDs like we used earlier and then when you wire your circuit verify it with a multimeter in that but don't actually use the high voltage is on the workbench make sure that you wired into an insulated enclosure make sure the wiring for the high voltage is 18 gauge or 16 gauge don't use anything thinner than that and be very very careful remember that depending where in the world you are the voltage you're dealing with could be anywhere from a hundred and ten to two hundred and forty volts and that can be lethal if you are not sure of what you are doing if you've never worked with that kind of voltage just don't do it find someone else who is qualified and capable of doing it to assist you or just simply do not do it do not risk anything because again these voltages can be lethal so with the safety message over let's move on to our next experiment so so far we have seen how we can use the IR remote library to receive and decode remote control codes to determine the manufacturer type of the remote control codes and to even repurpose a remote control to perform other functions but the IR remote library can also be used to transmit remote control codes and that's what we're going to do right now I've taken this little RCA television and put it on my workbench now it's the TV that's associated with this remote control that we played with earlier and basically what I've done is I've emulated the power button with the next demo we're going to do so the power button toggles the television on and off now you could emulate any other button that you wanted to I just chose the power button for this demo you could even emulate a series of buttons and they don't have to be to the same device so there's a real practical use for this let's say you have a television and a sound system and a blu-ray player and you need to turn on the TV set it to HDMI input number two let's turn on the sound system set its input up turn on the blu-ray player that's a whole bunch of buttons you'd have to press well you could make one button that does all of them and just fires off the remote control code so it can be very convenient now in order to do that you'll have to do what I did in order to emulate the remote control here and you'll need to determine the code and the manufacturer type which we saw with the second demo that we did another thing you need to know though is the different manufacturer types transmit a different number of bits with each stream and you need to know that now that's a little bit difficult to find that information I've got some resources on the drone bot workshop comm website and the article that's associated with this video that can help you determine the number of bits for your remote control but they vary between different manufacturers essentially the NEC remote control codes that are used in this RCA and that are used in this little keep one that we got from ebay they use 32 bits the RC 5 which is another very common remote control set uses 12 bits now the Sony remote that I used earlier can use either 12 or 20 bits depending on the type of Sony remote so it gets even confusing between manufacturers RC 6 is another type that uses 20 bits and the Sounion arts six protocols require that you send the code three times so you have to go through a loop and repeat it three times you may have noticed when I demoed with the Sony remote that every time I press the button they actually got three codes that's because the protocol requires that again you'll find a resource in the drone bot workshop comm website to help you determine the number of bits now in order to do this I've wired up a little Arduino circuit and it's wired as follows I've got an Arduino Uno an infrared LED which is like a regular LED except of course it transmits in in the infrared light zone a push-button a 10k resistor and a 150 ohm resistor that have wired it as follows I've taken pin three of the Arduino and connected it to one end of the 150 ohm resistor and I've taken the other end of the 150 ohm resistor and connected it to the anode of the LED and then I've connected the cathode of the LED to the ground of the Arduino now you need to use pin three this is one of these instances when you can't change the pin number and that's because the IR remote uses pin three because it's associated with one of the timers inside the Arduino so this can't be changed next I've taken the five volts in the Arduino and tied it to one end of the push-button I've taken the ground from the Arduino and hooked at the one end of the 10k resistor I've taken the other end of the 10k resistor and hooked it to the other side of the push-button and then I've taken a line from that connection and hooked it up the pin seven of the Arduino now you could use a different pin other than pin seven and you could just change the sketch accordingly so now that we've seen how all of this is wired up let's take a look at the sketch we'll be using in order to emulate our remote control so here's the sketch for our transmitter we start off by including the IR remote library just as we have for our previous sketches and then we define a pin that we'll be using for our switch so I call switch pin equal to seven because we've got our switch connected to pin seven of course if you used a different pen you just change the number seven now notice we are not defining a pin for the output because the put has been fixed on pin three and that's part of the IR remote library the next thing we define is an integer that I called button state and I set it to a value of zero this represents the value of the push-button zero being not being pushed down and one being pushed down then it creates an object called IR send so I create an IR send object and then we go into our setup and the setup is quite simple I just set the pin mode of our switch pin to be an input this is the input from our push-button then after that we go into the loop first of all we read the value from the push-button so we do a digital read on the switch pin and assign that value to the button state now if the button state is high this means that the button has been pushed and so we use our IR send object and we use to send NEC function in order to send the code to the television now the send NEC function works as follows and will be a send Sony function ascends rc6 function etc etc first of all we're using a hexadecimal number so 0x just means that the following code is hexadecimal now this is the code that I captured for my power button so Fe a 857 of course when you do this experiment with a different television and different remotes you'll need to change this value then as you recall the NEC codes require a 32 bits so that's what the second parameter is is 32 bits and this is the power code for my television and then after that I just add a small delay this is to debounce the push-button so that when I push it I don't get a series of on-off on-off but it lets the button have a time to settle down so that's basically all that the code is so now let's take a look at it and see how it functions ok so let's demonstrate the little circuit that we've wired up to emulate our remote control on-off switch going to go over and press my push button and lo and behold my television turns itself on now I don't have any input connect to it so that's all we're going to see so since it's not that interesting let's press the button again and the TV turns off so as you can see this works very well one thing to note is that the infrared LED I bent it so that it was aiding at the television remember that the light from the LED like in any LED comes out the top and not the side so if you're having trouble with your circuit you might want to look at that also when you're troubleshooting of course you can use your telephone or your tablet to make sure the infrared LED is transmitting you should see it flashed when you push the button and another thing you could do if you don't want to use your phone is this temporarily substitute a red LED in its place and you should see it briefly flash when the button is pressed now for our final demonstration I want to show you how you code for the repeat function and their remote controls you might recall from the first demo that whenever I held a key down I got a series of F's and that just indicates repeat the last key that was pressed so in order to demo this I've got a little demonstration up tup with a remote control and a servo motor now the remote that I'm going to use is this little one that I got off of ebay it's very inexpensive and it comes complete with not only the remote control but also with an IR receiver module and with an infrared LED so it's a very very inexpensive and useful way of getting into remotes and adding them to your project I'm going to look at doing three of the keys on this remote the left one the okay one and the right key and what I'm going to do is that we're going to hook it up so that it drives a servo motor when I hit the left key the servo motor will move a couple of degrees to the left when I hit the right it'll move a couple of degrees to the right want to hit the okay key in the center the servo motor will Center itself and if I hold down the left or right keys the servo motor will continuously rotate in that direction until it comes to the end of its travel so let's take a look at how I'm going to hook this up and then at the sketch that we're going to use in order to accomplish this now for this demonstration we're going to require Arduino Uno an IR sensor module a small servo motor like an SG 90 and a separate 5 volt power supply for the servo motor we're going to hook it up as follows the IR sensor will be hooked up identically to the way we did in the previous experiments with VCC connected to the arduino 5 volt outs ground connected to the arduino ground and the output of the sensor connected to pin 4 of the arduino next we will hook our servo motor up we'll connect the positive to the servo motors 5 volt power supply positive will connect the negative to the 5 volt power supply ground and we will also connect that ground to the arduino ground so we have a common ground finally the drive pin of the servo motor will be connected to pin 6 of the arduino and so now that we've seen this let's take a look at the sketch we'll be using in order to make it work now in our sketch we'll start off by including the IR remote library as we have for all of our experiments we're also going to need to include the Arduino servo library which is already included in your arduino ide a game will define a receive pin for the sensor and i've defined that as being pinned for we also need to define a pin for the servo which i've connected to pin 6 now I have to find an integer that i've called pause and this represents the position of the servo motor I'm initializing it with a value of 90 which will put the servo motor in the center position when the sketch starts I've also defined another variable along called last code and this stores the last code that we received from the remote control and it's used in the repeat function once again we'll define an IR receiver and some results objects we also need to create a servo object and now we move into the setup we start by enabling the receiver we attach the servo motor to the servo pin that we defined earlier and we'll write to the servo motor giving it a value of 90 so the servo starts in the center position then we go into the loop when we actually receive something from the IR receiver will go as follows the first if statement checks to see if we're receiving nothing but a value of F's this means that it's a repeat and so what we do is we assign results dot value to equal the last code that we received now if results dot value equals FF 2 to DD then that indicates the left button has been pressed if you're using a different remotes you will need to change this value accordingly when the left button has been pressed we assign last code to the results value we move the servo ahead by 2 degrees by adding 2 to the value of pause now we make sure that pause has not exceeded 180 because that's the limit of the servos travel so if it goes over 180 we just make the value into 180 and then we write that out to the servo motor for the right button we do virtually the same thing we detect the right button code which in my case is FF c2 3d and we do exactly the same thing except we minus 2 from the value of paused and move it right to degrees and finally if the center button has been pressed we set the value of pause to 90 and we write that out to the servo motor now at the end I've placed a slight delay and that's to present false readings from when the button is being pressed down and then after that we just resume everything and go back and start the loop over and over so now that we've seen the code we'll upload it to our arduino and watch it work ok let's watch our project in action I placed a little piece of blue paper behind the servo so you can see its position a bit better and I've initialized it so it started off in the center position so I'm going to hit the left key I'm just going to press it once and as you can see every time I press that the servo moves a bit and I'm going to hold it down and as you can see it continuous the moves when to do the same with the right key hold it down it continuously moves if I press it once it just moves a bit if I hit the center button it goes back to the center so I can move my servo back and forth and when I hold down the key the repeat function is working alright so that wraps it up for our look at IR remote controls and how we can use them with the Arduino hopefully it's given you some ideas for how you can use these devices in your own projects and it's shown you how versatile the IR remote library actually is now if you need any of the code for the experiments we've done here you will find it along with a detailed article about everything on the drone bot workshop comm website so please go and visit that you'll find a link below the video to that and if you haven't subscribed to the channel yet please do I would really appreciate that until next time I hope you take care of yourselves and I will see you soon here in the workshop bye for now [Music] you
Info
Channel: DroneBot Workshop
Views: 709,419
Rating: undefined out of 5
Keywords: Infrared (IR) Remote control, arduino, arduino uno, IR, arduino tutorial, ir receiver, ir transmitter, ir remote, ir control, remote control, tv remote, arduino remote control
Id: 8E3ltjnbV0c
Channel Id: undefined
Length: 35min 27sec (2127 seconds)
Published: Fri Jan 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.