IR decoder on an Arduino Uno
The kit remote and a VS1838B with the IRremote library: timer 2 samples the receiver in the background, and each button is named on the serial monitor. 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $29. On Mokxi they're free.
What you'll learn
- How an infrared receiver and remote talk
- The NEC protocol and its one-byte command code
- Using a library that samples in the background with a timer
- Mapping codes to names with a switch
- Why timer 2 use blocks tone() on pins 3 and 11
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
The VS1838B receiver picks up the remote's infrared flashes and outputs a clean digital signal on pin 11. The IRremote library starts timer 2 to sample that pin in the background, so your loop never has to wait for a button.
When a whole frame has arrived, the sketch checks it. A repeat frame, sent while you hold a button, prints (repeat). A NEC frame prints its one-byte command in hex and the button name. Anything else prints not NEC.
Parts list
4 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.
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 11 |
| Infrared receiver, VS1838B pin VCC | 5 V |
| 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.
// IR decoder: the Elegoo kit's infrared lesson with the IRremote library.
// Press a button on the handset and the serial monitor names it.
//
// receiver OUT -> pin 11, GND -> GND, VCC -> 5 V
// handset its IR pin to the receiver's IR pin (the beam)
//
// IrReceiver.begin() starts timer 2 sampling pin 11 every 50 microseconds in
// the background, as the library does on an Uno, so decode() never waits: it
// returns true once a whole frame has arrived. The handset speaks NEC, so
// decodedIRData.command is the button's one-byte code and the address is 0.
// Holding a button sends a short repeat frame every 110 ms, which the library
// flags as a repeat.
//
// Timer 2 belongs to the library now, so tone() and analogWrite() on pins 3
// and 11 are not available, as on a real Uno.
//
// Things to try. Hold a button and watch the repeats. Then add a case to
// switch on the command and light the Uno's LED from a button.
// 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 = 11;
const char *nameOf(uint8_t command) {
switch (command) {
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 0x16: return "0";
case 0x19: return "100+";
case 0x0D: return "200+";
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";
default: return "?";
}
}
void setup() {
Serial.begin(9600);
IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR receiver ready");
}
void loop() {
if (IrReceiver.decode()) {
if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) {
Serial.println(" (repeat)");
} else if (IrReceiver.decodedIRData.protocol == NEC) {
Serial.print("0x");
Serial.print(IrReceiver.decodedIRData.command, HEX);
Serial.print(" ");
Serial.println(nameOf(IrReceiver.decodedIRData.command));
} else {
Serial.println("not NEC");
}
IrReceiver.resume();
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
nameOf sketch.ino
A switch that turns a command byte into the label printed on the remote, such as 0x45 for CH- or 0x15 for VOL+. Any code not in the list returns a question mark.
const char *nameOf(uint8_t command) {
switch (command) {
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 0x16: return "0";
case 0x19: return "100+";
case 0x0D: return "200+";
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";
default: return "?";
}
}setup sketch.ino
Opens serial at 9600 and starts the receiver on RECEIVER_PIN (11) with LED feedback on. The begin call also starts the timer that does the sampling.
void setup() {
Serial.begin(9600);
IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR receiver ready");
}loop sketch.ino
Asks the library whether a full frame is ready. If so, it prints one of three things: a repeat note, the hex command with its name, or not NEC. Then IrReceiver.resume() tells the library to listen for the next frame.
void loop() {
if (IrReceiver.decode()) {
if (IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT) {
Serial.println(" (repeat)");
} else if (IrReceiver.decodedIRData.protocol == NEC) {
Serial.print("0x");
Serial.print(IrReceiver.decodedIRData.command, HEX);
Serial.print(" ");
Serial.println(nameOf(IrReceiver.decodedIRData.command));
} else {
Serial.println("not NEC");
}
IrReceiver.resume();
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Only ? appears for some buttons. This project
nameOfonly lists 21 known codes. A different remote sends other commands, so add cases for them. - Nothing prints when you press a button. This project
Check the receiver's OUT goes to pin 11, and its VCC to 5 V and GND to ground. - 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.