Remote-controlled colors: IR on pin 2, RGB on 11, 10 and 9, on an Arduino Uno
An IR receiver on pin 2 and an RGB LED with red on 11, green on 10 and blue on 9: buttons 1 to 7 on the remote pick a color of the rainbow and 0 turns the light off. The whole build, an Arduino Uno and 6 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.
Intermediate Runs in your browser. Free, and no account needed.
The parts retail for about $34. 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.
// A remote-controlled light: number buttons pick the RGB LED's color.
//
// IR receiver OUT -> pin 2 VCC -> 5V GND -> GND
// RGB red -> 220 ohm -> pin 11 green -> 220 ohm -> pin 10
// RGB blue -> 220 ohm -> pin 9 the long common leg -> GND
//
// Each button on the remote sends its own command number. The receiver
// decodes it, keyOf() turns it into the label on the button, and the label
// picks a color: 1 to 7 are red, orange, yellow, green, blue, indigo and
// violet, and 0 is off.
//
// Click a number on the remote on the canvas.
// The simulated remote speaks NEC, so only that decoder is compiled in.
#define DECODE_NEC
#include <IRremote.hpp>
// The remote's buttons by the command each one sends.
const char *keyOf(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 "";
}
}
const int RECEIVER = 2;
const int RED = 11;
const int GREEN = 10;
const int BLUE = 9;
const int RAINBOW[8][3] = {
{0, 0, 0}, // 0: off
{255, 0, 0}, // 1: red
{255, 80, 0}, // 2: orange
{255, 200, 0}, // 3: yellow
{0, 255, 0}, // 4: green
{0, 0, 255}, // 5: blue
{75, 0, 130}, // 6: indigo
{160, 0, 255}, // 7: violet
};
void setup() {
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
Serial.begin(9600);
IrReceiver.begin(RECEIVER, ENABLE_LED_FEEDBACK);
Serial.println("Mokxi Uno: press 0 to 7 on the remote");
}
void loop() {
if (!IrReceiver.decode()) return;
bool repeat = IrReceiver.decodedIRData.flags & IRDATA_FLAGS_IS_REPEAT;
const char *key = keyOf(IrReceiver.decodedIRData.command);
IrReceiver.resume();
if (repeat || key[0] < '0' || key[0] > '7' || key[1] != '\0') return;
int n = key[0] - '0';
analogWrite(RED, RAINBOW[n][0]);
analogWrite(GREEN, RAINBOW[n][1]);
analogWrite(BLUE, RAINBOW[n][2]);
Serial.print("button ");
Serial.println(key);
}
Parts list
8 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
How it is wired
10 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.
- Arduino Uno R3 pin 11; Resistor, 220 Ω (3) pin 1
- Arduino Uno R3 pin 10; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 9; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 2; Infrared receiver, VS1838B pin OUT
- Arduino Uno R3 pin 5V; Infrared receiver, VS1838B pin VCC
- Ground: Arduino Uno R3 pin GND; RGB LED pin C; Infrared receiver, VS1838B pin GND
- RGB LED pin R; Resistor, 220 Ω (3) pin 2
- RGB LED pin G; Resistor, 220 Ω (2) pin 2
- RGB LED pin B; Resistor, 220 Ω (1) pin 2
- 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.