IR controller detection

Update 27.2.2015: This doesn’t work out of the box anymore since Arduino 1.0.X includes a library for Robot IR-control with a conflicting library name, the code is not the same though. The new library is inside Arduino program (right-click, show package contents > Contents > Resources > Java > libraries >RobotIRremote. So either trash that one and use the IRremote Library as normal, or rename instances of IRremote to something else (IR.h for example) in all of the file names and inside files (IRremote.cpp, IRremote.h, IRInt.h). Then #include <IR.h>

Detecting Infrared commands is pretty easy with the IRLibrary (link updated) by Ken Shirriff. You can get the latest version here. While the first web page states to use a three pin IR transistor for detection, it functions with the IR transistors we have at the lab. Red ones are the detectors. Hooking this up needs just a 10K resistor between an Arduino digital pin and the positive leg (the longer) of the IR Detector. (Could just pull up?). Here is a schematic that helps.

I used the Arduino IR Library Receive Demo to write down a hex code to the Serial Monitor for each button press. Some button presses were interpreted with various codes (I guess the remote batteries were a bit weak, the ambient light affects and the detectors are not the most powerful). Then one can compare a code by adding the HEX description (0x) to the front of the HEX sequence printed out in serial monitor: if (results.value == 0xA818A1A0) do something.

So each remote has their own patterns and sets of buttons, but they can be recorded and detected. Some of the code in the IR Library let you even record the keycodes and send them out with a IR emitter, so you can build your own Arduino universal remote.

Here is my final code for one of the projector remotes, which controls four different leds. I didn’t manage to make a toggle work, setting led on if it was off, and vice versa, I don’t know why it did not function, so the code is a bit bulky, sorry for that.

/*
 * IRremote: IRrecvDemo - demonstrates receiving IR codes with IRrecv
 * An IR detector/demodulator must be connected to the input RECV_PIN.
 * Version 0.1 July, 2009
 * Copyright 2009 Ken Shirriff
 * http://arcfn.com
 */

#include <IRremote.h>

int ledleft = 7;
int ledup = 6;
int ledright = 5;
int leddown = 4;

int RECV_PIN = 11;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
  pinMode(ledleft, OUTPUT);
  pinMode(ledup, OUTPUT);
  pinMode(ledright, OUTPUT);
  pinMode(leddown, OUTPUT); 

  digitalWrite(ledleft, LOW);
  digitalWrite(ledup, LOW);
  digitalWrite(ledright, LOW);
  digitalWrite(leddown, LOW); 

  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
}

void loop() {
  if (irrecv.decode(&results)) {
    switch (results.value) {

      //left
    case 0xA818A1A0:
      Serial.println("left");
      digitalWrite(ledleft, HIGH);
      digitalWrite(ledup, LOW);
      digitalWrite(ledright, LOW);
      digitalWrite(leddown, LOW);
      break;

 //left another
    case 0xE17240BF:
      Serial.println("left");
      digitalWrite(ledleft, HIGH);
      digitalWrite(ledup, LOW);
      digitalWrite(ledright, LOW);
      digitalWrite(leddown, LOW); 

      break;

//right
    case 0xD6087DC0:
      Serial.println("right");
      digitalWrite(ledleft, LOW);
      digitalWrite(ledup, LOW);
      digitalWrite(ledright, HIGH);
      digitalWrite(leddown, LOW);
      break;   

//right another version
    case 0xE1724CB3:
      Serial.println("right");
      digitalWrite(ledleft, LOW);
      digitalWrite(ledup, LOW);
      digitalWrite(ledright, HIGH);
      digitalWrite(leddown, LOW);
      break;

//up
    case 0x7DEB34DE:
      Serial.println("up");
      digitalWrite(ledleft, LOW);
      digitalWrite(ledup, HIGH);
      digitalWrite(ledright, LOW);
      digitalWrite(leddown, LOW);
      break;

//up again
    case 0xE172C837:
      Serial.println("up");
      digitalWrite(ledleft, LOW);
      digitalWrite(ledup, HIGH);
      digitalWrite(ledright, LOW);
      digitalWrite(leddown, LOW);
      break;

// down
    case 0xE17228D7:
      Serial.println("down");
      digitalWrite(ledleft, LOW);
      digitalWrite(ledup, LOW);
      digitalWrite(ledright, LOW);
      digitalWrite(leddown, HIGH);
      break;

//killaallllll
    case 0xE1728C73:
      Serial.println("shutdown - blank");
      digitalWrite(ledleft, LOW);
      digitalWrite(ledup, LOW);
      digitalWrite(ledright, LOW);
      digitalWrite(leddown, LOW);
      break;

    default:
      Serial.println(results.value, HEX);
    }

    irrecv.resume(); // Receive the next value
  }
}