IR remote control relay module on an Arduino Uno
IRremote and a 21-key handset: keys 1 to 4 toggle the four channels of a relay board, each switching a 12 V lamp, and 0 turns them all off. The whole build, an Arduino Uno and 8 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 $43. 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.
// IR remote control relay module: the 1 to 4 keys of the handset switch the
// four channels of a relay board, each running a 12 V lamp, and 0 turns them
// all off. The first step of most home automation builds.
//
// pin 2 the VS1838B receiver's OUT (VCC 5V, GND)
// pins 4 to 7 the relay board's IN1 to IN4 (VCC 5V, GND)
// each relay COM to the 12 V adapter's +, NO to its lamp, the lamp's
// other lead to the adapter's -
// adapter - also to GND, so the 12 V side shares the Uno's ground
//
// The handset's IR pin is wired to the receiver's: in Mokxi two parts only
// meet on a net, so the beam is a wire. Click a key on the handset.
//
// IRremote does the decoding. Each key of the common 21-key NEC handset
// sends a command byte: 1 is 0x0C, 2 is 0x18, 3 is 0x5E, 4 is 0x08 and 0 is
// 0x16. Hold a key and the handset sends repeat frames; a toggle must ignore
// them, or the lamp flickers on and off while the key is held.
//
// The relay board is active low, like nearly every board sold: LOW pulls a
// relay in. Each pin is written HIGH before it becomes an output, or every
// relay clicks on for an instant at power-up.
//
// The lamps here are 12 V. A relay board's contacts are rated for mains, but
// mains wiring belongs in a closed box and is not something to learn on.
#define DECODE_NEC
#include <IRremote.hpp>
const int RECEIVER_PIN = 2;
const int RELAYS[4] = {4, 5, 6, 7};
const uint8_t KEYS[4] = {0x0C, 0x18, 0x5E, 0x08}; // 1, 2, 3, 4
const uint8_t KEY_0 = 0x16;
bool on[4] = {false, false, false, false};
void set(int i, bool state) {
on[i] = state;
digitalWrite(RELAYS[i], state ? LOW : HIGH); // active low
Serial.print("relay ");
Serial.print(i + 1);
Serial.println(state ? " ON" : " OFF");
}
void setup() {
for (int i = 0; i < 4; i++) {
digitalWrite(RELAYS[i], HIGH); // off before it is an output
pinMode(RELAYS[i], OUTPUT);
}
Serial.begin(9600);
IrReceiver.begin(RECEIVER_PIN, ENABLE_LED_FEEDBACK);
Serial.println("IR relays: keys 1 to 4 toggle a relay, 0 turns all off");
}
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;
if (command == KEY_0) {
for (int i = 0; i < 4; i++) set(i, false);
return;
}
for (int i = 0; i < 4; i++) {
if (command == KEYS[i]) {
set(i, !on[i]);
return;
}
}
Serial.print("key 0x");
Serial.print(command, HEX);
Serial.println(" is not used");
}
Parts list
10 parts, plus the jumper wires. They retail for about $43; on Mokxi they're free.
How it is wired
18 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 7; Relay board, four channels pin IN4
- Arduino Uno R3 pin 6; Relay board, four channels pin IN3
- Arduino Uno R3 pin 5; Relay board, four channels pin IN2
- Arduino Uno R3 pin 4; Relay board, four channels pin IN1
- Arduino Uno R3 pin 2; Infrared receiver, VS1838B pin OUT
- Arduino Uno R3 pin 5V; Infrared receiver, VS1838B pin VCC; Relay board, four channels pin VCC
- Ground: Arduino Uno R3 pin GND; Infrared receiver, VS1838B pin GND; Relay board, four channels pin GND
- Infrared remote, 21 buttons pin IR; Infrared receiver, VS1838B pin IR
- Relay board, four channels pin NO1; Filament lamp (1) pin 1
- Relay board, four channels pin COM1; Relay board, four channels pin COM2; Relay board, four channels pin COM3; Relay board, four channels pin COM4; DC jack and adapter pin V+
- Relay board, four channels pin NO2; Filament lamp (2) pin 1
- Relay board, four channels pin NO3; Filament lamp (3) pin 1
- Relay board, four channels pin NO4; Filament lamp (4) pin 1
- Ground: DC jack and adapter pin V-
- Ground: Filament lamp (1) pin 2
- Ground: Filament lamp (2) pin 2
- Ground: Filament lamp (3) pin 2
- Ground: Filament lamp (4) pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.