Introduction: Arduino, Relays and Infrared for Beginners

Picture of Arduino, Relays and Infrared for Beginners

If you're a beginner and not so sure about the basics of electricity and electronics, I recommend Afrotechmods on YouTube.

This tutorial is loosely inspired on Relay Basics by MKme Lab on YouTube.

A microcontroller is a useful tool that brings hardware and software together allowing you to connect outputs and inputs to precise programming logic. The outputs, however, operate at a mere 5V DC which is not enough to power your household electronics. One of the solutions is a relay. The simplest relays and the ones used in this tutorial are electromagnetic switches that operate on a signal from your microcontroller. By outputting a signal to the relay of 5V or HIGH you can close the relay's open circuit, thus allowing current to flow. This is a practical solution to controlling high voltage devices that operate on mains power supply (220 V).

Step 1: Collecting Your Parts

Picture of Collecting Your Parts

The circuit shown in this tutorial is a simple infrared receiver that outputs a signal to the relay depending on a command emitted by the remote. I suggest and even highly recommend that you play around with the circuit and switch the IR receiver for another form of input (with the relay disconnected from the 220 V supply, of course). This circuit is by no means solid, parts can be switched for others and it can be adapted to a wide range of situations. Its purpose is just to get you started on using a relay with your Arduino.

Materials:

