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鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add IRremote to lib_deps in PlatformIO. Mokxi follows the calls of release 4.6.0, so a sketch written here builds there too.

What to know in the simulator: A send pin wired to an irreceiver鈥檚 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.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

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鈥檚 command number. decodedIRData.address is the remote鈥檚 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鈥檚 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鈥檚 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鈥檚 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.