Control your project like a TV. Each button on the infrared remote sends its own invisible code. In this project the UNO learns your remote's codes and uses buttons 1, 2 and 3 to switch a red, a yellow and a green LED, plus one button to turn everything off.
What you need
- UNO R3 board and USB cable
- IR remote control and IR receiver module
- Red, yellow and green LEDs
- 3 resistors, 220 Ω
- Breadboard and jumper wires (all from the UNO R3 Starter Kit)
Wiring

| From | To |
|---|---|
| IR module S (signal) | UNO pin 2 |
| IR module + (middle) | 5V (breadboard + rail) |
| IR module − | GND (breadboard − rail) |
| UNO pin 4 | 220 Ω resistor, then the red LED's long leg |
| UNO pin 5 | 220 Ω resistor, then the yellow LED's long leg |
| UNO pin 6 | 220 Ω resistor, then the green LED's long leg |
| All LEDs' short legs | GND (breadboard − rail) |
Check the labels printed on your IR module. Some modules order the pins differently.
Build it step by step
- Install the IRremote library (by shirriff, z3t0 and ArminJo, version 4 or newer).
- Wire the IR module and the three LEDs as in the diagram.
- Upload the code and open the Serial Monitor at 9600 baud.
- Point the remote at the receiver and press 1, 2, 3 and the button you want for "all off". Note the code shown for each one.
- Put your codes into
BTN_RED,BTN_YELLOW,BTN_GREENandBTN_OFF, then upload again. - Press the buttons to switch the lights.
The code
#include <IRremote.hpp>
const int IR_PIN = 2;
const int RED_LED = 4;
const int YELLOW_LED = 5;
const int GREEN_LED = 6;
// Replace these with the codes your remote shows in the Serial Monitor
const uint8_t BTN_RED = 0x0C;
const uint8_t BTN_YELLOW = 0x18;
const uint8_t BTN_GREEN = 0x5E;
const uint8_t BTN_OFF = 0x16;
void toggle(int pin) {
digitalWrite(pin, !digitalRead(pin));
}
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_PIN, ENABLE_LED_FEEDBACK);
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
Serial.println("Press a button on the remote");
}
void loop() {
if (!IrReceiver.decode()) return;
uint8_t command = IrReceiver.decodedIRData.command;
bool isRepeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
IrReceiver.resume(); // ready for the next code
if (isRepeat) return; // ignore the repeat signal while a button is held
Serial.print("Button code: 0x");
Serial.println(command, HEX);
if (command == BTN_RED) toggle(RED_LED);
else if (command == BTN_YELLOW) toggle(YELLOW_LED);
else if (command == BTN_GREEN) toggle(GREEN_LED);
else if (command == BTN_OFF) {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, LOW);
}
}
How it works
The remote flashes an infrared LED in a fast pattern that our eyes can't see. The receiver module turns that light into pulses on its signal pin, and the IRremote library decodes the pulses into a command number for each button. toggle() reads the LED's current state and sets the opposite, so the same button switches it on and off. Holding a button sends "repeat" signals, which the sketch ignores.
Make it your own
- Use the arrow buttons to dim an LED up and down with
analogWrite()on a PWM pin. - Control the RGB LED and give each button its own colour.
- Combine this with the servo lesson to move a servo from the remote.
Troubleshooting
- No codes appear: check the S wire on pin 2, and make sure nothing blocks the receiver.
- Different codes every time: point the remote straight at the receiver, from about 1 m.
- Compile errors: make sure IRremote version 4 or newer is installed.
Learn the basics first
New to these parts? These lessons explain them step by step: Set up the Arduino IDE and blink an LED and Make a tilt alarm with a buzzer.
0 comments