Built-in project · Arduino Uno R3

Four AC lamps on an IR remote SSR board, on an Arduino Uno

Keys 1 to 4 on an IR handset toggle the four channels of a G3MB-202P solid-state relay board, each switching a 12 V AC lamp; 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.

Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $39. On Mokxi they're free.

Four AC lamps on an IR remote SSR board · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Lights
  • IoT
  • Using the Infrared remote
  • Using the Infrared receiver
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 8

How it works

Keys 1 to 4 on an IR handset toggle the four channels of a G3MB-202P solid-state relay board, each switching a 12 V AC lamp; 0 turns them all off.

Arduino Uno R3 runs the sketch below. The circuit around it: Infrared remote, 21 buttons, Infrared receiver, VS1838B, SSR board, 4-channel G3MB-202P, Signal generator, Filament lamp. pinMode sets RELAYS[i] as an output. digitalWrite drives RELAYS[i] HIGH. Serial.begin opens the serial port at 9600 baud.

Parts list

8 parts, plus the jumper wires and a bench instrument. The parts retail for about $39; on Mokxi they're free, and so are the instruments.

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 pinConnects to
SSR board, 4-channel G3MB-202P pin DC-Arduino Uno R3 pin GND
SSR board, 4-channel G3MB-202P pin CH4Arduino Uno R3 pin 7
SSR board, 4-channel G3MB-202P pin CH3Arduino Uno R3 pin 6
SSR board, 4-channel G3MB-202P pin CH2Arduino Uno R3 pin 5
SSR board, 4-channel G3MB-202P pin CH1Arduino Uno R3 pin 4
Infrared receiver, VS1838B pin OUTArduino Uno R3 pin 2
Infrared receiver, VS1838B pin VCCArduino Uno R3 pin 5V
SSR board, 4-channel G3MB-202P pin DC+Arduino Uno R3 pin 5V
Infrared receiver, VS1838B pin GNDArduino Uno R3 pin GND
Infrared receiver, VS1838B pin IRInfrared remote, 21 buttons pin IR
SSR board, 4-channel G3MB-202P pin 2ASSR board, 4-channel G3MB-202P pin 1A
SSR board, 4-channel G3MB-202P pin 3ASSR board, 4-channel G3MB-202P pin 1A
SSR board, 4-channel G3MB-202P pin 4ASSR board, 4-channel G3MB-202P pin 1A
Signal generator pin OUTSSR board, 4-channel G3MB-202P pin 1A
Filament lamp (1) pin 1SSR board, 4-channel G3MB-202P pin 1B
Filament lamp (2) pin 1SSR board, 4-channel G3MB-202P pin 2B
Filament lamp (3) pin 1SSR board, 4-channel G3MB-202P pin 3B
Filament lamp (4) pin 1SSR board, 4-channel G3MB-202P pin 4B
Signal generator pin GNDGround
Filament lamp (1) pin 2Ground
Filament lamp (2) pin 2Ground
Filament lamp (3) pin 2Ground
Filament lamp (4) pin 2Ground

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// 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");
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Runs once when the board starts. It gets the pins and libraries ready.

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");
}

loop sketch.ino

Runs over and over for as long as the board has power. This is where the project does its work.

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");
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • 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.
  • 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.
  • The circuit under test shows clipped or distorted signals. Signal generator
    A sine swinging below zero can exceed what an input allows. Add a DC offset or bias so the signal stays inside the supply range of the part.
  • Measurements are off or noisy. Signal generator
    Join the generator's ground to the circuit ground. Keep amplitude and frequency within what the connected parts can handle.
  • The lamp is dim or does not light when driven from a board pin. Filament lamp
    A lamp needs far more current than a pin gives (a pin is limited to about 20 mA). Switch it with a transistor or MOSFET from its own supply that matches the lamp's voltage rating.
  • The lamp's brightness lags or it burns out the switch when it first turns on. Filament lamp
    A cold filament draws several times its running current at first. Size the switch for that surge, not just the steady current.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.