005 Decode IR code using Arduino language

1. Forewords

I found a very good library for receiving/emitting Infrared code. It supports NEC, SONY, RC5 and RC6 codes. Anyway, I had some problems using an universal TV Remote:

In the image above, the firmware is displaying the two Remote keys I have pressed repeatedly: key nr.2 and key nr.5

but I get also garbage. For example, for key nr. 2 I get 2, 02, 802 hex values. And this is true for any key I press.

I tested this with only one TV remote, so I can't decide which is the problem. I will try with a different remote and if the behavior is the same, then I will use a workaround.

My guess is that the range of the transmitted code is in 0x00 - 0xFF range. So, the algorithm can be this:

- if the length of string code is 3 chars longer, then remove the first char.

- else if the length of string code is only one char, then display first a "0" char then the code

- otherwise, display the code (which is 2 chars length)

--- to be continued ---

Still no luck in finding a second RC5 IR remote control. I found one but is of NEC protocol :( but I played a little with it - good to have it for some future projects (even if I don't have any ideas for now):

I think I will put this on hold until I get an RC5 IR remote (which affects the first goal - remake of the original EvB4.3 demo application) and continue with SD card examples and the second goal - a logger application.

And I must do it fast, because I intend to remove the ATmega controller from the board and continue to use the peripherals for FreeJALduino projects (some Pic Micro Pascal libraries and testing Matt's SD card library in JAL language). Also some projects are planned for Pinguino. So, I really need to hurry this up...

UPDATE 2010-11-07: I obtained another RC5 TV remote and the results are the same as previously reported so, the work around is required. And I did it this way:

     if (results->value >= 0x800) {       results->value = results->value - 0x800;     } 

Now results are clean so, we can continue with the initial road map .

2. Set the board

Data is sent over the USB cable to the console terminal included in Arduino IDE.

Start Arduino IDE and write the sketch bellow:

3. The Sketch

/* * IR decode: receive a signal, decode and display the type and code, * Support NEC, SONY, RC5, RC6 code type. * An IR detector/demodulator must be connected to the input RECV_PIN. * An IR LED must be connected to the output PWM pin 3. * A visible LED can be connected to STATUS_PIN to provide status. * * The logic is: * If an IR code is received, decode and display it. * * Version 0.11 September, 2009 * Copyright 2009 Ken Shirriff * http://arcfn.com */ #include <IRremote.h>

int RECV_PIN = 1;

int STATUS_PIN = 13;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()



irrecv.enableIRIn(); // Start the receiver pinMode(STATUS_PIN, OUTPUT);

digitalWrite(0, LOW);


// Storage for the recorded code int codeType = -1; // The type of code unsigned long codeValue; // The code value if not raw int codeLen; // The length of the code int toggle = 0; // The RC5/6 toggle state void showCode(decode_results *results) {

codeType = results->decode_type;

int count = results->rawlen;

if (codeType == UNKNOWN) {

Serial.println("Received unknown code!");

codeLen = results->rawlen - 1;


else {

if (codeType == NEC) {

Serial.print("Received NEC: ");

if (results->value == REPEAT) {

// Don't print a NEC repeat value as that's useless. Serial.println("repeat; ignoring.");




else if (codeType == SONY) {

Serial.print("Received SONY: ");


else if (codeType == RC5) {

Serial.print("Received RC5: ");


else if (codeType == RC6) {

Serial.print("Received RC6: ");


else {

Serial.print("Unexpected codeType ");

Serial.print(codeType, DEC);



// the workaround if (results->value >= 0x800) {

results->value = results->value - 0x800;


// end workaround Serial.println(results->value, HEX);

codeValue = results->value;

codeLen = results->bits;



void loop() {

digitalWrite(STATUS_PIN, HIGH); //switch off the LED on our board (common anode) if (irrecv.decode(&results)) {

digitalWrite(STATUS_PIN, LOW);


irrecv.resume(); // resume receiver } }

4. Results

The results are as expected and the code is sent to the serial terminal, nothing special from now on.