Wiring guide

How to wire an IR receiver to an Arduino Mega

To wire an IR receiver to an Arduino Mega, connect VCC to 5V, GND to GND and OUT to pin 2. The receiver runs from 5 V and its output idles at 5 V, which the Arduino Mega reads directly. The sketch below uses the IRremote (IRremote.hpp) library. It decodes the buttons of an NEC remote and prints each command code.

  • 5 V logic
  • Library: IRremote.hpp
  • 3 wires, 1 helper parts
Arduino Mega · IR receiverlive0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

IR receiver to Arduino Mega 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 pinArduino Mega pinWire colorWhy
VCC5VredThe VS1838B-style receiver runs from 2.7 to 5.5 V.
GNDGNDblueGround.
OUTpin 2amberLow while a 38 kHz burst arrives, high otherwise: the demodulated code.
IR (light)The remoten/aIn 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:

FromToWire color
RM1 pin IRIRgray
Voltage

Power and logic levels for IR receiver and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The IR receiver takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

The receiver runs from the Arduino Mega’s 5V pin and its output idles at 5 V, which the board reads directly.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: IRremote (IRremote.hpp). This is the sketch the circuit above runs.

IR receiver on Arduino Mega pin 2
#include <IRremote.hpp>

const int IR_PIN = 2;   // OUT, pin 2

void setup() {
  Serial.begin(9600);
  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
  }
}
Output

What you should see

Open the serial monitor at 9600 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: 0x47
Pitfalls

Common mistakes wiring an IR receiver to an Arduino Mega

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.

Taking the serial or I2C pins

Pins 0 and 1 are the USB serial port, 14 to 19 are Serial1 to Serial3 and 20 and 21 are I2C. the receiver output works on any of them until the sketch needs that port, so start on pin 2 and up.

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.