How to wire an IR receiver to an ESP32
To wire an IR receiver to an ESP32, connect VCC to 3V3, GND to GND and OUT to GPIO 4 (D4). Power the receiver from 3.3 V so its output swings to 3.3 V, the board’s own level; from 5 V its output would idle at 5 V on a 3.3 V pin. The sketch below uses the IRremote (IRremote.hpp) library. It decodes the buttons of an NEC remote and prints each command code.
- 3.3 V logic
- Library: IRremote.hpp
- 3 wires, 1 helper parts
IR receiver to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| IR receiver pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | 3V3 | red | The VS1838B-style receiver runs from 2.7 to 5.5 V. |
| GND | GND | blue | Ground. |
| OUT | GPIO 4 (D4) | amber | Low while a 38 kHz burst arrives, high otherwise: the demodulated code. |
| IR (light) | The remote | n/a | In the editor the infrared light path is drawn as a gray wire from the remote. |
Helper parts
The extra parts in the circuit above, and where their wires go:
| From | To | Wire color |
|---|---|---|
| RM1 pin IR | IR | gray |
Power and logic levels for IR receiver and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The IR receiver takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.
This pair
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
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.
The working code for the ESP32
Library: IRremote (IRremote.hpp). This is the sketch the circuit above runs.
#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
}
}
What you should see
Open the serial monitor at 115200 baud. The sketch prints Point the remote and press a button, and then a Command line in hexadecimal for each button press on the remote in the circuit.
A receiver that never prints anything is usually on the wrong pin, or the remote uses a protocol other than NEC.
The serial monitor prints this, recorded from the circuit above with one remote button pressed after 300 ms:
Point the remote and press a button
Command: 0x47Common mistakes wiring an IR receiver to an 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.
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.