This is the first in series of articles on using my infrared remote library for Arduino. In this installment we’re going to show you how to set up the hardware and how to run a quick demo sketch. You can find out more about the library on my IRLib page.
In order to detect a single from IR remote control such as you might use for your TV or home entertainment system you need an IR receiver module. Typically I use a unit I get from Radio Shack. You can also buy a similar module from my favorite supplier Adafruit.com. See the links at the end of this post for places where you can buy all of the parts mentioned in this article.
Pin 1 (on the left as you looking at the lens) needs to be connected to a input pin of the Arduino. It can be any could be any pin that doesn’t conflict with anything else you’re doing with the device. All of the examples in the library assume you are connected to pin 11 so we suggest you use it. The center pin 2 connects to ground any right-hand pin 3 should connect to your +5V power supply. If you are using a different microcontroller that runs at +3V this device will work at that voltage.
The simplest output device is simply an infrared LED and a current limiting resistor. IR LEDs can be purchased from a variety of places. Again see the links at the bottom of this post for sources. You are limited to using PWM pins for output because we use the PWM feature to modulate the signal. The default pin to use on Arduino Uno or other ATmega328-based controllers is pin 3. This particular pin is connected to “timer 2” of the chip. However the Arduino Leonardo and other controllers based on the ATmega32u4 does not have a “timer 2”. On these devices the default is to use timer 1 and pin 9 for output. We will talk more later about other options for timers and how to use this library on other types of hardware. For now all you need to know is if you have an Uno you should use pin 3 or a Leonardo use pin 11. Connect a 100 ohm resistor in series with the LED. Make sure you get the polarity of the LED correct. The shorter of the two leads should connect to ground. The longer lead connects to the resistor which in turn connects to the Arduino. Here is a schematic for the simplest setup. Note this post was edited on 2/5/2014 to correct the polarity. After telling you to be sure to get it right, I had described it wrong. The schematic has always been correct but my description was wrong. Sorry about that.
That was the absolute simplest schematic however the output from the IR LED will be pretty weak because the output pins of Arduino cannot supply much current. You might want to consider adding an NPN transistor to drive the LED.
Here is a schematic using a PN2222 NPN transistor and a 470 ohm base resistor with the LED. WARNING: This circuit will drive the LED beyond its continuous current limits of 100 mA. However because we are only pulsing the LED for a brief period using a modulated signal we can get away with this. So you should only use the circuit if you are certain that the output pin is not going to be left on continuously for more than a fraction of a second.Here is a simple sketch you can use to test if you are receiving IR signals. Is a stripped down version of the example sketch “IRrecvDump” that is in the examples subdirectory of the library.
/* Receiving IR signal and dump the details */
#include
//create a receiver object
IRrecv My_Receiver(11);//Use input pin 11
//create a decoder object
IRdecode My_Decoder;
void setup()
{
Serial.begin(9600);//We will read the output on the serial monitor
My_Receiver.enableIRIn(); // Start the receiver
}
void loop() {
//Loop until we get a signal and pass it to the decoder
if (My_Receiver.GetResults(&My_Decoder)) {
My_Decoder.decode();//decode the signal
My_Decoder.DumpResults();//dump the results on the serial monitor
My_Receiver.resume(); //restart the receiver
}
}
Upload the sketch and start the serial monitor on the Arduino IDE. Point a remote control such as a TV remote at the receiver and press a button. It will dump the information about the signal received. If it’s a protocol with the library understands it will tell you which protocol and it will give you a hex number of up to 32 bits that is the code for that particular function.
To test your transmitter, first determine the 32 bit hex code and protocol that the library can decode. A good example would be the power off/on button of your device assuming that your protocol is supported. As an example I sent a power signal to my Sony DVD player. The dump program tells me that it is a 20 bit code that is Sony protocol and that the value is 0xa8bca. The following sketch is a version of the example “IRsendDemo” in the examples directory of the library.
/*Transmit a power code for Sony DVD*/
#include
IRsend My_Sender;
void setup()
{
Serial.begin(9600);
}
void loop() {
if (Serial.read() != -1) {
//send a code every time a character is
//received from the serial port
//Sony DVD power A8BCA
My_Sender.send(SONY,0xa8bca, 20);
}
}
You will need to substitute the protocol name, the hexadecimal code, and the number of bits for whatever device you are operating. After you upload the sketch type a character into the serial monitor and press enter. Every time you transmit a character from the serial monitor, it will transmit the code you have programmed into the “My_Sender.send(…)” Function call.
In the installments which follow we will explain more of the details of the library including a more advanced circuit for transmitting codes. However for now this should get you up and running.
As promised here are some links to the hardware mentioned in this post
- Arduino Uno available at Adafruit.com or RadioShack.com
- Arduino Leonardo available at Adafruit.com
- 38kHz IR receiver module available at Adafruit.com or RadioShack.com
- IR LED available at Adafruit.com (single unit) or Adafruit.com (25 pack) or RadioShack.com
- 100 ohm resistor at RadioShack.com (5 pack)
- 470 ohm resistor at RadioShack.com (5 pack)
- NPN transistor at Adafruit.com (10 pack) or RadioShack.com
In the next installment will show how to receive and decode IR signals and use them to do something useful such as control a servo.
Hi,
First of, thank you for your hard work on the library. It takes me so much time to understand a little bit of KS library. I have a question, Can I use your IRLib to use 2 receivers at same time? I mean they can both receive signal from a single 38 kHz IR source and pull 2 different PWM pin high?
Hello:- Thanks for the IRlib. I am a volunteer working with continuing care clients. I am using Arduino to serve as a multi-purpose remote control amongst other things. I came across a Cisco 8642HD pvr whose remote’s output is not decodeable by IRlib. Would you say the simplest method would be to just clone the pulses? Could I modify IRlib to do that as in here http://hackaday.com/2013/11/20/primer-tutorials-for-arduino-ir-remote-cloning-and-keyboard-simulation/ ?
Would appreciate some guidance. Thanks.
“Make sure you get the polarity of the LED correct. The longer of the two leads should connect to ground. The shorter lead connects to the resistor which in turn connects to the Arduino.”
Is this correct?
http://upload.wikimedia.org/wikipedia/commons/thumb/5/52/%2B-_of_LED_2.svg/418px-%2B-_of_LED_2.svg.png
Surely the positive anode has the longer connector and should connect to the resistor?
You are correct I had polarity backwards. The schematic is correct but my description was wrong. Good catch.
hello friend I write from chile
I want to try your library
I’m making a universal remote control with an arduino nano and NRF24L01
coamdos voice sent him from a raspberry
the idea is to turn on and off the tv for you
my question is
as I can configure the output pins LEDs go
and input pins on the arduino nano
I hope to finish my project and comment as I was
regards
It depends on what version of the nano you have. We support the ATmega328 used by the nano 3.x but not the ATmega168.
uso el arduino nano 3.0 and mega
how can I disable the pin 13 as this what utiliso with NRF24L01
saludos desde chile
i want to how to access the receiving code to on off any output pin of arduino using if statement.should i use mydecoder-decode() return value to put in if statement?
You would need a receiver object to receive the code a decoder object to decode it. Then the results would be in MyDecoder.value and you would use that value in your if statement. I suggest you read through the documentation and look at the example code. Look at the servo example later in this tutorial. Except instead of servo commands you would have commands to turn off or on whatever pin you wanted depending on the value received.
Thank you for your work on this article and the library. I was wondering if the library would work with a tsop4856 or not. I know that it is for 56 kHz modulated info, but you do reference it (the 56 kHz ir) in the hardware area. Everything I am able to find to read on the arduino uno and the ir lib seems to suggest to me that the library was written to be used with 38khz carrier frequency. I just happen to have tsop 4856s on hand, but can order some 38s if necessary. I will probably acquire a cheap universal remote for my project. Thanks for you input.
The modulation frequency depends on what protocol you want to use. Most IR remotes use frequencies of about 38 kHz. However the protocol which I call “old Panasonic” which is used by Cisco and Scientific Atlantic cable boxes and a “RCA protocol” which is used by some very old RCA brand equipment both use 56 kHz. Most people are interested in using 38 kHz devices and are worried if they can receive 56 kHz signals. I’ve had mixed results. Some 38 kHz devices can read 56 kHz signals. I’ve never had a 56 kHz device so I don’t know if the gap in the bandpass filter is wide enough to pick up 38. So you’re kind of doing the opposite of what most people are asking me 🙂 The bottom line is the library can use any frequency from about 36 to about 58 for transmitting and for receiving it can use whatever your hardware device is. It should work just fine with that device if you are actually sending it 56 kHz signals. I’ll be interested to see if a 56 kHz TSOP device is wide enough to sense 38 signals.
Thanks for the library! I want to suggest a change to your schematic using a transistor to drive the IR LED. You rightly say that you’re overdriving the LED, but why bother? Wouldn’t it make more sense to put a current limiting resistor in series with it which would remove all risk of destroying the LED and possibly the transistor. Most IR LEDs I’ve used have a max current rating of about 100mA so for a 5v supply, a resistor value of 47ohms would allow a max current through the LED of about 75-80 mA, well within the rated range. If you want a higher output, just add a second resistor and LED in parallel with the existig LED and resitor. Just feels like a better design.
The purpose of that schematic was to provide the simplest emitter possible using parts you might find around in your spare part drawer. For most of the applications of this library, the overcurrent is not a problem. People like to get as much power as simply as possible. I do want to give other possible schematics including one with up to 4 LEDs each with current limiting resistors.
hi! excuse my English. which is the voltage VCC in the schematic IR emitter transistor? thanks!
Use whatever voltage your Arduino is using. If it is a 5v Arduino then use 5v. Some Arduinos use 3.3v and that should work as well although the transmitter will not be as powerful.
Hi,
Really nice tutorial, thanks for it. I’ve done my setup using arduino uno for the IR transmitter and arduino nano for emitter.
I got no problems decoding the NEC signals from my remote control, but this is not the case with my arduino emitter. I tried other protocol (SONY) and it worked just fine. What could be the cause of that?
Let me make sure I understand your emitter will not work with NEC but it does work with Sony? It is highly unusual that the choice of protocol affects whether or not a transmitter works. When you say that NEC doesn’t work do you mean it will not control your home electronics devices or does it mean that you cannot decode the signal using the receiver on the other Arduino? Same question about the Sony. When you say that the Sony works do you mean it operates your home electronics equipment or do you mean you’ve successfully decoded the receiving of the Sony signal? I’m going to need more to work with to figure out what your problem might be.
May I pose a different but related problem.
The parking gate of our building opens up with a dedicated remote.
I would like to use a smartphone with IR blaster to do the same.
I would like therefore
1. to use Arduino record and copy the IR sequence from the remote.
2. transfer a file with that IR sequence to the smartphone and
3. Use an application on the smartphone that knows to read such file of IR sequences.
I think that I know 1. i.e. how to read the IR sequences using the Arduino and the library.
Any suggestions regarding 2 and 3 are welcome.
Great library,
I need to receive a continuos variable frequency signal from a generator, so i use photodiode instead an TSOP. Can I copy the decoded frequency to a variable?
Many thanks.
Ok, I found it:
if (My_Freq.HaveData()) {
delay(10);
My_Freq.disableFreqDetect();
My_Freq.ComputeFreq();
double Khz=My_Freq.Results;
Serial.println(Khz);
My_Freq.enableFreqDetect();/
}
I have that works out for you. I don’t have the experience with photodiodes.
How to receive and decode the IR frequencies of Tata Sky DTH?
I’m sorry I don’t know what a “Tata Sky DTH” is. But there is an entire section of the documentation on how to identify and decode unknown protocols. It’s an advanced topic but it is fully documented.