Build an ESP32 IR Learning Remote Demo
The Story
This project receives real IR codes and can replay learned NEC commands. It is not a universal compatibility promise or access-control bypass tool.
Explain Like I'm 12
A remote blinks invisible light in patterns. The receiver turns those patterns into numbers, and the ESP32 can blink an IR LED with the same pattern.
Learning Support
- Recommended ageAges 12+
- Adult supervisionAdult help recommended when checking transistor and IR LED polarity.
- Classroom useUse a classroom TV remote or spare consumer remote and discuss protocols without promising universal compatibility.
- Parent promptAsk why a transmitter needs line of sight and why high-current LEDs need a driver.
- Screen-free activityDraw carrier pulses, receiver output pulses, and a transistor-driven IR LED.
- Next challengeStore learned codes only after basic receive/send behavior is reliable.
- Skills practiced
- IR receiving
- Protocol decoding
- Transistor driver
- Line-of-sight limits
- Learning outcomes
- Decode IR signals on GPIO15.
- Transmit learned NEC commands from GPIO4.
- Explain 38 kHz carrier modulation.
- State protocol and appliance compatibility limits.
- Mini experiments
- Test remote angle and distance.
- Compare two remote protocols.
- Block line of sight and observe failure.
Safety Standards
- Unplug USB before changing jumper wires. Recheck 3.3 V, 5 V, and GND before reconnecting power.
- Do not power motors, pumps, LED strips, or servos from the ESP32 3.3 V pin. Use a suitable external supply and common ground.
- Breadboards are for low-current prototypes. Move high-current or unattended builds to proper terminals, enclosure, strain relief, and fusing.
What You Will Build
A safe receive-and-send IR learning demo for consumer-device education.
Learning Objectives
- Decode IR signals on GPIO15.
- Transmit learned NEC commands from GPIO4.
- Explain 38 kHz carrier modulation.
- State protocol and appliance compatibility limits.
Components List
- ESP32 DevKit boardUSB-programmable ESP32 board for the low-voltage logic side.
- TSOP38238 IR receiver38 kHz demodulating receiver, OUT to GPIO15.
- 940 nm IR LEDTransmitter LED; use correct polarity.
- 2N2222 NPN transistorDrives IR LED current from GPIO4 control signal.
- ResistorsBase and LED current limiting resistors.
- Breadboard and jumper wiresFor low-voltage prototyping.
Bill of Materials
| Part | Qty | Estimated Cost | Notes |
|---|---|---|---|
| ESP32 DevKit board | 1 | Varies | USB-programmable ESP32 board for the low-voltage logic side. |
| TSOP38238 IR receiver | 1 | Varies | 38 kHz demodulating receiver, OUT to GPIO15. |
| 940 nm IR LED | 1 | Varies | Transmitter LED; use correct polarity. |
| 2N2222 NPN transistor | 1 | Varies | Drives IR LED current from GPIO4 control signal. |
| Resistors | 1 | Varies | Base and LED current limiting resistors. |
| Breadboard and jumper wires | 1 | Varies | For low-voltage prototyping. |
Wiring
TSOP38238 OUT goes to GPIO15. GPIO4 drives a transistor stage for the IR LED transmitter.
-
1
Unplug USB before wiring.
-
2
Connect TSOP38238 VCC to 3.3 V, GND to GND, and OUT to GPIO15.
-
3
Connect GPIO4 through a base resistor to the 2N2222 base.
-
4
Wire the IR LED and current-limiting resistor through the transistor driver.
-
5
Keep receiver and transmitter aimed at safe consumer devices.
-
6
Upload the sketch and open Serial Monitor.
GPIO Mapping
| Signal | ESP32 Pin | Direction | Notes |
|---|---|---|---|
| TSOP38238 OUT | GPIO15 | Input | IR receive data; boot strap pin, avoid external pull conflicts. |
| IR LED driver | GPIO4 | Output | Controls transistor base through resistor. |
Circuit Explanation
The TSOP receiver demodulates 38 kHz IR into digital pulses. The transmitter uses GPIO4 to switch a transistor that pulses the IR LED.
Engineering Explanation
IR protocols vary. This sketch stores and retransmits NEC commands only; unsupported protocols can still be logged for study.
Libraries
- IRremoteInstall Arduino-IRremote library.
Code
Copy into Arduino IDE. Install any libraries noted in the component guides first.
// ESP32 IR receive-and-send learning demo
// Learn codes from your own remote before transmitting.
#include <IRremote.hpp>
const int IR_RECV_PIN = 15;
const int IR_SEND_PIN = 4;
uint16_t learnedAddress = 0;
uint16_t learnedCommand = 0;
decode_type_t learnedProtocol = UNKNOWN;
bool hasLearnedCode = false;
void setup() {
Serial.begin(115200);
IrReceiver.begin(IR_RECV_PIN, ENABLE_LED_FEEDBACK);
IrSender.begin(IR_SEND_PIN);
Serial.println("IR demo ready. Point a safe consumer remote at the receiver.");
}
void loop() {
if (!IrReceiver.decode()) return;
Serial.println("IR signal received");
Serial.print("Protocol: ");
Serial.println(getProtocolString(IrReceiver.decodedIRData.protocol));
Serial.print("Address: 0x");
Serial.println(IrReceiver.decodedIRData.address, HEX);
Serial.print("Command: 0x");
Serial.println(IrReceiver.decodedIRData.command, HEX);
if (IrReceiver.decodedIRData.protocol == NEC) {
learnedProtocol = NEC;
learnedAddress = IrReceiver.decodedIRData.address;
learnedCommand = IrReceiver.decodedIRData.command;
hasLearnedCode = true;
Serial.println("Stored NEC code. Send it back by opening Serial Monitor and typing s.");
} else {
Serial.println("This sketch retransmits NEC only. Use raw examples for other protocols.");
}
IrReceiver.resume();
if (Serial.available()) {
char c = Serial.read();
if ((c == 's' || c == 'S') && hasLearnedCode && learnedProtocol == NEC) {
IrSender.sendNEC(learnedAddress, learnedCommand, 2);
Serial.println("Sent learned NEC command.");
}
}
}
Code Explanation
The sketch decodes any received IR signal, stores NEC address/command values, and sends the learned NEC command when the user types s.
Expected Output
Serial Monitor shows protocol, address, and command. NEC codes can be retransmitted after learning.
Troubleshooting
- No receive data Check receiver pinout and aim the remote close to the sensor.
- Protocol UNKNOWN The remote may use an unsupported or complex protocol; inspect raw output.
- Transmit does not work Check IR LED polarity, transistor wiring, and whether the appliance uses NEC.
Common Mistakes
- Expecting universal compatibility.
- Driving a high-current IR LED directly from GPIO.
- Using IR to bypass access systems.
- Forgetting line-of-sight limits.
Testing Checklist
- Decode a known consumer remote.
- Learn one NEC command.
- Send only toward the same safe device.
Engineering Tips
- Test in moderate lighting.
- Keep examples to owned consumer devices.
- Label learned codes by device and button.
Upgrade Ideas
- Store learned NEC codes in Preferences.
- Add OLED display for the last decoded command.
- Add a web page after local transmit works.
Real-World Applications
- Consumer remote protocol lesson
- IR receiver test fixture
- Line-of-sight communication demo
FAQs
Will it control every appliance?
No. Protocols and timings vary.
Can I copy any remote?
Only learn and replay codes for devices you own and are allowed to control.
Why use a transistor?
IR LEDs often need more pulse current than an ESP32 pin should provide directly.
Review, Testing, and References
Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-06-27. Reviewed: wiring logic, Arduino code structure, beginner safety, and learning sequence.
Educational level: Intermediate. Estimated completion time: 60-75 min. This project is for learning and prototyping; production or unattended hardware needs additional engineering review.
Project Complete!
You completed Build an ESP32 IR Learning Remote Demo with matching wiring, code, tests, and limitations.
- IR receiving
- Protocol decoding
- Transistor driver
