Software installation

Installation of the IRLib library is as follows:

  1. Visit the IRLib2 page on GitHib.
  2. Select the “Download ZIP” button, or simply click this link to download directly.
  3. Uncompress the ZIP file after it’s finished downloading.
  4. The resulting folder should be named "IRLib2-master" and will contain 5 separate folders. That is because IRLib 2.x is actually a collection of 5 libraries that work together. Sometimes in Windows you’ll get an intermediate-level folder and need to move things around.
  5. Copy all five folders into your Arduino library folderalongside your other Arduino libraries, typically in your (home folder)/Documents/Arduino/Libraries folder. Libraries should not be installed alongside the Arduino application itself.
  6. Re-start the Arduino IDE if it’s currently running

This repository consists of a total of five libraries each of which must be in your arduino/libraries/ folder. So for example it should be installed as follows…

  • arduino/libraries/IRLib2
  • arduino/libraries/IRLibFreq
  • arduino/libraries/IRLibProtocols
  • arduino/libraries/IRLibRecv
  • arduino/libraries/IRLibRecvPCI

Do not install them in a single folder such as this…

  • arduino/libraries/IRLib2_master
    • IRLib2
    • IRLibFreq
    • IRLibProtocols
    • IRLibRecv
    • IRLibRecvPCI

Here’s a tutorial that walks through the process of correctly installing Arduino libraries.

Hardware Needed

IRLib runs on 8-bit AVR based Arduino boards such as Uno, Leonardo, Mega and Micro. It also runs on the Leonardo portion of the Arduino Yun. We have recently added support for 32 bit ARM SAMD 21 processors as used in the Arduino Zero, Feather M0, and Circuit Playground Express. However we do not support the Arduino Due or other Arduino-like systems. Unfortunately at this time it does not run on ATtiny85 base systems such as Adafruit Trinket and Adafruit Gemma but support for that is in the job jar and should be available in a stripped-down version for those platforms in the near future. At this writing, it has not been tested on the Adafruit Trinket Pro however since it is based on the same ATmega328 processor as the Uno, it should work fine. So the first thing you need is Arduino Uno or other compatible board.

You will need an IR receiver. Such as the TSOP38238 shown on the right column under featured products. This device combines an IR sensitive photocell, a 38 kHz bandpass filter, and automatic gain control. It operates on a variety of supply voltages including 3.3v and 5v. It de-modulates the received IR signal and gives you a nice clean square wave of off and on pulses at the voltage level of your power supply. This means it is ideal for feeding its output directly into the digital input pin of our Arduino.

Finally you will need an IR remote such as you use for controlling your TV, cable box, or DVD player. All of our examples will use the Adafruit Mini Remote Control shown on the right however we will show you how to detect what protocol your own TV remote is using and if it is a protocol supported by IRLib you can use it instead.

Connecting the IR receiver is very simple. Connect the left-hand pin to any digital input pin on your Arduino. In our examples we will use pin 2. Connect the center pin to ground and the right-hand pin to +5v.

