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

QtyPartWhat it does
1Arduino Unothe microcontroller board
1IR receiverA 38 kHz receiver: DAT idles HIGH and pulses LOW with each NEC frame from the IR remote. VCC, GND, DAT to a pin.
1IR remotePress a key while running: every IR receiver in the circuit gets its NEC code (address 0).

Wiring

#FromTo
1Arduino Uno 5VIR receiver VCC
2Arduino Uno GNDIR receiver GND
3Arduino Uno pin 2IR receiver DAT

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an IR receiver and an IR remote from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to IR receiver VCC.
  4. Wire 2. Connect Arduino Uno GND to IR receiver GND.
  5. Wire 3. Connect Arduino Uno pin 2 to IR receiver DAT.
  6. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  7. 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