IR remote light switch

Use the infrared remote from your kit to switch three LEDs on and off from across the room.
  • Level:Beginner
  • Duration: 45 min
  • Ages 10+
  • Platform: Arduino
Community project EEMENTOR team
IR remote light switch – EEMENTOR STEM™ project

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

Breadboard wiring diagram: IR remote light switch
Breadboard view. Check each connection against the wiring table below.
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

  1. Install the IRremote library (by shirriff, z3t0 and ArminJo, version 4 or newer).
  2. Wire the IR module and the three LEDs as in the diagram.
  3. Upload the code and open the Serial Monitor at 9600 baud.
  4. 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.
  5. Put your codes into BTN_RED, BTN_YELLOW, BTN_GREEN and BTN_OFF, then upload again.
  6. 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

Leave a comment

Please note, comments need to be approved before they are published.

Build it yourself

Get the parts used in this project, tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the parts

More projects

  • Pulse visualiser

    • Level:Intermediate
    • Duration: 45 min
    • Platform: Arduino

    by EEMENTOR team

  • Flame alarm

    • Level:Beginner
    • Duration: 40 min
    • Platform: Arduino

    by EEMENTOR team

Community projects

Built something awesome? Share it.

Send us your project and we'll feature it in the hub, credited to you, so other makers can learn from it.

  1. Sign in and tell us about your build
  2. Our team reviews your submission
  3. We publish it in the hub, credited to you