Arduino Mega IR Receiver Simulator: Wiring, Code and a Live Circuit
This is a 38 kHz IR receiver wired to an Arduino Mega 2560 with a remote control beside it, decoded live with the IRremote library: open it in the editor, press a button on the remote, and the serial monitor prints its command code.
- 5 V logic
- Library: IRremote.hpp
- OUT on pin 2
- NEC protocol, 38 kHz
Wiring the IR Receiver to the Arduino Mega
Three wires to the receiver: power, GND, and OUT to pin 2. The remote speaks NEC, the protocol of most cheap kit remotes.
Every connection in the circuit above, with the board’s own pin names.
| IR Receiver pin | Arduino Mega pin | Why |
|---|---|---|
| VCC | 5V | The VS1838B-style receiver runs from 2.7 to 5.5 V. |
| GND | GND | Ground. |
| OUT | pin 2 | Low while a 38 kHz burst arrives, high otherwise: the demodulated code. |
| IR (light) | The remote | In the editor the infrared light path is drawn as a gray wire from the remote. |
Power
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 on the Arduino Mega
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.
On a real bench
The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The IR Receiver uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.
The code, and what it does.
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
#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
}
}
IrReceiver.begin(IR_PIN) starts the IRremote library listening on the pin. decode() returns true once a whole code has arrived, and decodedIRData.command holds the button’s command byte, 0x47 for the button pressed in this demo.
Holding a button makes an NEC remote send short repeat frames, which the library flags with IRDATA_FLAGS_IS_REPEAT. The sketch prints only the first frame of each press. resume() must be called after each code or nothing more is decoded.
Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.
Common mistakes with the IR Receiver on the 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.
Questions and answers
Can I simulate an IR remote with an Arduino Mega 2560?
Yes. The circuit at the top of this page is an Arduino Mega 2560 with a IR receiver and remote, running in your browser on a simulated ATmega2560. Click a button on the simulated remote: it sends a real NEC frame, which the receiver demodulates onto the pin. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
How do I find my remote’s codes?
Run this sketch and press each button: the command printed is the code to compare against. Address and command are both in decodedIRData if you need to tell two remotes apart.
Why does every press print FFFFFFFF?
That is the repeat code of an NEC remote as old versions of the library reported it. The current library flags repeats instead, which the sketch checks.
Which receiver is this?
A three-pin 38 kHz demodulating receiver like the VS1838B or TSOP38238. Check the pin order on yours: it differs between makers.
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.