ESP32 · IR Receiver

ESP32 IR Receiver Simulator: Wiring, Code and a Live Circuit

This is a 38 kHz IR receiver wired to an ESP32 DevKit with a remote control beside it, decoded live with the IRremote library: open it in the editor, press a button on the remote, and the serial monitor prints its command code.

Three wires to the receiver: power, GND, and OUT to GPIO 4 (D4). The remote speaks NEC, the protocol of most cheap kit remotes.

  • 3.3 V logic
  • Library: IRremote.hpp
  • OUT on GPIO 4 (D4)
  • NEC protocol, 38 kHz
ESP32 · IR Receiverlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.

Wiring the IR Receiver to the ESP32

Every connection in the circuit above, with the board’s own pin names.

IR Receiver pinESP32 pinWhy
VCC3V3The VS1838B-style receiver runs from 2.7 to 5.5 V.
GNDGNDGround.
OUTGPIO 4 (D4)Low while a 38 kHz burst arrives, high otherwise: the demodulated code.
IR (light)The remoteIn the editor the infrared light path is drawn as a gray wire from the remote.

Power the receiver from 3V3 on the ESP32: its output then swings to 3.3 V, the board’s own level. Fed from 5 V, its output would idle at 5 V on a 3.3 V pin.

Pins to leave alone on the ESP32

GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.

On a real bench

The thirty-pin DevKit straddles a breadboard with one row of holes free on each side, which is where the IR Receiver’s jumpers go. Many ESP32 boards are wider and cover both rows; then use two breadboards side by side. Hold BOOT while uploading if the IDE cannot connect.

The code

The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.

IR receiver on ESP32 GPIO 4 (D4)
#include <IRremote.hpp>

const int IR_PIN = 4;   // OUT, GPIO 4 (D4)

void setup() {
  Serial.begin(115200);
  IrReceiver.begin(IR_PIN);
  Serial.println("Point the remote and press a button");
}

void loop() {
  if (IrReceiver.decode()) {
    if (!(IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT)) {
      Serial.print("Command: 0x");
      Serial.println(IrReceiver.decodedIRData.command, HEX);
    }
    IrReceiver.resume();   // ready for the next one
  }
}

IrReceiver.begin(IR_PIN) starts the IRremote library listening on the pin. decode() returns true once a whole code has arrived, and decodedIRData.command holds the button’s command byte, 0x47 for the button pressed in this demo.

Holding a button makes an NEC remote send short repeat frames, which the library flags with IRDATA_FLAGS_IS_REPEAT. The sketch prints only the first frame of each press. resume() must be called after each code or nothing more is decoded.

Serial.begin(115200) opens the serial monitor. Serial is UART0 on the USB bridge. ESP32 sketches usually run it at 115200 baud, which this one does.

Common mistakes with the IR Receiver on the ESP32

Old IRremote code

Sketches written for IRremote 2.x use IRrecv irrecv(pin), irrecv.enableIRIn() and results.value. Version 3 and later use IrReceiver and decodedIRData, as here. Mixing the two is the most common reason an IR sketch will not compile.

Forgetting resume()

After decode() returns true, the library holds the code until resume() is called. Leave it out and the first button press is the only one you ever see.

Putting the receiver output on an input-only pin

GPIO 34, 35, 36 and 39 on the ESP32 have no output driver at all. pinMode() to OUTPUT on one of them does nothing and the line never moves, which looks exactly like a dead part. GPIO 4 (D4) is a safe choice; so are GPIO 13, 14, 16 to 19, 23, 25 to 27 and 32 to 33.

Questions and answers

Can I simulate an IR remote with an ESP32 DevKit?

Yes. The circuit at the top of this page is an ESP32 DevKit with a IR receiver and remote, running in your browser on a simulated ESP32 (Xtensa LX6). Click a button on the simulated remote: it sends a real NEC frame, which the receiver demodulates onto the pin. Press Run it in the editor to change the wiring or the code; it is free and needs no account.

How do I find my remote’s codes?

Run this sketch and press each button: the command printed is the code to compare against. Address and command are both in decodedIRData if you need to tell two remotes apart.

Why does every press print FFFFFFFF?

That is the repeat code of an NEC remote as old versions of the library reported it. The current library flags repeats instead, which the sketch checks.

Which receiver is this?

A three-pin 38 kHz demodulating receiver like the VS1838B or TSOP38238. Check the pin order on yours: it differs between makers.

Build your own IR Receiver project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.