Maker.io main logo

Building an Arduino-Powered Universal IR Switch Box

2021-06-01 | By Maker.io Staff

License: See Original Project

Previous articles discussed how infrared communication works, what the common communication protocols are, how they function, and how it’s possible to employ an Arduino to utilize the convenience and power of IR remote controls in custom projects. This article will show you how to do this yourself!

The finished product allows a user to make everyday household electronic devices controllable with practically any commonly available IR remote control — for example, the remote control of an old TV. All this is possible without having to modify any devices by building something similar to a smart home power outlet that reacts to a predefined IR remote signal.

Required Parts and Components

This project was made using the following parts and electronic components:

Name/Qty./Link

ABS hobby project enclosure (approx. 5.9” x 3.15” x 1.95”)

Nema Tamper Resistant Outlet

IEC Connector

SPST Rocker Switch

2.1 / 5.5 DC barrel connector

Arduino Uno

Arduino Relay Module

Note that this project does not require this specific gray ABS enclosure. Any other container will work, as long as the components fit. If the case is conductive, however, then it's a good idea to attach an additional ground wire to the enclosure to prevent the risk of getting an electric shock in case of a mechanical failure, for example, when a wire loosens because of vibrations. Also, note that this article links a power outlet commonly found in the US. You’ll see in these pictures, however, a European-style socket with an additional dust cover.

Assembling the Case for the IR Switch Box

This project started with an empty gray ABS project enclosure. Then, I employed a sharp hobby knife to cut through the soft plastic case and create the cutouts for all the components.

Most of the components snap into place if their respective position in the case has the correct size, so there’s no need for additional screw holes. There is, however, a need for two extra holes to allow the IR light to reach the IR receiver inside of the box and to provide means for a user to plug in an external power cable to supply the Arduino and the relay module. The cutout for the IR receiver is located on the opposite side of the case. Snapping all the parts into place transforms the boring grey box from before into the following interesting looking device:

Note that even though all the parts snap in place safely, I still added a small layer of hot glue around the edges where the power outlet and the IEC plug snap in place to help support the parts if a cable gets pulled out aggressively. Doing so also better protects the electronics inside from water and dust.

Assembling the Electronics

The circuit of this project is not complicated. To build it, attach the phase, neutral, and earth wires to the IEC connector, and then connect the neutral and earth wire directly to the AC socket. Note that I used wires with colors according to the current IEC standard. That is blue for neutral, brown for the phase, and green/yellow for the earth wire. Other standards exist where, for example, a red wire represents the phase, black is the neutral wire, and green/yellow is the earth wire. Anyway, connect the remaining phase wire to the normally open contact of one of the relays (usually labeled NO2). Then use another piece of wire to connect the COM terminal of the selected relay to the AC power outlet:

connect_1

Make sure to insulate the wires and terminals as well as possible and ensure that the screw terminals grip onto the metal core of the wire and not the insulation. Then attach two wires to the rocker switch and each pin of the DC jack. Note that the Arduino uses a center-positive plug, which means that the outer ring of the power cable connects to GND.

Next, connect the Arduino to the control pin of the relay (usually labeled IN2). Then, attach one of the two switch wires to GND and the other one to D12 on the Arduino. The Arduino will change between two modes according to the position of the rocker switch. Connect the IR receiver/sender module to the Arduino as well. Last, connect one of the 5V wires of the DC jack to the Arduino’s VIN pin and the other one to the VCC pin of the relay module. Then, connect the GND pins as well so that all the components share a common GND:

schemeit_2

You can see this schematic in more detail at Scheme-It.

Once all the components have been connected, mount the Arduino and the IR sensor board to the lid of the case, and close everything up. Align the IR sensor with the previously drilled holes in the case and also make sure that the wires have enough space within the enclosure. Then, close the case with the screws that come with it.

box_3

The finished build with all wires connected. You can use cable ties to keep the wiring neatly organized.

Note that the Arduino can’t supply enough power to drive the relay module directly. That’s why the DC jack is directly connected to the relay module to power it.

Make sure to use a relay board that’s rated for mains AC voltage and note the maximum amperage rating of the relay. Some relay modules might break if high-power household appliances, such as an AC unit or a hairdryer, are connected.

Bringing it All to Life with the Right Code

Just like in the previous articles, the IRremote library for Arduino will do the heavy lifting of capturing and decoding IR signals. Therefore, the code is rather short:

Copy Code
#include <IRremote.h>

#define IR_RECEIVE_PIN 11
#define SWITCH_INPUT 12
#define RELAY_OUTPUT 8
#define STATUS_LED 13
#define CODE_STORE_DELAY 1000

long code = 0xFFE01F;
long lastCodeStored = 0;

bool on = false;

IRrecv receiver(IR_RECEIVE_PIN);

void setup()
{
Serial.begin(9600);

receiver.enableIRIn();

pinMode(SWITCH_INPUT, INPUT_PULLUP);
pinMode(RELAY_OUTPUT, OUTPUT);
pinMode(STATUS_LED, OUTPUT);
}

void loop()
{
bool recMode = digitalRead(SWITCH_INPUT);
decode_results results;

// The IR receiver detected an input
if (receiver.decode(&results))
{
if(!recMode && results.value == code)
{
Serial.print("RECEIVED CODE ");
Serial.println(results.value, HEX);

on = !on;
digitalWrite(RELAY_OUTPUT, on);
}
else if(recMode && millis() - lastCodeStored > CODE_STORE_DELAY)
{
code = results.value;
lastCodeStored = millis();

Serial.print("STORED CODE ");
Serial.println(results.value, HEX);
}

receiver.resume();
}

digitalWrite(STATUS_LED, recMode);
}

The setup method initializes the required I/O pins of the Arduino and sets up a serial output for debugging. The loop method first checks whether the switch is in its “On” position or not. The result gets stored in the recMode variable. As mentioned earlier, the rocker switch allows the Arduino to change between states. Regardless of the state, the Arduino listens for incoming IR signals. In the record mode (recMode is true), the received value gets stored in the code variable, and the current timestamp is also recorded. Doing so prevents repeat commands from being falsely detected. If the Arduino is not in the record mode, and it receives an incoming IR signal, it checks whether the incoming signal matches the stored value. If it does, the state of the relay changes. Note that it's mandatory to call the receiver.resume() function after each detection process to allow for the next IR input to be detected. The last line of the loop method turns the internal LED on whenever the Arduino is in the record mode.

Summary

Previous articles discussed how IR communications work and how an Arduino can receive and send IR signals. This article shows how to apply those techniques in a practical project. The finished device listens for incoming IR signals, and it can detect any button on almost any remote control. Once the Arduino stored an IR sequence, it listens for that exact value and toggles a relay’s state when it receives that IR signal. When building the circuit, choose a relay that’s rated for use with mains voltage and can withstand high enough currents. Other than that, this project is easy to assemble and understand, and it should allow beginners and more experienced makers to build a useful helper for their home.

制造商零件编号 RA1122AC
SWITCH ROCKER SPST 15A 125V
CIT Relay and Switch
制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
Add all DigiKey Parts to Cart
TechForum

Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.

Visit TechForum