CYTHEIA AIOpen the editor

IR Remote LED Control

Click keys 1, 2 and 3 on the infrared remote to switch three LEDs on and off, the way a TV remote does it. Play turns them all off.

Open this circuit and run it

Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.

How it works

Infrared remotes flash an invisible LED in a pattern that encodes a number, a different one for each key. The receiver module (OUT on pin 11) decodes that and hands the Arduino the number. Three LEDs with resistors, on pins 8, 9 and 10, are the outputs. In the editor the remote is a part you can click, standing in for a real handset.

The IRremote library does the decoding: irrecv.enableIRIn() starts listening, and irrecv.decode(&results) is true when a code has arrived, which is left in results.value. The sketch prints every code in hexadecimal so you can discover the codes of other keys, then compares it with the codes for keys 1, 2 and 3 (0xFF30CF, 0xFF18E7, 0xFF7A85). Each of those toggles its own LED through setLed(), which remembers every LED’s state in the ledOn array. The Play key (0xFFC23D) turns all three off. irrecv.resume() readies the receiver for the next key.

Parts

Wiring

Arduino code

// IR Remote LED Control
// The remote has no wires: click a key on it and the receiver hears it, just
// like a real remote. Each key sends its own code; the sketch compares the
// code it receives with the codes of keys 1, 2 and 3 (and Play) and acts.
// Open the Serial Monitor and press other keys to see their codes.
#include <IRremote.h>

const int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;

const int ledPins[] = {8, 9, 10};
bool ledOn[] = {false, false, false};

void setLed(int which, bool on) {
  ledOn[which] = on;
  digitalWrite(ledPins[which], on ? HIGH : LOW);
}

void setup() {
  Serial.begin(9600);
  irrecv.enableIRIn();
  for (int i = 0; i < 3; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
  Serial.println("Press 1, 2 or 3 on the remote (Play turns all off)");
}

void loop() {
  if (irrecv.decode(&results)) {
    Serial.print("Code: ");
    Serial.println(results.value, HEX);

    if (results.value == 0xFF30CF) {
      setLed(0, !ledOn[0]);       // key 1
    } else if (results.value == 0xFF18E7) {
      setLed(1, !ledOn[1]);       // key 2
    } else if (results.value == 0xFF7A85) {
      setLed(2, !ledOn[2]);       // key 3
    } else if (results.value == 0xFFC23D) {
      for (int i = 0; i < 3; i++) {   // Play: everything off
        setLed(i, false);
      }
    }
    irrecv.resume();
  }
}

Try changing it

Open this circuit and run it

Next challenge

More projects in Input & Controls