advancedChapter 41 of 4545 min

41. Project: IR Remote LED

Project: control LED with IR remote; outcome: one button turns the LED on, another turns it off; serial prints each button's command and the action.

This lesson uses: Arduino UNO R3 board, TinkerBlock TK15 IR Receiver module, TinkerBlock TK01 XL LED (or TK02), jumper wires (or breadboard). Project: control LED with IR remote; outcome: one button on the remote turns the LED on, another turns it off; serial prints each button's command and the action.

1. Project goal

Build an IR remote LED system:

  • Receive IR remote signals
  • Control LED state based on different keys

2. Wiring

  • TK15 IR Receiver: GND → GND, VCC → 5V, SIGNAL → D3
  • TK01 LED: GND → GND, SIGNAL → D13

3. Program structure

Same as last lesson: two fixed blocks setup() and loop().

  • setup(): Include library, create IR object, init pins, start serial; runs once.
  • loop(): Keep repeating “receive IR → decide key → control LED → wait → repeat”.

Point the remote at the receiver and press a key to turn the LED on or off; serial prints each button's command so you can put your remote's commands into the code.

4. Code to write

Type the code below at the top of the file and in setup() and loop() (do not copy-paste; typing it yourself helps you remember). It needs IRremote version 4 or later.

// IR remote LED: one button = on, another = off; print each command
#include <IRremote.hpp>
#define IR_PIN 3    // TK15 SIGNAL on D3
#define LED_PIN 13  // TK01 SIGNAL on D13

// REPLACE both with commands your own remote prints (see step 5)
#define ON_COMMAND  0x16
#define OFF_COMMAND 0x19

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  IrReceiver.begin(IR_PIN, DISABLE_LED_FEEDBACK);   // Start receiving
  Serial.println("IR remote LED started");
}

void loop() {
  if (IrReceiver.decode()) {                // A whole frame has arrived
    IRData &d = IrReceiver.decodedIRData;

    if (!(d.flags & IRDATA_FLAGS_IS_REPEAT)) {  // Ignore a held button's repeats
      Serial.print("command=0x");
      Serial.println(d.command, HEX);

      if (d.command == ON_COMMAND) {
        digitalWrite(LED_PIN, HIGH);
        Serial.println("LED on");
      } else if (d.command == OFF_COMMAND) {
        digitalWrite(LED_PIN, LOW);
        Serial.println("LED off");
      }
    }

    IrReceiver.resume();                    // Ready for the next frame
  }
}

Program notes

Overall idea: When IrReceiver.decode() is true, decodedIRData.command says which button was pressed. Compare it with the two commands you chose, switch the LED, then call IrReceiver.resume() for the next frame. A held button also sends repeat frames with no command in them; the sketch skips those.

The commands belong to your remote, not to the library: 0x16 and 0x19 are placeholders. Read your own buttons' commands on the Serial Monitor first, then put them in ON_COMMAND and OFF_COMMAND.

CodeIn this lesson
decodedIRData.commandWhich button: compare it with your two commands
IRDATA_FLAGS_IS_REPEATSet on a held button's repeat frames, which carry no command
IrReceiver.resume()Must be called after each frame to keep receiving

5. Hands-on

  1. After entering the code, click Verify (✓) to compile
  2. Click Upload (→) to upload to the board
  3. Open Serial Monitor (Tools → Serial Monitor, baud 9600)
  4. Point remote at TK15:
    • Press different keys and watch the command=0x.. lines on serial
    • Put the two you want into ON_COMMAND and OFF_COMMAND, and upload again
    • Test LED on/off
  5. Try yourself: add more keys, e.g. control RGB LED colors

Expected result: IR remote controls LED on/off.

Proceed to Lesson 42.

Edit this page — content/guides/tinkerblock-workshop/41-project-ir-remote-led.mdx

Community

Questions about this product

See what other owners have asked, and read their solutions.

This page covers several products. Choose yours to see the right discussions.

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Projects and guides on the forum