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 About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $32. On Mokxi they're free.
What you'll learn
- How a remote and an IR receiver talk
- Decoding the NEC protocol with the IRremote library
- Mapping command codes to names with a
switch - Telling a new press from a held-down repeat
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A remote flashes an infrared LED about 38,000 times a second while a button is held. The receiver turns those flashes back into a digital signal on its OUT pin, wired to Arduino pin 2.
The IRremote library decodes the signal into a command number, one per button. The sketch looks the number up and prints the button name with the code in hex, for example 0x16 for button 0.
Held-down repeats are ignored, so each press prints once.
Parts list
4 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Infrared receiver, VS1838BIn IR Remote Kit.
- 1 × Infrared remote, 21 buttonsIn IR Remote Kit.
Kits that can build it
These 8 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Infrared receiver, VS1838B pin GND | Arduino Uno R3 pin GND |
| Infrared receiver, VS1838B pin OUT | Arduino Uno R3 pin 2 |
| Infrared receiver, VS1838B pin VCC | Arduino Uno R3 pin 5V |
| Infrared remote, 21 buttons pin IR | Infrared receiver, VS1838B pin IR |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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.
// The handset speaks NEC, so only that decoder is compiled in, as IRremote's
// own examples do to save flash. Without this line the library brings in
// every protocol's decoder, about 9 KB of the Uno's 32.
#define DECODE_NEC
#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(")");
}
Code walkthrough
The key functions of the sketch, and what each one does.
nameOf sketch.ino
Turns a command byte into a label. A switch matches codes such as 0x16 for 0 and 0x15 for VOL+. Any code it does not know returns unknown.
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";
}
}setup sketch.ino
Opens serial at 9600 baud and starts the receiver on RECEIVER_PIN. The #define DECODE_NEC above the include keeps only the NEC decoder, which saves memory on the Uno.
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");
}loop sketch.ino
Does nothing until decode finds a button press. It then saves the command, calls resume to listen again, skips repeats, and prints the name and hex code.
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(")");
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Nothing prints when you press a button This project
Check OUT goes to pin 2, VCC to 5V and GND to ground. - A button prints unknown This project
Its code is missing fromnameOf. Add acasefor it. - No signal is seen, or the module gets hot, because the three legs are in the wrong order. Infrared receiver
On the VS1838B, with the dome facing you, the legs are usually OUT, GND, VCC from left to right. Check your module's marking before powering it. Supply is 2.7 to 5.5 V. - A plain LED blinking at the receiver is never detected. Infrared receiver
The receiver only responds to light modulated at about 38 kHz. Send with a library such as IRremote, which makes the carrier. OUT idles high and goes low during a burst, so decode that inverted signal. - Button codes come out wrong or do not match another sketch. Infrared remote
Remotes differ. Print the raw codes for your buttons first, then compare against those values. A repeat while you hold a button is a special repeat code, not a new press. - Nothing is received at all. Infrared remote
Check the receiver pin order of OUT, GND and VCC, which varies by module. Use the pin your IR library expects. Keep the receiver away from bright light and aim the remote at it. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.