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 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. The shorter of the two leads connects to the transistor and the longer one connects to the positive supply. Note that the current passing through the LED will in all likelihood will exceed the maximum continuous current rating. However because the signal is modulated and sending a sequence of pulses that will only last a few milliseconds the circuit will work fine.
More advanced driver circuit schematics are available in the IRLib manual in section 1.4 Hardware Considerations.
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. If you have modified the library to use a different timer such as changing the timer number on a Leonardo to avoid conflicts with the servo library, then you will need to use a different specific pin. See section 1.4.1 Supported Platforms in the IRLib users manual for a table which shows the relationship between hardware timers and pin numbers.
Loading the Software
We presume you have already installed the IRLib library as described earlier in this tutorial. Let's load a simple sketch and see how it works. This is the IRsendDemo sketch from the examples folder.
#include <IRLibAll.h> IRsend mySender; 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 mySender.send(SONY,0xa8bca, 20); } }
In this somewhat trivial example we send the code to turn on and off a Sony DVD player every time you type a character into the serial monitor. We create a sending object mySender. There is no set up except to initialize the serial port. In the loop we check for incoming characters and if we've got one we send the code.
The send method has three parameters: the protocol type, the data, and the number of bits.
The IRsend object we created is a generic routine that supports all 11 supported protocols. However in this case since we are only using one protocol we could've created the object using:
IRsendSony mySender;
Inside the loop the send command would then be:
mySender.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 the users manual to see if the extra bits parameter is required.
The users manual also gives you information about how to create decoding and sending routines that only use a specific subset of the supported protocols. This is much easier to do in IRLib 2.0 than it was in the original version.
Sending and Receiving in the Same Program
There are special considerations when doing sending and receiving in the same program. Both sending and receiving make use of the building hardware timers. However the timer is used for two different purposes. When you send a code, it reconfigures the timer and disables receiving. So you have to reenable your receiver after each send. For example:
mySender.send(Protocol, Data, Bits); myReceiver.enableIRIn(); // Re-enable receiver
Normally you would only need to call myReceiver.enableIRIn(); once in the setup routine but if you are both sending and receiving you have to call it after each send.
For a complete example of how to send and receive in the same program, look at the record.ino sample sketch in the "IRLib2/examples" folder. When you load the sketch, open your serial monitor, point a remote at the receiver and push a button. The program will capture that code. Then every time the type of character into the serial monitor it will repeat that code through the LED.
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.
All photos, videos and wiring courtesy Kenneth Young
Text editor powered by tinymce.