IR remote codes on pin 12, on an Arduino Uno
An infrared receiver on pin 12: press a button on the remote and the IRremote library prints the code it sends, the full 32-bit NEC value the lesson’s table lists. 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 parts retail for about $32. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Read a remote control: an IR receiver on pin 12.
//
// receiver OUT -> pin 12 VCC -> 5V GND -> GND
//
// Each button sends an address and a command. Printed as one 32-bit NEC
// value, as older code and many tutorials list them, the 0 button is
// FF6897 and the 1 button FF30CF.
//
// Click a button on the remote on the canvas.
// The simulated remote speaks NEC, so only that decoder is compiled in.
#define DECODE_NEC
#include <IRremote.hpp>
const int RECEIVER = 12;
// The classic 32-bit NEC value: address, its inverse, command, its inverse,
// each byte sent least significant bit first.
unsigned long classicCode(uint8_t address, uint8_t command) {
unsigned long code = 0;
uint8_t bytes[4] = {address, (uint8_t)~address, command, (uint8_t)~command};
for (int b = 0; b < 4; b++) {
for (int i = 0; i < 8; i++) code = (code << 1) | ((bytes[b] >> i) & 1);
}
return code;
}
void setup() {
Serial.begin(9600);
IrReceiver.begin(RECEIVER, ENABLE_LED_FEEDBACK);
Serial.println("UNO is ready! Press a button on the remote.");
}
void loop() {
if (!IrReceiver.decode()) return;
bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
uint8_t address = IrReceiver.decodedIRData.address;
uint8_t command = IrReceiver.decodedIRData.command;
IrReceiver.resume();
if (repeat) return;
Serial.println(classicCode(address, command), HEX);
}
Parts list
4 parts, plus the jumper wires. They retail for about $32; on Mokxi they're free.
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 12; Infrared receiver, VS1838B pin OUT
- Arduino Uno R3 pin 5V; 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.