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 Runs in your browser. Free, and no account needed.
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.
#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();
}
}
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 11; Infrared receiver, VS1838B pin OUT
- 5 V: 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.