* External power supply of 5V (Do not use your Arduino's 5V pin as it can only supply a current of 400mA)

* 1 Diode for flyback protection (I'm not an expert in this field so do some research before deciding which one to use)

* Arduino Uno microcontroller (If you're more comfortable with another Arduino or another microcontroller altogether go for it as long as it can output a HIGH voltage of 5v)

* TSOP4838 Infrared Receiver used as an input (This is the part of the circuit that is highly adaptable, change it to your liking!)

* 1 Electromagnetic Relay module rated for a maximum of >230V AC (If you simply want to use this with lower voltages, invest in a relay that operates at your required voltage)

* A few Jumper Cables

Step 2: Writing the Code

Picture of Writing the Code

It is debatable whether the code or circuit building should be done first, however, writing the code first allows you to sketch out tiny logic details early on that will greatly help you understand what you've built. It's also always better to write the code first and load it on to the board before you connect anything in case any old program could harm your new circuit.

Arduino's IDE makes it easy for tinkerers like you and to me to code. It is always recommended that you do some research on your components or at least watch a quick YouTube video about the component to get a general idea of what it does and how it interacts with software. In this case, the relay works by turning on the electromagnet after receiving a signal from the Arduino. Therefore, your code must be able to output a signal to a pin connected to the relay telling it when to turn on and off.

Once you've got a rough idea of what your program needs to do, start by writing the basics such as defining pin constants, setting up the pins with pinMode() and writing some basic comments.

Now, we need to know how to implement the IR sensor (or whatever input you're using) with the relay so that it will be triggered when it receives a signal from a remote. Put simply, an IR receiver outputs a signal to your Arduino consisting of small pulses of 5v and GND, and the duration of these pulses is what determines the protocol (how you're communicating) and the content of the command from the remote. Analyzing these signals is way too complicated for us, so we use someone else's library to help us. I'm using IRLib2 that you can find here. By slightly modifying their example comboDump sketch to only include the protocols that I need* I shortened the code and copied the bits I wrote earlier.

*You can find the protocol your remote is communicating on by using the dump example sketch. Check the library documentation for more information regarding this. I have another Instructables on this, please check it out!

When using a library it is always useful to read or at least skim over the documentation. It presents the library to you from the programmer's perspective. For example, by reading the documentation I learned that after getting results from the receiver, I can request the .value attribute of the decoder object to get the hexadecimal value given by the remote.

The full code is available here . PLEASE DO NOT SIMPLY COPY IT. TAKE SOME TIME TO UNDERSTAND IT. ASK FOR HELP IF NEEDED.

Step 3: Understanding the Code

Since this tutorial is a general purpose tutorial teaching you both how to use the IR receiver and a relay, it is of paramount importance that you understand the code so you can easily apply it to other projects.

The first part of the code handles the imports. To reduce the memory load on your Arduino, you only need to reference specific parts of the IRLib2 library. Since I want to use the project on the RC5 and NECx protocols, I include them. There are more details you need to know, for example, when importing specific protocols the protocol for the smallest number must be referenced first and then the others. This is all covered in the extensive documentation and examples. To include them you need to know their numbers, you could check the documentation, check the libraries folder and find the file, or just start up the dump example sketch and it will print the number to the console after the protocol in parathesis ie. RC5(4). (Check my other Instructables for more info).

const int relayPin = 12;
boolean state = false; // declare a boolean variable that keeps track of the relay's output state const int signalBlocker = 400; //duration of time during which the circuit will not accept incoming signals

This bit of code is part of the variable declaration and initialization. I have three variables, one for defining what pin I'm going to use, one to keep track of the light state ie. on or off and a threshold value that determines how long I should wait before processing another signal.

The setup function is pretty standard, it really is a straight copy from the dump example, except that I added a pinMode(relayPin, OUTPUT) for the relay.

void loop() {

if (myReceiver.getResults()) {

myDecoder.decode();

The decode and getResults functions are part of the library and I don't really need to mess around with them.

if(myDecoder.protocolNum == 7){

if(myDecoder.value == 0xE0E0E21D){

digitalWrite(relayPin, getState());

state = !state; } }

myReceiver.enableIRIn(); }

delay(signalBlocker); }

This part of the code checks if the protocol is the one I'm looking for, to prevent any mismatches with other remotes and confirms if it is the hex value E0E0E21D. Put the 0x prefix to let C know that it's a hexadecimal number. You can find the number for a key by using the dump example sketch. It then turns the relay on or off depending on its current state, ie. if it's on, turn it off. I could have made my life easier by simply setting the state to an int and using HIGH and LOW to directly control it but I like using boolean variables.


That's pretty much it for the code. Let's move on!

Step 4: Building the Circuit

Picture of Building the Circuit

Keeping in mind the pins we defined in our sketch, it is time to build the circuit.

You should load your program onto the Arduino and then disconnect it to prevent the old program from interfering with your new circuit.

Use the above schematic to connect all your components. The above setup is simply a recommendation, it is by no means a fixed layout.

As for the relay outputs, most relay modules have a NC, NO, and C pins. When the module only has two, it's normally a NO and a C pin.

NO - Normally open: this is the pin that will be connected to the common when the relay is on

C - Common: use this pin to connect either to ground or to the other cable in your open wire

NC - Normally closed: this pin will be connected to the common when the relay is off, it will disconnect once the relay turns on.

You can see in the photos below that I've connected my relay to a lamp by getting the socket wire and taking one side, opening it, removing the plastic coating on the wires, and hooked up one side to the NO pin and the other to the C pin.

Step 5: Testing the Circuit

Picture of Testing the Circuit

Now that your code is written and your circuit is built, it is time to put the two together and watch it succeed!

Be careful if you are using mains power supply: high voltage is extremely dangerous.

You can use the pictures of my circuit as a reference to see how I've done it.

After this tutorial, you know how to use an IR sensor to control a relay and know how to control a relay using an Arduino. Congratulations! Build other cool things with what you've learned and share them in the comments!

Please leave a comment telling me what you thought about the project and if you had any troubles!

Comments

Swansong (author)2017-08-23

That would be really convenient :)

hamlam (author)Swansong2017-08-23

^_^

corneta.funda (author)2017-08-23

I think you did a good work by joining 2 applications infrared and relay. Suggest you to add an large or proximit sensors to the project

corneta.funda (author)2017-08-23

good application. .it works like a breeze.

About This Instructable

728views

18favorites

License:

Bio: A fellow tinkerer.
More by hamlam:Infrared Protocol Decoder With ArduinoArduino, Relays and Infrared for Beginners
Add instructable to: