IRLib Tutorial part 3a: Sending IR Codes

In this lesson we will demonstrate how to send IR codes to a TV, DVD, DVR/cable box or any other device that uses an infrared remote. You will need…

  • a 38 kHz IR receiver module,
  • an IR LED and driver circuit such as described in part 1 of this tutorial,
  • a TV, DVD, DVR or whatever device to control,
  • an IR remote control for that device so we can obtain the proper codes.

Begin the process by wiring an IR receiver to pin number 11 on your Arduino as described in part 1 of this tutorial. Hook up an IR LED and a driver circuit also described in part 1 of this tutorial. If you are using Arduino Uno it will connect to pin 3. On the Arduino Leonardo you should use pin 9

Now you should load the IRrecvDump sketch from the examples folder of the IRLib library. We will use this sketch to determine what codes your infrared remote is transmitting. Run the sketch and open the serial monitor. This is the same process described in part 2 of this tutorial. Write down the received codes for all of the major functions of your device such as power on, channel up and down, volume up and down, rewind, fast-forward, play, stop etc. Also make note of the protocol and the number of bits. Here is what the dump for the power on function looks like on my Magnavox TV
Decoded RC5: Value:180C (13 bits)
Raw samples(24): Gap:30014
Head: m850 s900
0:m900 s900 1:m1700 s900 2:m900 s900 3:m850 s900
4:m850 s900 5:m850 s900 6:m900 s850 7:m900 s1750
8:m900 s850 9:m1750 s900 10:m900
Mark min:850 max:1750
Space min:850 max:1750

As before, you only need to concern yourself with the first line of text. The protocol is “RC5” the code is “180C” and it uses 13 bits.

We are going to presume that you are using a remote that has a protocol that this library understands. If the top line of the dump says “Decoded Unknown” then you will have to skip ahead to section on how to program your own protocol in a future lesson of this tutorial.

Now load the following sketch named “IR_serial_remote” which is shown here. It is also available in the latest update to IRLib version 1.1 which was uploaded to GitHub on April 21, 2013 or later. This upgrade to the library also contains some other minor enhancements and a copy of the “IRservo” example used in part 2 of this tutorial.
#include

IRsend My_Sender;

int protocol;
long code;
int bits;
void setup() {
Serial.begin(9600);
}

long parseHex (void) {
long Value=0; char C;delay(100);
while (Serial.available()>0) {
C= tolower(Serial.read());
if ((C>='0')&&(C<='9')) C=C-'0'; else if ((C>='a') && (C<='f')) C=C-'a'+10; else return Value; Value= C+(Value<<4); }; return Value; } void parseDelimiter () { char C; while(Serial.available()>0) {
C=tolower(Serial.peek());
if( (C>='0') && (C<='9') )return; if( (C>='a') && (C<='f') )return; C=Serial.read();//throwaway delimiters delay (5); } } // enum IRTYPES {UNKNOWN, NEC, SONY, RC5, RC6, PANASONIC_OLD, JVC, NECX, HASH_CODE, LAST_PROTOCOL=HASH_CODE}; void loop() { if (Serial.available ()>0) {
protocol = Serial.parseInt (); parseDelimiter();
code = parseHex (); parseDelimiter();
bits = Serial.parseInt (); parseDelimiter();
/* Serial.print("Prot:"); Serial.print(protocol);
Serial.print(" Code:"); Serial.print(code,HEX);
Serial.print(" Bits:"); Serial.println(bits);
*/
My_Sender.send(IRTYPES(protocol), code, bits);
}
}

Upload the sketch and open the serial monitor. We will type in the protocol number, function code, and the number of bits for one of the functions that you wrote down. For example to turn on my TV I would type in

3,180c,13

The “3” is the number of the protocol that I’m using. If you look in “IRLib.h” at about line 50 you will see the following enum definition.

enum IRTYPES {UNKNOWN, NEC, SONY, RC5, RC6,
PANASONIC_OLD, JVC, NECX, HASH_CODE,
LAST_PROTOCOL=HASH_CODE};

You can send that “RC5” is the third protocol. In my examples I will also be using protocol 5 which is “PANASONIC_OLD” which is the protocol used by my Scientific Atlantic SA 8300 HD cable box/DVR.

After typing in the protocol number, code, and number of bits in the serial monitor you should press enter or click on the “send” button. The Arduino should decode this information and send the proper signals from your IR LED. If you want to make sure that what you are typing is getting parsed you can uncomment the “Serial.print…” statements near the end of the sketch.

Here are the details on how the sketch works…

We start by including the IRLib.h file for the library. We create the sender object and create some integers to store the protocol number, code, and number of bits. The setup routine simply initializes the serial port.

The standard Serial objects include routines for parsing integers in decimal format coming across the serial line but it does not include parsing hexadecimal values so we’ve written a little routine called

long parseHex (void)

Which accepts characters “0” through “9” and “A” through “F” either upper or lower case and converts it into an integer value. We also have a routine called

void parseDelimiter ()

It skips over any commas, blanks or other extraneous characters which you use as a separator.

The main loop simply looks for serial characters available, parses them, and then sends them using the send method as follows

My_Sender.send(IRTYPES(protocol), code, bits);

Note that because the protocol is actually an enum rather than an integer we have to typecast it when passing it to the send method.

Of course it would be quite tedious to have to look up the protocol number, hex codes, and number of bits every time you wanted to send a function. So we’re going to write a program which will send that serial data from your PC to the Arduino. We will create a “virtual remote control” in which you can click on buttons on it will send that serial data to the Arduino which will in turn control your device.

In part 3b we will show you how to use a Python script to create just such a virtual remote and send the codes from your PC or laptop to the Arduino.

IRLib Tutorial part 2: Controlling a Servo Using an IR Remote

As seen on Show-and-TellIn this lesson we will demonstrate how to receive IR codes and to use that information to do something useful. In this case we’re going to position a servo. You will need

  • a 38 kHz IR receiver module
  • a servo motor that uses PWM for positioning
  • an IR remote control for a TV or VCR or any such device

At the end of this blog we will link to some sources for these items. If you do not have a servo available you should be able to follow the code given here to control relays or turn on and off LEDs or any other function you might control with an Arduino microcontroller.
Begin the process by wiring an IR receiver to pin number 11 on your Arduino as described in Lesson 1 of this tutorial. For this example you do not need to hook up an IR LED. We’re going to be receiving only. Also hook up a servo motor to power and ground and to pin number 9 on your Arduino. This diagram shows the complete set up.

Hardware set up for IR servo control demo

Hardware set up for IR servo control demo


Now you should load the IRrecvDump sketch from the examples folder of the IRLib library. We will use this sketch to determine what codes your infrared remote is transmitting. Run the sketch and open the serial monitor. Point your remote at the receiver and press the right arrow button. In this example I’m using a remote from my Sony DVD/VCR. The output on the serial monitor looks like this…

Decoded Sony: Value:86BCA (20 bits)
Raw samples(42): Gap:12550
  Head: m2300  s650
0:m1200 s600 1:m550 s650  2:m550 s650 3:m500 s650
4:m550 s650 5:m1150 s650  6:m1150 s600 7:m600 s600
8:m1150 s650 9:m550 s650  10:m1100 s650 11:m1200 s600
12:m1150 s650 13:m1150 s600  14:m550 s650 15:m550 s650
16:m1150 s650 17:m550 s600  18:m1200 s600 19:m550
Mark  min:500  max:1200
Space min:600  max:650 

We really only need to concern ourselves with the first line of text of this dump. It says that the protocol that was decoded was “Sony” and that the value was 86BCA which is a number in hexadecimal code. The remainder of the information is unimportant to us. You need to write down the value received for each of the following buttons: right arrow, left arrow, up arrow, down arrow, select, and the number buttons 0 through 9.
We are going to presume that you are using a remote that has a protocol that this library understands. If the top line of the dump says “Decoded Unknown” then you either need to use a different remote or skip ahead to section on how to program your own protocol in a future lesson of this tutorial.
Now load the following sketch named “IR_Servo” and we will modify it with the codes you have obtained. You can click on the little orange pair of scissors to copy the code into your clipboard.

/* Example program for from IRLib – an Arduino library for infrared encoding and decoding
 * Version 1.0  April 2013 by Chris Young http://cyborg5.com
 * "IR_Servo" Control a servo using an IR remote
 */
#include <IRLib.h>
#include <Servo.h> 
// You will have to set these values depending on the protocol
// and remote codes that you are using. These are from my Sony DVD/VCR
#define MY_PROTOCOL SONY
#define RIGHT_ARROW   0x86bca //Move several clockwise
#define LEFT_ARROW    0x46bca //Move servo counterclockwise
#define SELECT_BUTTON 0xd0bca //Center the servo
#define UP_ARROW      0x42bca //Increased number of degrees servo moves
#define DOWN_ARROW    0xc2bca //Decrease number of degrees servo moves
#define BUTTON_0 0x90bca  //Pushing buttons 0-9 moves to fix positions
#define BUTTON_1 0x00bca  // each 20 degrees greater
#define BUTTON_2 0x80bca
#define BUTTON_3 0x40bca
#define BUTTON_4 0xc0bca
#define BUTTON_5 0x20bca
#define BUTTON_6 0xa0bca
#define BUTTON_7 0x60bca
#define BUTTON_8 0xe0bca
#define BUTTON_9 0x10bca
IRrecv My_Receiver(11);//Receive on pin 11
IRdecode My_Decoder; 
Servo My_Servo;  // create servo object to control a servo 
int pos;         // variable to store the servo position 
int Speed;       // Number of degrees to move each time a left/right button is pressed
 
void setup() 
{ 
  My_Servo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pos = 90;            // start at midpoint 90 degrees
  Speed = 3;           //servo moves 3 degrees each time left/right is pushed
  My_Servo.write(pos); // Set initial position
  My_Receiver.enableIRIn(); // Start the receiver
} 
 
void loop() 
{ 
    if (My_Receiver.GetResults(&My_Decoder)) {
       My_Decoder.decode();
       if(My_Decoder.decode_type==MY_PROTOCOL) {
          switch(My_Decoder.value) {
            case LEFT_ARROW:    pos=min(180,pos+Speed); break;
            case RIGHT_ARROW:   pos=max(0,pos-Speed); break;
            case SELECT_BUTTON: pos=90; break;
            case UP_ARROW:      Speed=min(10, Speed+1); break;
            case DOWN_ARROW:    Speed=max(1, Speed-1); break;
            case BUTTON_0:      pos=0*20; break;
            case BUTTON_1:      pos=1*20; break;
            case BUTTON_2:      pos=2*20; break;
            case BUTTON_3:      pos=3*20; break;
            case BUTTON_4:      pos=4*20; break;
            case BUTTON_5:      pos=5*20; break;
            case BUTTON_6:      pos=6*20; break;
            case BUTTON_7:      pos=7*20; break;
            case BUTTON_8:      pos=8*20; break;
            case BUTTON_9:      pos=9*20; break;
          }
        My_Servo.write(pos); // tell servo to go to position in variable 'pos' 
       }
     My_Receiver.resume();
    }
}

First you need to tell it what protocol you have received. Edit it into the line which reads

#define MY_PROTOCOL SONY

In general want to use the protocol name that was shown in the dump routine but it must be in all capital letters. If you want to get technical about it, legal values for this item are shown in the file “IRLib.h” at approximately line 53 which looks like this.

enum IRTYPES {UNKNOWN, NEC, SONY, RC5, RC6, 
  PANASONIC_OLD, JVC, NECX, HASH_CODE, 
  LAST_PROTOCOL=HASH_CODE};

Now back to the sketch… You need to edit into the sketch the values which you copied down for each of your buttons on your remote. Note that since these are hexadecimal numbers you should precede each one with “0x”. So in our example for the right arrow which produced a value of “86BCA” the line should look like this…

#define RIGHT_ARROW   0x86BCA

Note that in hexadecimal numbers when the letters “A” through “F” are used you may use either upper or lower case letters.
Once you have all of the values edited into the sketch, you should upload it. The servo will automatically center itself when the sketch has been uploaded and runs. Point your remote at the IR receiver and press the right or left arrow on your remote. The servo should rotate right or left as you push the buttons. If you push the select button on your remote it should center of the servo again. Pressing the numbers 0 through 9 will position the servo to fixed points 20 degrees apart.
By default, when using the left and right arrow buttons, the servo will rotate left or right by 3 degrees for each push of the button. By using the up arrow or down arrow buttons you can change how far the servo rotates. If you press and hold the down arrow for a couple of seconds it will change the default rotation to 1 degree. Pressing the up arrow button increases it to a maximum of 10 degrees. You won’t see anything happen when you push up or down but subsequent presses of left or right will be faster or slower. Here is a YouTube video demonstrating the project.

Now let’s look at the sketch line by line to see how it works. Just below the section which you edited we define several variables and objects

IRrecv My_Receiver(11);//Receive on pin 11
IRdecode My_Decoder; 
Servo My_Servo;  // create servo object to control a servo 
int pos;         // variable to store the servo position 
int Speed;       // Number of degrees to move each time a left/right button is pressed

We create a receiver object and tell it to look for codes on pin 11. IR codes consist of a series of on and off signals called marks and spaces. The receiver object is an interrupt driven routine that looks for a signal every 50µs. It then stores in a buffer of integers the length of each mark or space in 50µs intervals. The buffer is located in the decoder object so we pass the address of the decoder to the receiver. There are technical reasons why we have separated these processes into receiver and decoder unlike other IR libraries. We will get into that in a future must. As mentioned we also have to create a decoder object. The decoder object looks at the values and attempts to determine which protocol was used and what the data values are that were encoded in the signal. We also create a servo object. Details on the servo library can be found here in the standard Arduino documentation. And we need integers to hold the position and the speed of the servo.
In the “Setup” function we initialize the servo on pin 9 set some default values and give it the command to move the servo into the default position. You then have to use this line

  My_Receiver.enableIRIn(); // Start the receiver

To initialize the IR receiver. In the “Loop” routine we have a single if statement that is constantly looking for an IR code.

    if (My_Receiver.GetResults(&My_Decoder)) {
       My_Decoder.decode();

The “GetResults(address)” function returns true if a code has been received. It puts it into the decoder object whose address you passed to it. You then call the “decode()” method of your decoder.
We next check to see if the results received were from the proper protocol. No need to go through a bunch of testing if you picked up a signal from a different remote. We check to see if the “decode_type” is the right one using this if statement. The variable “My_Decoder.decode_type” is an enum which was shown earlier in this lesson and can be found in “IRLib.h”.

       if(My_Decoder.decode_type==MY_PROTOCOL) {

Finally we check the “My_Decoder.value” in a switch statement to see if any of our defined codes are what was sent. If none of the case statements are activated, the code just falls through and updates the servo at its previous position.
It is important that we restart the IR receiver after any signal has been received even if it’s not one that we were interested in. This is done with the following statement

     My_Receiver.resume();

You could easily modify the sketch to handle multiple servos doing all sorts of things such as positioning a robot arm or steering a remote-controlled car or doing any other function. You could hook up LEDs that would turn off on depending on which button to press on the remote. You could hook up relays to turn lights on motors off and on.
As promised here are some links to the hardware mentioned in this post

In the next lesson we will focus on transmitting IR signals.