Using an Infrared Library on Arduino Created by Chris Young Last updated on 2015-02-26 05:00:06 PM EST
Guide Contents Guide Contents Overview About IR libraries Receiving and Decoding IR Software installation Hardware Needed Decoding IR Data How It Works Protocol Specific Issues NEC Repeat Codes Sony Triple Messages RC5 and RC6 Toggle Bits Controlling NeoPixels with IR Controlling a Servo with IR Setting up the Example Upload the Code How It Works Special Instructions for ATmega32u4 based systems Sending IR Codes Hardware Issues Loading the Software Sending and Receiving in the Same Program All photos, vide
Overview Most consumer electronic devices such as TV, cable box, DVD players and other devices use infrared signals for remote control. Each manufacturer has its own protocols for encoding the data so that signals intended for one device do not interfere with another. In an earlier tutorial by LadyAda (http://adafru.it/ez6) she describes the inner working of reading IR signals from a remote and creating your own IR signals using an IR LED.
About IR libraries IR signals consists of a series of modulated pulses called "marks" separated by intervals called "spaces". Typically there is a long mark and space at the beginning of each signal that serves as a header. Then by varying the timing of marks and spaces, a sequence of bits is transmitted. If you had to store the precise timing of the entire signal it would take an array of up to 100 16 bit integers.
Receiving and Decoding IR Software installation Installation of the IRLib library is as follows: 1. Visit the IRLib page on GitHib (http://adafru.it/ez9). 2. Select the “Download ZIP” button, or simply click this link (http://adafru.it/eza) to download directly. 3. Uncompress the ZIP file after it’s finished downloading. 4. The resulting folder should be named "IRLib-master" and will contain a number of header files, IRLib.cpp and two subfolders containing a user manual and some example sketches.
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 11. 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.
IR Receiver Selection Guide from Vishay (PDF format) http://adafru.it/ezb 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 "IRecvDump" sketch from the examples folder of the library. #include //Create a receiver object to listen on pin 11 IRrecv My_Receiver(11); //Create a decoder object IRdecode My_Decoder; void setup() { Serial.
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 32:m500 Extent=65850 Mark min:450 max:550 The important part of this dump is the first lin
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.decode_type, 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 seven of the supported protocols.
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.
Controlling NeoPixels with IR In this very simple example we will change the color of a NeoPixel by pushing buttons on the remote. We are using a single pixel but you can modify the sketch to control an entire strip or matrix. For more information on NeoPixels visit this guide in the Adafruit Learning System (http://adafru.it/dYa). Here is the code: © Adafruit Industries https://learn.adafruit.
#include #include IRrecv My_Receiver(11);//receiver on pin 11 IRdecode My_Decoder;//Decoder object //One NeoPixel connected to pin six Adafruit_NeoPixel strip = Adafruit_NeoPixel(1,6,NEO_GRB + NEO_KHZ800); void setup() { strip.begin(); strip.show(); // Initialize all pixels to 'off' My_Receiver.enableIRIn(); // Start the receiver } void loop() { if (My_Receiver.GetResults(&My_Decoder)) { My_Decoder.decode(); if (My_Decoder.decode_type== NEC) { switch(My_Decoder.
actually change the color and My_Receiver.resume() to reset the receiver so it can collect another code. Upload the sketch and try pressing the volume down, play/pause, and volume up buttons. You should see the pixel change to red, green, or blue. You can easily add additional case statements and colors or perhaps have one of the cases call an animation routine to animate the entire strip of pixels. Different buttons on the remote would select different animation patterns.
Controlling a Servo with IR Setting up the Example In this example we will control the servo using an IR remote. We can adjust the speed that the servo moves and we can select individual preset angles for positioning the servo. Here is an illustration showing how to wire up the devices. As usual we have an IR receiver connected to +5v, ground, and pin 11. We also have a servo with three wires. The red wire is +5v.
Upload the Code Below is a version of the IRservo.ino sketch from the IRLib examples folder. It has been modified to be used with the Adafruit Mini Remote. If you're using a different remote, you will have to collect information about your codes for various buttons using IRrecvDump and modify the sketch with the proper protocol name and codes. #include #include // You will have to set these values depending on the protocol // and remote codes that you are using.
void loop() { if (My_Receiver.GetResults(&My_Decoder)) { My_Decoder.decode(); if(My_Decoder.decode_type==MY_PROTOCOL) { if(My_Decoder.value==0xFFFFFFFF) My_Decoder.value=Previous; switch(My_Decoder.
How It Works The program creates a receiver on object, a decoder object and a servo object. You can find more information on the standard Arduino Servo library here (http://adafru.it/ecQ). The setup function attaches the servo, enables IR input, and initializes several variables. The loop function gets an IR code and passes it to a switch statement depending on its value. Each case of the switch statement handles a different function moving the servo as needed.
You will need to put // in front of #define IR_SEND_TIMER1 to comment out that line. Then remove the slashes from in front of one of the other two options either TIMER3 or TIMER4_HS. Note that these defines say "IR_SEND_TIMERxx". Later in the file we copy this value to also be used as the receiving timer. If you're using Leonardo and you later use IRLib to send IR signals you will need to make note of the numbers after these defines.
Sending IR Codes Hardware Issues IRLib not only receives and decodes IR signals but it can transmit them as well using an IR LED and a driver circuit. The library has been used to control TVs, cable boxes, DVDs, VCRs, and IR controlled toys such as helicopters and dinosaur robots. It could also be used to control some home automation devices. Some users have attempted to © Adafruit Industries https://learn.adafruit.
control air conditioners and fans however protocols used for air-conditioners are extremely difficult to implement and we have not directly supported such protocols because they are very rare. Typically the output pin of an Arduino cannot supply sufficient current to drive and IR LED so you will want to implement a simple driver circuit using NPN transistor and a 470 ohm resistor is shown here: Make sure that you get the polarity of the LED correct.
While we can connect an IR receiver to any available digital input pin, you can only use very specific pins for output. The library uses PWM pins and modifies the timing parameters to change the default frequency of that pin. The default timer is TIMER2 on the Arduino Uno and Arduino Mega. On the Leonardo with is TIMER1. The pin numbers are pin 3 for Uno and use pin 9 for the Leonardo and Mega.
IRsendSony My_Sender; Inside the loop the send command would then be: My_Sender.send(0xa8bca, 20); Some protocols such as NEC always use the same number of bits and so you do not need to specify as an additional parameter. See the prototypes in IRLib.h and the users manual to see if the extra bits parameter is required. Sending and Receiving in the Same Program There are special considerations when doing sending and receiving in the same program.
In a separate tutorial we will show you how to use IRLib to create your own universal remote by either sending data through the serial port into the Arduino or by creating a web-based universal remote using Arduino Yun.