All articles

    Infrared (IR) Remotes: Receiving and Sending with an Arduino

    How a TV remote works: the 38 kHz carrier and the shape of an NEC message, the popular protocols, the VS1838B receiver, reading any remote's codes with the IRremote library, held buttons, a relay control project, sending codes with an IR LED and calculating its resistor, air conditioners, timer conflicts, and the causes of poor range.

    Updated 5 min read

    Any old remote in the house can become a controller for your project: switch on an LED, move a motor, or change a mode. And the other way round, the Arduino can imitate the remote and switch on the air conditioner or TV.

    The remote sends pulses at 38 kHz, and the receiver decodes the code
    The remote sends pulses at 38 kHz, and the receiver decodes the code

    How does it work?

    • The remote contains an IR LED (light we can't see).
    • The LED flashes 38,000 times a second, and that flashing is switched on and off in a particular pattern. That pattern is the button's code.
    • The 38 kHz makes the receiver ignore light from lamps and the sun and focus only on the remote.
    • The most common protocol in cheap remotes is NEC: the device address + the command code.

    A quick test: look at the remote's LED through your phone camera and press a button. Many cameras show a purple light.

    The shape of the message

    An NEC message: a long 9 ms pulse, a 4.5 ms gap, then the address and command, with each bit identified by the length of its gap
    An NEC message: a long 9 ms pulse, a 4.5 ms gap, then the address and command, with each bit identified by the length of its gap

    In NEC, the message starts with a long pulse that says "attention", then 32 bits: the address, its inverse, the command, and its inverse. The inverse is a check: if the command and its inverse don't match, the message is corrupted and is ignored. And when you hold a button down, the remote doesn't repeat the whole code; it sends a short repeat message.

    Other protocols

    ProtocolUsually used by
    NECCheap remotes and many devices
    RC5 / RC6Philips devices and some European ones
    SIRCSony devices
    SamsungSamsung TVs

    The library recognises most of them by itself and tells you the protocol's name.

    The receiver

    VS1838B or TSOP38238: 3 pins (OUT, GND and VCC). The pin order varies between types, so check the datasheet before wiring. It outputs the code ready-made (without the 38 kHz). Many ready-made modules have an LED that blinks on reception.

    The receiver's output is HIGH when idle and goes LOW with each pulse, so the shape in the diagram above is exactly what you'd see with an oscilloscope on it.

    Reading the codes (the IRremote library)

    #include <IRremote.hpp>
    
    void setup() {
      Serial.begin(9600);
      IrReceiver.begin(2);
    }
    
    void loop() {
      if (IrReceiver.decode()) {
        Serial.println(IrReceiver.decodedIRData.command, HEX);
        IrReceiver.resume();
      }
    }

    Press each button and note its code, then use it in an if or switch. This library changed between old and new versions, so if you find old code with irrecv.decode(&results), that's the old form.

    And if you want to know everything about the message (protocol and address), use IrReceiver.printIRResultShort(&Serial); instead of the print line.

    Held buttons

    bool rep = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;

    For a volume button, for example: if rep is true, keep increasing. For the power button: ignore repeats, so a single press doesn't toggle the state several times.

    Project: a relay by remote

    #include <IRremote.hpp>
    const int RELAY = 7;
    bool on = false;
    
    void setup() { pinMode(RELAY, OUTPUT); IrReceiver.begin(2); }
    
    void loop() {
      if (!IrReceiver.decode()) return;
      bool rep = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
      if (!rep && IrReceiver.decodedIRData.command == 0x45) {  // your button's code
        on = !on;
        digitalWrite(RELAY, on);
      }
      IrReceiver.resume();
    }

    Change 0x45 to the code you got from your remote's button. The relay itself is in the relay module article, and if you'll switch something on 220 V, read mains safety.

    Sending

    • An IR LED (940 nm) with a resistor.
    • For longer range, drive it with a transistor at higher current (the pulses are short, so the LED can take more than its continuous current, as per the datasheet).
    • The library generates the 38 kHz and the timings; you send the address and command.
    #include <IRremote.hpp>
    void setup() { IrSender.begin(3); }        // pin 3 on the Uno
    void loop() {
      IrSender.sendNEC(0x00, 0x45, 0);         // address, command, repeat count
      delay(2000);
    }

    The resistor with a transistor: the LED drops about 1.3 V, the transistor about 0.2, and for 100 mA at 5 V: (5 − 1.3 − 0.2) ÷ 0.1 ≈ 35 Ω (use 33). That's acceptable because the LED only runs in short pulses. Work it out with the Ohm's law calculator.

    Air conditioners usually send a long message containing all the settings (temperature, mode and fan), not just a button, so they need a library specific to the AC brand. With the ESP32, the IRremoteESP8266 library supports many AC brands, so you can build an AC controlled from a web page.

    Conflicts with other libraries

    The IRremote library uses a timer inside the Uno. This can break analogWrite on certain pins (3 and 11), or conflict with tone() or other libraries using the same timer. If something stops working suddenly after you add IR, that's probably why: change the pin or the timer in the library's settings.

    Poor range or wrong codes?

    • Sunlight and old energy-saving lamps cause interference.
    • The receiver needs a small capacitor on its supply (for example 100 nF). See decoupling capacitors.
    • Remotes at a different frequency (36 or 40 kHz) perform worse with a 38 kHz receiver.
    • IR needs a line of sight or a reflection off a wall; it doesn't pass through walls.
    ProblemUsual cause
    No reading at allThe receiver's pin order is wrong
    A different code every timeA remote with an unsupported protocol, or interference
    The code repeats with one pressYou're not ignoring repeat messages
    Sending doesn't operate the deviceWrong protocol or address, or a weak LED
    PWM stopped after adding IRA timer conflict

    FAQ

    Can one remote control several projects? Yes, each project responds only to its own codes. And if two remotes produce the same code, choose different buttons.

    Can I operate the remote from my phone? If the phone has an IR LED, yes. Otherwise, an ESP32 + an IR LED can do it from a web page.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.