Arduino IR Remote Receiver: Wiring, Code and Simulator
To build this Arduino IR Remote Receiver project you need an Arduino Uno and an IR receiver and an IR remote. Decode NEC infrared frames from a remote by timing the pulses on pin 2 with pulseIn. Make the 3 connections in the wiring table, upload the Arduino C++ code, and press keys on the remote while it runs; each one prints its command code.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | IR receiver | A 38 kHz receiver: DAT idles HIGH and pulses LOW with each NEC frame from the IR remote. VCC, GND, DAT to a pin. |
| 1 | IR remote | Press a key while running: every IR receiver in the circuit gets its NEC code (address 0). |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 5V | IR receiver VCC |
| 2 | Arduino Uno GND | IR receiver GND |
| 3 | Arduino Uno pin 2 | IR receiver DAT |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add an IR receiver and an IR remote from the parts palette.
- Wire 1. Connect Arduino Uno 5V to IR receiver VCC.
- Wire 2. Connect Arduino Uno GND to IR receiver GND.
- Wire 3. Connect Arduino Uno pin 2 to IR receiver DAT.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Press keys on the remote while it runs; each one prints its command code.
Arduino C++ code
// IR remote: decode NEC frames from an IR receiver on pin 2, by timing pulses.
// A frame is a 9 ms burst (DAT LOW), a 4.5 ms gap, then 32 bits: each bit is
// a 560 us LOW and then a HIGH of 560 us (0) or 1690 us (1), LSB first.
const int IR = 2;
void setup() {
pinMode(IR, INPUT);
Serial.begin(9600);
Serial.println("Press a key on the remote.");
}
void loop() {
if (pulseIn(IR, LOW, 100000UL) < 8000) return; // wait for the 9 ms leader
// The line is now HIGH for the 4.5 ms gap. pulseIn(HIGH) waits for a HIGH to
// START, so each call below skips ahead and times the next bit's HIGH.
unsigned long code = 0;
for (int i = 0; i < 32; i++) {
unsigned long high = pulseIn(IR, HIGH, 10000UL); // the first also waits out the gap
if (high == 0) return;
if (high > 1000) code |= 1UL << i;
}
byte command = (code >> 16) & 0xFF;
Serial.print("Key code: 0x");
Serial.println(command, HEX);
}
What to try
Press keys on the remote while it runs; each one prints its command code.
Frequently asked questions
How do I wire the IR Remote Receiver project on an Arduino Uno?
Make 3 connections: Arduino Uno 5V to IR receiver VCC; Arduino Uno GND to IR receiver GND; Arduino Uno pin 2 to IR receiver DAT.
What parts do I need for this Arduino project?
An Arduino Uno and an IR receiver and an IR remote.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
Decode NEC infrared frames from a remote by timing the pulses on pin 2 with pulseIn.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator