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.
- Intermediate
- Input & Controls
- 10 parts
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
- Arduino Uno
- IR Remote Receiver
- IR Remote (21-key)
- 3 × Resistor
- 3 × LED
- GND
Wiring
- Arduino Uno 5V → IR Remote Receiver VCC
- IR Remote Receiver OUT → Arduino Uno D11
- Arduino Uno GND → IR Remote Receiver GND
- Arduino Uno D8 → Resistor 1 A
- Resistor 1 B → LED 1 Anode (+)
- LED 1 Cathode (-) → GND
- Arduino Uno D9 → Resistor 2 A
- Resistor 2 B → LED 2 Anode (+)
- LED 2 Cathode (-) → GND
- Arduino Uno D10 → Resistor 3 A
- Resistor 3 B → LED 3 Anode (+)
- LED 3 Cathode (-) → GND
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
- Make Play turn the LEDs all on instead: in the Play branch change setLed(i, false) to setLed(i, true).
- Find the code of another key: press it, read “Code: …” in the Serial tab, then add an else if branch that compares results.value with that code.
Next challenge
More projects in Input & Controls
- Button Controlled LEDPress a button to turn on an LED.
- Potentiometer ReadingRead a potentiometer and display its value on the serial monitor.
- Buzzer AlarmPress and hold the button to sound the alarm buzzer.
- Interrupt Button CounterAn interrupt toggles the LED the instant you press the button, even while loop() is busy…