We’re going to use remote control keys to adjust the brightness of an LED.

The IR Decoder IC will demodulate the signals from the IR remote control and the IR Library for Arduino will decode the signals.

Let’s get started.

The IR Receiver Module is an IC that Senses Infra-Red Signals from a Remote Control and Demodulates the Signal onto the Output pin.

The connections are as follows:

IRRx

Adafruit has an excellent tutorial on IR Receivers HERE.

But we’re going to make use of a Library to simplify things a bit. We’re using a Multi-Protocol Infrared Remote Library for the Arduino.

Download the Library from Github, extract and add to Libraries in the IDE (Sketch /> Import Library -> Add Library)   : IDE 1.0.5  .

To start with, connect the IR Sensor as follows:

Vcc   ->  +5V

Gnd  -> Gnd

Vout -> D2

Add a 10k Ohm Pullup resistor to D2 or enable the internal Pullup and an LED/Resistor as shown below.

IRRx_Fade_bb

The code below goes through a calibration sequence for keys Zero to Four. Then those keys will control the brightness of the LED.

Here’s the code:

/*
  RemoteDecode sketch
 Infrared remote control signals are decoded to control LED brightness
 The values for keys 0 through 4 are detected and stored when the sketch starts
 key 0 turns the LED off, the brightness increases in steps with keys 1 through 4
 */

#include <IRremote.h>            // IR remote control library

const int irReceivePin = 2;     // pin connected to IR detector output
const int ledPin       = 9;     // LED is connected to a PWM pin

const int numberOfKeys = 5;     //  5 keys are learned (0 through 4)
long irKeyCodes[numberOfKeys];  // holds the codes for each key

IRrecv irrecv(irReceivePin);    // create the IR library
decode_results results;         // IR data goes here

void setup()
{
  Serial.begin(9600);
  pinMode(irReceivePin, INPUT);
  pinMode(ledPin, OUTPUT);
  irrecv.enableIRIn();              // Start the IR receiver
  learnKeycodes();                  // learn remote control key  codes
  Serial.println("Press a remote key");
}

void loop()
{
  long key;
  int  brightness;

  if (irrecv.decode(&results))
  {
    // here if data is received
    irrecv.resume();
    key = convertCodeToKey(results.value);
    if(key >= 0)
    {
      Serial.print("Got key ");
      Serial.println(key);
      brightness = map(key, 0,numberOfKeys-1, 0, 255);
      analogWrite(ledPin, brightness);
    }
  }
}

/*
 * get remote control codes
 */
void learnKeycodes()
{
  while(irrecv.decode(&results))   // empty the buffer
    irrecv.resume();

  Serial.println("Ready to learn remote codes");
  long prevValue = -1;
  int i=0;
  while( i < numberOfKeys)
  {
    Serial.print("press remote key ");
    Serial.print(i);
    while(true)
    {
      if( irrecv.decode(&results) )
      {
          if(results.value != -1 && results.value != prevValue)
          {
            showReceivedData();
            irKeyCodes[i] = results.value;
            i = i + 1;
            prevValue = results.value;
            irrecv.resume(); // Receive the next value
            break;
          }
        irrecv.resume(); // Receive the next value
      }
    }
  }
  Serial.println("Learning complete");
}

/*
 * converts a remote protocol code to a logical key code
 * (or -1 if no digit received)
 */
int convertCodeToKey(long code)
{
  for( int i=0; i < numberOfKeys; i++)
  {
    if( code == irKeyCodes[i])
    {
      return i; // found the key so return it
    }
  }
  return -1;
}

/*
 * display the protocol type and value
 */
void showReceivedData()
{
  if (results.decode_type == UNKNOWN)
  {
    Serial.println("-Could not decode message");
  }
  else
  {
    if (results.decode_type == NEC) {
      Serial.print("- decoded NEC: ");
    }
    else if (results.decode_type == SONY) {
      Serial.print("- decoded SONY: ");
    }
    else if (results.decode_type == RC5) {
      Serial.print("- decoded RC5: ");
    }
    else if (results.decode_type == RC6) {
      Serial.print("- decoded RC6: ");
    }
    Serial.print("hex value = ");
    Serial.println( results.value, HEX);
  }
}

Open the Serial Monitor and follow the instructions to Learn the codes for keys Zero to Four.

Once learned, the keys will control the brightness of the LED!

Make it Better:

  1. Make the remote control light one of 4 LEDs when you press keys 1,2,3 or 4

Leave a Reply

Your email address will not be published. Required fields are marked *