IRremoteESP8266: IR receive and send on the ESP8266 and ESP32
IRremoteESP8266, led by David Conran, is the infrared library written for Espressif chips. The Arduino IRremote library long had trouble with the ESP8266’s timers, so this one grew up separately and now decodes and sends dozens of protocols, plus full air conditioner states for many brands. Its code values are 64-bit, which is why it comes with helpers to print them.
Built in on Mokxi: #include <IRremoteESP8266.h> and it builds, with nothing to install. Free, and no account needed.
What IRremoteESP8266 does
Receiving follows the classic pattern. An IRrecv on the receiver’s data pin, enableIRIn() to start capturing, and in loop() decode(&results) returns true when a whole signal has arrived. results.value holds the code, results.decode_type the protocol, and resultToHumanReadableBasic() formats both. resume() readies the receiver for the next one. Sending is an IRsend on an LED pin with begin() and a protocol call such as sendNEC().
In Mokxi the IRrecvDemo example runs on an ESP32-C3 with the simulated IR receiver and the kit remote: press a button and its NEC code prints, followed by repeat frames while held. NEC is the only protocol decoded here; other signals report UNKNOWN with a hash of their timings, and the air conditioner classes are not included.
| Library Manager name | IRremoteESP8266 |
|---|---|
| Author | David Conran |
| Include | #include <IRremoteESP8266.h>#include <IRrecv.h>#include <IRsend.h>#include <IRutils.h> |
| Boards | every board Mokxi compiles for but ATtiny85 |
| In the simulator | Runs, with a gap the note below names. |
| Parts it drives | Infrared 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 <IRremoteESP8266.h> and the sketch compiles, with no download, no zip and no Library Manager.
On a real board, install "IRremoteESP8266" by David Conran from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add IRremoteESP8266 to lib_deps in PlatformIO. Mokxi follows the calls of release 2.9.0, so a sketch written here builds there too.
What to know in the simulator: NEC is the one protocol decoded; other signals are UNKNOWN with a hash of their timings. The air-conditioner classes are not here.
- ESP32-C3
- Arduino Uno
- Arduino Nano
- Arduino Mega 2560
- Arduino Leonardo
- SparkFun Pro Micro
- Arduino Micro
- ESP32-C6
- WCH CH32V003
- 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.
IRrecv irrecv(kRecvPin)
Creates the receiver on the pin wired to the IR module’s output. On an ESP8266, D5 (GPIO 14) is a common choice.
irrecv.enableIRIn()
Starts capturing in the background with a timer interrupt. Call it once in setup().
irrecv.decode(&results)
Returns true when a complete signal is ready in results, a decode_results you declared globally.
serialPrintUint64(results.value, HEX)
Prints the 64-bit code in hex. Serial.print() alone cannot print a 64-bit number on these cores.
resultToHumanReadableBasic(&results)
A String with the protocol name, the code and its bit count, ready to print.
irrecv.resume()
Clears the last capture so the next signal can be received. Call it after handling each code.
if (irrecv.decode(&results)) {
if (results.value == 0xFFA25D) toggleLamp();
irrecv.resume();
}IRsend irsend(kIrLed)
A transmitter on the pin driving the IR LED, usually through a transistor. Call irsend.begin() in setup().
irsend.sendNEC(0x00FFE01FUL)
Sends a 32-bit NEC code, the protocol most cheap remotes use. Other protocols have their own send calls.
Example: IRrecvDemo: the kit remote on an ESP32-C3
On the ESP32-C3-DevKitM-1. It runs in the browser exactly as printed.
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
const uint16_t kRecvPin = 2;
IRrecv irrecv(kRecvPin);
decode_results results;
void setup() {
Serial.begin(115200);
irrecv.enableIRIn();
Serial.println("IRrecvDemo is now running and waiting for IR message");
}
void loop() {
if (irrecv.decode(&results)) {
serialPrintUint64(results.value, HEX);
Serial.println("");
Serial.print(resultToHumanReadableBasic(&results));
irrecv.resume();
}
}Wiring
- ESP32-C3-DevKitM-1 2 to Infrared receiver, VS1838B OUT
- ESP32-C3-DevKitM-1 3V3 to Infrared receiver, VS1838B VCC
- ESP32-C3-DevKitM-1 GND to Infrared receiver, VS1838B GND
- Infrared remote, 21 buttons IR to Infrared receiver, VS1838B IR
Common errors and how to fix them
call of overloaded 'print(uint64_t, int)' is ambiguous
results.value is 64 bits wide, and the ESP8266 Print class has no overload for that. Use serialPrintUint64(results.value, HEX), or cast to uint32_t when you know the code fits in 32 bits, as NEC codes do.
Errors about IRrecv or decode_results being defined twice
IRremote and IRremoteESP8266 are both included or installed in a way that clashes, and they use the same class names. Use one library per sketch; on an ESP board, this one, and remove the other include.
Nothing decodes, or the board will not boot with the receiver attached
Check the pin: on the ESP8266, GPIO 0, 2 and 15 decide the boot mode, and a receiver output there can stop the board starting. Use D5 or D6, power the module from 3.3V, and make sure enableIRIn() was called.
Questions and answers
IRremoteESP8266 or IRremote on an ESP32?
Recent IRremote versions do run on the ESP32. IRremoteESP8266 covers far more protocols and air conditioners and is the usual choice for ESP8266 projects. For a basic NEC remote, either works.
Keep going
Try IRremoteESP8266 without installing anything
Open the example, change the sketch, and keep your version in a free account.