Arduino library · Input

IRremote: read and send TV remote codes

IRremote is the standard Arduino library for infrared remote controls. With a 38 kHz receiver such as the VS1838B or a KY-022 module on a digital pin, it decodes the button presses of almost any remote, NEC, Samsung, Sony, RC5, RC6, LG, JVC, Denon and more, into a protocol, an address and a command number. With an IR LED it sends them back.

Built in on Mokxi: #include <IRremote.h> and it builds, with nothing to install. Free, and no account needed.

What IRremote does

Version 3 changed the interface, and many tutorials still show the old one. The current code uses a global IrReceiver object: IrReceiver.begin(pin), then in the loop IrReceiver.decode() to check for a frame, IrReceiver.decodedIRData.command to read the button, and IrReceiver.resume() to listen for the next. The old IRrecv irrecv(pin) and decode(&results) style is still accepted.

A cheap kit remote sends NEC codes: the address identifies the remote and the command identifies the button, so a sketch usually switches on the command. Holding a button sends repeat frames, flagged in decodedIRData.flags, which you can ignore or use for volume-style controls.

Library Manager nameIRremote
AuthorArmin Joachimsmeyer, Ken Shirriff and others
Include#include <IRremote.h>
#include <IRremote.hpp>
#include <IRremoteInt.h>
#include <TinyIRReceiver.hpp>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesInfrared receiver, VS1838B, Infrared remote, 21 buttons

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <IRremote.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "IRremote" by Armin Joachimsmeyer, Ken Shirriff and others from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add IRremote to lib_deps in PlatformIO. Mokxi follows the calls of release 4.7.1, so a sketch written here builds there too.

What to know in the simulator: A send pin wired to an irreceiver’s IR pin is the beam, so a board can send to itself or to another; the handset part sends NEC. Off the AVR boards the end of a frame is noticed when the sketch next asks, so a receive-complete callback runs then. On the Uno R4 decode() reads a frame it sees starting rather than in the background. Bang & Olufsen needs RECORD_GAP_MICROS of 16 ms, as upstream.

The calls you will use

In the order a sketch meets them.

IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK)

Starts receiving on a pin and blinks the built-in LED with each frame.

IrReceiver.decode()

True when a frame has been decoded and is waiting.

if (IrReceiver.decode()) {
  Serial.println(IrReceiver.decodedIRData.command, HEX);
  IrReceiver.resume();
}

IrReceiver.decodedIRData.command

The button’s command number. decodedIRData.address is the remote’s address and decodedIRData.protocol the protocol.

IrReceiver.printIRResultShort(&Serial)

Prints the protocol, address and command in one line, the quickest way to learn a remote’s codes.

IrReceiver.resume()

Gets ready for the next frame. Without it, no further button is decoded.

IrSender.begin(3)

Starts sending on the IR LED’s pin.

IrSender.sendNEC(0x00, 0x45, 0)

Sends an NEC frame with an address, a command and a number of repeats.

Example: Read the kit remote

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3
#include <IRremote.hpp>

#define IR_RECEIVE_PIN 11

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
  Serial.println("Press a button on the remote");
}

void loop() {
  if (IrReceiver.decode()) {
    if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) {
      Serial.println("Repeat");
    } else {
      IrReceiver.printIRResultShort(&Serial);
      Serial.print("Command: 0x");
      Serial.println(IrReceiver.decodedIRData.command, HEX);
    }
    IrReceiver.resume();
  }
}

Wiring

  • Arduino Uno R3 11 to Infrared receiver, VS1838B OUT
  • Arduino Uno R3 5V to Infrared receiver, VS1838B VCC
  • Arduino Uno R3 GND to Infrared receiver, VS1838B GND
  • Infrared remote, 21 buttons IR to Infrared receiver, VS1838B IR

Open it in the editor, wired and ready to run

Common errors and how to fix them

'decode_results' does not name a type, or the old example will not build

A 2.x tutorial is being built against a newer library in your IDE. Either switch to the IrReceiver style shown here, or check that the include is IRremote.hpp or IRremote.h and that only one IRremote library is installed.

Only the first button press is seen

IrReceiver.resume() is missing after reading the frame. Call it once you have handled each decode.

Every press reads as 0 or the same code

Those are repeat frames from a held button, or the protocol is UNKNOWN. Print printIRResultShort() to see the protocol, press briefly, and skip frames whose flags include IRDATA_FLAGS_IS_REPEAT if you only want new presses.

Nothing is received at all

Check the receiver’s pinout, which differs between bare sensors and modules, and that the pin in begin() is the signal pin. Bright sunlight and some LED bulbs swamp IR receivers; test in the shade.

Try IRremote without installing anything

Open the example, change the sketch, and keep your version in a free account.