Note that this device has a bandpass filter tuned to 38 kHz which is the typical frequency for most protocols. However some protocols use frequencies from 36 kHz all the way up to 57 kHz. The filter however is not extremely specific and we have had good success receiving anywhere from 36-40 kHz using a 38 kHz receiver. The Panasonic_Old protocol however uses 56 kHz. The TSOP38238 sold by Adafruit has difficulty decoding that frequency. I have however had good success with the receiver sold by Radio Shack at 56 kHz even though it is a 38 kHz device. Radio Shock did not list a part number but we believe it to be a TSOP4438. Sadly that may not be an option anymore :-(

These devices are made by Vishay and come in a variety of package styles, frequencies, and AGC methods. If the Adafruit device does not work for you and you need 56 kHz you can refer to the following guide.

More information on receivers as well as schematics for using multiple receivers can be found in the IRLib manual section 1.4.3.

Decoding IR Data

Load the following sketch. It is a slightly modified version of "dump" sketch from the examples folder of the library. All of the example sketches are in the folder "IRLib2/examples". 

#include "IRLibAll.h"

//Create a receiver object to listen on pin 2
IRrecvPCI myReceiver(2);

//Create a decoder object 
IRdecode myDecoder;   

void setup() {
  delay(2000); while (!Serial); //delay for Leonardo
  myReceiver.enableIRIn(); // Start the receiver
  Serial.println(F("Ready to receive IR signals"));

void loop() {
  //Continue looping until you get a complete signal received
  if (myReceiver.getResults()) {
    myDecoder.decode();           //Decode it
    myDecoder.dumpResults(true);  //Now print results. Use false for less detail
    myReceiver.enableIRIn();      //Restart receiver

After the sketch has loaded, open your serial monitor and make sure it is set to 9600 baud. Aim your IR remote at the receiver and press a button. In this example we press the "Play/Pause" button on the Adafruit Mini Remote. The results were as follows:

Decoded NEC(1): Value:FD807F (32 bits)
Raw samples(68): Gap:40826
Head: m8850 s4450
0:m500 s600 1:m550 s550 2:m500 s600 3:m550 s600
4:m500 s600 5:m500 s600 6:m500 s600 7:m550 s550
8:m500 s1750 9:m500 s1700 10:m500 s1700 11:m550 s1650
12:m550 s1700 13:m500 s1700 14:m500 s600 15:m550 s1700

16:m500 s1700 17:m500 s600 18:m500 s600 19:m500 s600
20:m550 s600 21:m450 s650 22:m500 s600 23:m500 s600
24:m500 s600 25:m500 s1700 26:m550 s1700 27:m500 s1700
28:m500 s1700 29:m550 s1700 30:m500 s1700 31:m500 s1700

Mark min:450 max:550

The important part of this dump is the first line. This tells us that the protocol detected was "NEC" which is protocol number "1" in IRLib's supported protocols. The data value received was the 32-bit hexadecimal value FD807F. The rest of the information is the raw timing data of the actual marks and spaces received. That information is useful in trying to understand and supported protocols.

This 32-bit number uniquely identifies the button that you pushed. If we push the Volume down and Volume up buttons on this remote we would get the values 0xFD00FF and 0xFD40BF.

Try pressing various buttons on a TV or DVD remote you might have lying around the house. If the top line says:

Decoded Unknown(0): Value:0 (0 bits)

this means that IRLib did not understand the protocol used by your remote. Here are some typical values from other remotes. I got these from the power button on a Sony DVD player, and the play button on my Scientific Atlantic DVR/Cable Box.

Decoded Sony(2): Value:74BCA (20 bits)
Decoded Panasonic Old(5): Value:37990C (22 bits)

This shows that the DVD player used Sony protocol which is protocol number 2 and that it is a 20 bit protocol. The cable box uses Panasonic_Old protocol 5 which is 22 bits. Most protocols always use the same number of bits however some such as Sony have different versions which could use 8, 12, or 15 bits in addition to 20.

How It Works

Let's look at what's going on here. The receiver object listens to the IR sensor and when it sees a signal it starts measuring the timing of the marks and spaces. If a particular amount of time passes with no additional signals, it presumes that the data is complete and when you call My_Receiver.GetResults it returns "true". It passes the data to your decoder object. The decoder uses the timing information and the number of bits to see if it matches one of the supported protocols. If it succeeds, it returns "true" although in this sketch we did not bother to check that first.

You can access the protocol number in My_Decoder.protocolNum, the number of bits in My_Decoder.bits and the the decoded data value in My_Decoder.value.

At the top of the sketch we created the decoder object as type "IRdecode". This is a class which incorporates all 11 of the supported protocols. If you're using the library to control a device such as a servo or turn relays off and on, you probably are going to be using one remote with one protocol. Once you know which protocol you are using, you may wish to use a different decoder class that only works for your particular protocol. It can save valuable program space in your sketch. For example if we were using the Adafruit Mini Remote which uses NEC protocol would change line #7 to read:

IRdecodeNEC My_Decoder;

Protocol Specific Issues

IRLib supports 11 protocols directly and may include example code on how to implement others. As stated earlier, one of the jobs of a library is to isolate the application programmer from the need to deal with internal issues. However there are some protocol specific things that you may need to deal with.

The protocols are enumerated in IRLibProtocols/IRLibProtocols.h at approximately line 14 as follows.

#define UNKNOWN 0
#define NEC 1
#define SONY 2
#define RC5 3
#define RC6 4
#define JVC 6
#define NECX 7
#define SAMSUNG36 8
#define GICABLE 9 
#define DIRECTV 10
#define RCMM 11

Here are some protocol specific issues you may have to deal with.

NEC Repeat Codes

The NEC protocol uses a special sequence of marks and spaces that mean "I held down the button so you should repeat whatever I sent you last time". It is up to you to decide do I want to allow repeat codes or do I want to force the operator to push and release the button each time. IRLib returns a value of 0xFFFFFFFF to tell you that the special repeat sequence was received. You can ignore that sequence which forces the user to release and repress the button each time or you can store the previously received code and process it whenever you see the special repeat message.

Sony Triple Messages

The technical specification for Sony protocol says that you should send each code 3 consecutive times per keypress. IRLib takes care of sending three times for you so you don't need to do anything special. However when receiving Sony, be aware that you're going to get three copies of the data each time the user presses a button. If you're busy processing the first sequence you might miss the other two so it won't matter. But you need to be aware of it in case you're counting the number of keypresses or some other application.

RC5 and RC6 Toggle Bits

The RC5 and RC6 protocols invented by Phillips use a special toggle bit to let you know whether a code was generated by holding down the button or whether this is an independent keypress. For example I have a TV which uses RC5 protocol and the code for Volume Up is 0x1010. If I press and hold that button it sends the same code repeatedly. However if I release the button and press it again I get 0x1810. Every other keypress the single bit 0x0800 will toggle off and on.  You can make sure that you ignore this feature by masking out that particular bit. When you receive a decoded value from this protocol you could do:

My_Decoder.value &=0xf7ff;

This will make sure that the toggle bit is always off. The RC6 protocol also has a title bit which is 0x10000. Therefore to mask it out you would do:

My_Decoder.value &=0xfeffff;

This guide was first published on Feb 26, 2015. It was last updated on Feb 26, 2015.

This page (Receiving and Decoding IR) was last updated on Feb 14, 2015.

Text editor powered by tinymce.