Built-in project · Arduino Uno R3

IR receiver on pin 2, on an Arduino Uno

An IR receiver module on pin 2: press a button on the remote and the IRremote library prints the button’s name and its code. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

IR receiver on pin 2 · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// An IR receiver on pin 2: which button was pressed on the remote?
//
//   receiver OUT -> pin 2   VCC -> 5V   GND -> GND
//
// A remote flashes an infrared LED in a pattern, about 38,000 times a second
// while it is "on". The receiver turns the flashes back into a digital
// signal, and the IRremote library decodes it into a command number, one per
// button.
//
// Click a button on the remote on the canvas.

#include <IRremote.hpp>

const int RECEIVER_PIN = 2;

const char *nameOf(uint8_t command) {
  switch (command) {
    case 0x16: return "0";
    case 0x0C: return "1";
    case 0x18: return "2";
    case 0x5E: return "3";
    case 0x08: return "4";
    case 0x1C: return "5";
    case 0x5A: return "6";
    case 0x42: return "7";
    case 0x52: return "8";
    case 0x4A: return "9";
    case 0x45: return "CH-";
    case 0x46: return "CH";
    case 0x47: return "CH+";
    case 0x44: return "PREV";
    case 0x40: return "NEXT";
    case 0x43: return "PLAY/PAUSE";
    case 0x07: return "VOL-";
    case 0x15: return "VOL+";
    case 0x09: return "EQ";
    case 0x19: return "100+";
    case 0x0D: return "200+";
    default: return "unknown";
  }
}

void setup() {
  Serial.begin(9600);
  IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
  Serial.println("Mokxi Uno: IR receiver on pin 2");
  Serial.println("Press a button on the remote");
}

void loop() {
  if (!IrReceiver.decode()) return;
  bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
  uint8_t command = IrReceiver.decodedIRData.command;
  IrReceiver.resume();
  if (repeat) return;
  Serial.print(nameOf(command));
  Serial.print("  (0x");
  Serial.print(command, HEX);
  Serial.println(")");
}

Parts list

4 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Infrared receiver, VS1838B pin GND
  • Arduino Uno R3 pin 2; Infrared receiver, VS1838B pin OUT
  • Arduino Uno R3 pin 5V; Infrared receiver, VS1838B pin VCC
  • Infrared receiver, VS1838B pin IR; Infrared remote, 21 buttons pin IR

Change it and keep it

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