{"id":12688,"date":"2016-11-27T09:30:58","date_gmt":"2016-11-27T14:30:58","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=12688"},"modified":"2016-11-27T09:40:00","modified_gmt":"2016-11-27T14:40:00","slug":"controlling-relay-switches-infrared-remote","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/controlling-relay-switches-infrared-remote\/","title":{"rendered":"Controlling relay switches with an infrared remote"},"content":{"rendered":"<p>The\u00a0infrared (IR) communication technology, which existed long before WiFi and Bluetooth, is still a key component\u00a0in implementing major components of a typical home automation system. For example, IR technology is still used in cordless headphones, for intrusion detection in home security systems, and in handheld remotes for controlling home entertainment systems (TV, DVD, soundbox, etc), air-conditioning units, and other household appliances. Because IR technology requires a line-of-sight between the transmitter and receiver units, it can only be used for connecting devices in short range, like in a same room. In one of my previous articles, I wrote about\u00a0a <a href=\"http:\/\/embedded-lab.com\/blog\/a-very-simple-ir-remote-control-switch-for-an-electrical-appliance\/\" target=\"_blank\">simple IR remote control\u00a0circuit<\/a>\u00a0based on the CD4017 decade counter IC. That was capable to\u00a0switch on\u00a0and off any home appliance hooked to it by using any TV\/DVD remote. Today, I\u00a0am going to show how to make an Arduino-based IR receiver\u00a0for decoding the IR signals coming from a TV\/DVD remote. Based on the decoded values corresponding to specific buttons on the remote, we will\u00a0program the Arduino to control multiple relay switches.<\/p>\n<div id=\"attachment_12691\" style=\"width: 1034px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup2.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12691\" class=\"size-full wp-image-12691\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup2.jpg\" alt=\"Controlling multiple relay switches using an IR remote\" width=\"1024\" height=\"860\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup2.jpg 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup2-580x487.jpg 580w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><p id=\"caption-attachment-12691\" class=\"wp-caption-text\">Controlling multiple relay switches using an IR remote<\/p><\/div>\n<h3>Circuit setup<\/h3>\n<p>The IR signal\u00a0transmitted by a remote\u00a0can be easily interfered by\u00a0other nearby IR sources, such as heaters\u00a0and incandescent lamps. Therefore, in order for the receiver to distinguish between the IR signals and all other sources of infrared noise, the transmitted IR signal is modulated. For modulation, a stable carrier frequency (typically 30\u201360 kHz)\u00a0is required, with 38Khz being the most commonly used carrier frequency. Although there are several\u00a0digital modulation techniques, most of the\u00a0modern consumer electronics use\u00a0Amplitude Shift Keying (ASK) for designing IR remote control units. In ASK, the amplitude of the carrier is varied in accordance with the\u00a0digital input signal. Logic 1 is represented by a carrier signal of certain\u00a0amplitude, while logic 0 is represented by changing the carrier amplitude to zero (or turning it off). You can find a lot more detail on IR modulation schemes in the following application notes:<\/p>\n<p><a href=\"http:\/\/www.ti.com\/lit\/an\/slaa644b\/slaa644b.pdf\" target=\"_blank\">IR remote control implementation &#8230; from <strong>TI<\/strong><\/a><\/p>\n<p><a href=\"http:\/\/www.nxp.com\/assets\/documents\/data\/en\/application-notes\/AN3053.pdf\" target=\"_blank\">IR remote control techniques &#8230; from <strong>NXP<\/strong><\/a><\/p>\n<p>In\u00a0this project, we will\u00a0use IR signals coming from a TV remote for controlling multiple relay switches.\u00a0In order to do that, we need an IR receiver circuit that would demodulate the IR signals transmitted by the TV remote. The demodulated digital output can be then directly interpreted by Arduino.<\/p>\n<p>The\u00a0receiving circuit for this project is built with the TSOP1740 IR receiver. The TSOP17XX series are miniaturized receivers with photo detector and preamplifier built in one package and are ready to use for infrared remote control systems. A bandpassfilter, an integrator stage and an automatic gain control are also used inside to suppress unwanted\u00a0noises. The last two digits (XX) of its name represent the center frequency of the bandpass. Therefore, the TSOP1740 operates for a modulated IR signal\u00a0of 40\u00a0KHz. For more details on TSOP17XX receiver, check the <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP1738.pdf\">datasheet<\/a>.<\/p>\n<div id=\"attachment_12710\" style=\"width: 862px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12710\" class=\"size-full wp-image-12710\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP.png\" alt=\"TSOP17XX series IR receiver\" width=\"852\" height=\"662\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP.png 852w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP-580x451.png 580w\" sizes=\"auto, (max-width: 852px) 100vw, 852px\" \/><\/a><p id=\"caption-attachment-12710\" class=\"wp-caption-text\">TSOP17XX series IR receiver pin diagram<\/p><\/div>\n<p>Shown below is the complete circuit setup required for this project. The demodulated output from TSOP17340 goes to Arduino pin D11. A dual relay control board is used in this demonstration. The control pins (IN1 and IN2) for the two relays are driven by D6 and D7 I\/O pins of Arduino. The entire circuit operates at 5V.<\/p>\n<div id=\"attachment_12719\" style=\"width: 1318px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/circuit.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12719\" class=\"size-full wp-image-12719\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/circuit.png\" alt=\"TSOP1740, dual relay module, and Arduino wiring\" width=\"1308\" height=\"1102\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/circuit.png 1308w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/circuit-580x489.png 580w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/circuit-1024x863.png 1024w\" sizes=\"auto, (max-width: 1308px) 100vw, 1308px\" \/><\/a><p id=\"caption-attachment-12719\" class=\"wp-caption-text\">TSOP1740, dual relay module, and Arduino wiring<\/p><\/div>\n<p>The relay board used in this project was purchased on\u00a0Aliexpress for ~$3 and features optocouplers for electrical isolation between the input control pins and output relay driver circuit. The board looks like following.<\/p>\n<div id=\"attachment_12715\" style=\"width: 623px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relay.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12715\" class=\"size-full wp-image-12715\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relay.jpg\" alt=\"Dual channel relay board\" width=\"613\" height=\"519\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relay.jpg 613w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relay-580x491.jpg 580w\" sizes=\"auto, (max-width: 613px) 100vw, 613px\" \/><\/a><p id=\"caption-attachment-12715\" class=\"wp-caption-text\">Dual channel relay board<\/p><\/div>\n<p>I have found that the onboard driver circuit for the two relay unite operates with\u00a0active low inputs. That means, the IN\u00a0pin must be pulled low to activate the relay. The following circuit describes how the relay control logic works. The same circuit is applicable for the second relay unit too. The jumper JP1 allows you to choose a separate\u00a0power supply (marked as JD-VCC on the board) for the output side (relay and its transistor driver). If you want to use the same 5V power supply\u00a0for the entire circuit, you need to place a shunt jumper (provided) between VCC and JD-VCC pins. It should be\u00a0easy to follow from the\u00a0circuit diagram that a logic 0 at the input turns the optocoupler on, which then\u00a0activates the relay.<\/p>\n<div id=\"attachment_12700\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relayboard.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12700\" class=\"size-full wp-image-12700\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relayboard.jpg\" alt=\"Relay and optocoupler circuit\" width=\"800\" height=\"529\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relayboard.jpg 800w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/relayboard-580x384.jpg 580w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><p id=\"caption-attachment-12700\" class=\"wp-caption-text\">Relay driver and optocoupler circuit<\/p><\/div>\n<p>Shown below is the complete hardware setup made on a breadboard. An Arduino Nano board is used in this project.<\/p>\n<div id=\"attachment_12690\" style=\"width: 1034px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup11.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12690\" class=\"size-full wp-image-12690\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup11.jpg\" alt=\"Complete circuit setup on a breadboard\" width=\"1024\" height=\"589\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup11.jpg 1024w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/setup11-580x334.jpg 580w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><p id=\"caption-attachment-12690\" class=\"wp-caption-text\">Complete circuit setup on a breadboard<\/p><\/div>\n<h3>Software<\/h3>\n<p>After the hardware setup is complete, we will now program the Arduino Nano to receive the TSOP1738 demodulated output\u00a0and interpret\u00a0the codes for individual button presses on the remote. There are many different\u00a0infrared protocol standards or data formats that are designed by different manufacturers for use in consumer electronics. The most commonly used protocols are RC-5 and RC-6 from Philips, SIRC from Sony, NEC, JVC, Sharp, and Nokia NRC17. Each of these protocols have their own formats of sending data, which consists of some address bits and some command bits. Describing each of these protocols is beyond the scope of this article. I would suggest the interested readers to look online\u00a0if they want more details, or check out the following links.<\/p>\n<p><a href=\"http:\/\/www.vishay.com\/docs\/80071\/dataform.pdf\" target=\"_blank\">Vishay Document (Data Formats for IR Remote Control)<\/a><\/p>\n<p><a href=\"http:\/\/ww1.microchip.com\/downloads\/cn\/AppNotes\/cn520424.pdf\" target=\"_blank\">AN1064 Microchip Application note<\/a><\/p>\n<p>We have a really nice <a href=\"https:\/\/github.com\/z3t0\/Arduino-IRremote\" target=\"_blank\">IRremote library<\/a>, contributed by <em>Ken Shirriff<\/em>, that allows you to transmit and send infrared remote control codes in different data formats using Arduino. We are using this library to decode the IR codes transmitted by an IR remote. In this example, I am using a Sony brand Blueray Disc\u00a0player remote, which\u00a0uses SIRC protocol for sending IR data.<\/p>\n<p>We will\u00a0develop the Arduino firmware for this project in two steps. First, we will write a simple program\u00a0to find out\u00a0the numeric codes (or decimal values) for specific buttons on the remote. It will read the IR receiver output, decode the message, and print out the type of protocol used\u00a0and the decimal value for the transmitted code on a serial monitor window. The program to do that is posted below (code is adapted\u00a0from IRrelay example written by Ken Shirriff).<\/p>\n<pre>#include &lt;IRremote.h&gt;\r\n\r\nint RECV_PIN = 11;\r\nIRrecv irrecv(RECV_PIN);\r\ndecode_results results;\r\n\r\nvoid dump(decode_results *results) {\r\n int count = results-&gt;rawlen;\r\n if (results-&gt;decode_type == UNKNOWN) {\r\n Serial.println(\"Could not decode message\");\r\n } \r\n else {\r\n Serial.println(\"Signal received.\");\r\n if (results-&gt;decode_type == NEC) {\r\n Serial.print(\"Decoded NEC, \");\r\n } \r\n else if (results-&gt;decode_type == SONY) {\r\n Serial.print(\"Decoded SONY, \");\r\n } \r\n else if (results-&gt;decode_type == RC5) {\r\n Serial.print(\"Decoded RC5, \");\r\n } \r\n else if (results-&gt;decode_type == RC6) {\r\n Serial.print(\"Decoded RC6, \");\r\n }\r\n Serial.print(\"Value= \");\r\n Serial.print(results-&gt;value, DEC);\r\n Serial.print(\" (\");\r\n Serial.print(results-&gt;bits, DEC);\r\n Serial.println(\" bits)\");\r\n }\r\n \r\n}\r\n\r\nvoid setup()\r\n{\r\n Serial.begin(9600);\r\n Serial.println(\"IR decoder\");\r\n irrecv.enableIRIn(); \/\/ Start the receiver\r\n}\r\n\r\nint on = 0;\r\nunsigned long last = millis();\r\n\r\nvoid loop() {\r\n if (irrecv.decode(&amp;results)) {\r\n \/\/ Wait for 0.5sec before decoding another code\r\n if (millis() - last &gt; 500) {\r\n \r\n dump(&amp;results);\r\n }\r\n last = millis(); \r\n irrecv.resume(); \/\/ Receive the next value\r\n }\r\n}<\/pre>\n<h4><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP1738_Decode_Test.zip\">Download the above test code<\/a><\/h4>\n<p>The hardware setup for this test program is not different than what was described above. Now, you aim a remote towards the TSOP1740 module on the breadboard and press a button, and you will see the decoded decimal value for that button displayed\u00a0on the serial monitor window. Make sure the serial monitor baud rate is selected to be 9600. If your remote uses a\u00a0different data coding scheme\u00a0that is not present\u00a0in the list of supported protocols of\u00a0the IRremote library, then it\u00a0won&#8217;t be able to decode the code. \u00a0I tested it with two different remotes: one came with\u00a0my Sony Blueray player and another with\u00a0my HP All-in-one computer. It was able to decode both. I could see on the serial monitor window that the HP remote uses Philips RC-6 protocol, while Sony uses its own SIRC protocol. With my Sony remote, I noted down the decimal codes for VOL+, VOL-, CH+, CH-, PAUSE, and PLAY buttons on the remote, as displayed below.<\/p>\n<div id=\"attachment_12694\" style=\"width: 577px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/testserialmonitor.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12694\" class=\"wp-image-12694 size-full\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/testserialmonitor.png\" alt=\"testserialmonitor\" width=\"567\" height=\"434\" \/><\/a><p id=\"caption-attachment-12694\" class=\"wp-caption-text\">Decoding the IR message<\/p><\/div>\n<p>Now\u00a0we know what code is sent out for specific button presses, we can\u00a0modify the code to implement additional logic for switching\u00a0the relay circuits. We will program the Arduino to perform\u00a0the following logic.<\/p>\n<p>When VOL+ is pressed, Relay1 is turned ON.<\/p>\n<p>When VOL- is pressed, Relay1 is turned OFF.<\/p>\n<p>When CH+ is pressed, Relay2 is turned ON.<\/p>\n<p>When CH-\u00a0is pressed, Relay2 is turned OFF.<\/p>\n<p>When PAUSE button is pressed, both relays are turned OFF.<\/p>\n<p>When PLAY button is pressed, both relays are turned ON.<\/p>\n<p>The following code does this task.<\/p>\n<pre>\/* Project: Arduino relay switch using an IR remote control\r\n * \r\n * Raj Bhatt (Nov 25, 2016)\r\n * http:\/\/www.embedded-lab.com\r\n*\/\r\n\r\n\r\n#include &lt;IRremote.h&gt;\r\n#define SW1 6 \/\/ Relay control pins are 6 and 7\r\n#define SW2 7\r\n\r\nint RECV_PIN = 11; \/\/ IR receive pin is 11\r\nIRrecv irrecv(RECV_PIN);\r\nlong int decodedCode; \r\n\r\ndecode_results rcv;\r\n\r\nvoid controlRelays(){\r\n if(decodedCode == 1168) digitalWrite(SW1, LOW); \r\n if(decodedCode == 3216) digitalWrite(SW1, HIGH); \r\n if(decodedCode == 144) digitalWrite(SW2, LOW); \r\n if(decodedCode == 2192) digitalWrite(SW2, HIGH); \r\n if(decodedCode == 625479){\r\n digitalWrite(SW1, HIGH); \/\/ All off \r\n digitalWrite(SW2, HIGH); \r\n }\r\n\r\n if(decodedCode == 363335){\r\n digitalWrite(SW1, LOW); \/\/ All on \r\n digitalWrite(SW2, LOW);\r\n }\r\n delay(300);\r\n}\r\n\r\nvoid setup(){\r\n pinMode(SW1, OUTPUT);\r\n pinMode(SW2, OUTPUT);\r\n digitalWrite(SW1, HIGH); \/\/ Relay is active low, so HIGH will turn it off at startup\r\n digitalWrite(SW2, HIGH); \r\n Serial.begin(9600);\r\n Serial.println(\"IR relay controller\");\r\n irrecv.enableIRIn(); \r\n}\r\n\r\nvoid loop(){\r\n if (irrecv.decode(&amp;rcv)) {\r\n Serial.print(\"Signal received, Value= \");\r\n decodedCode = rcv.value;\r\n Serial.println(decodedCode, DEC);\r\n controlRelays();\r\n irrecv.resume(); \r\n }\r\n}<\/pre>\n<h4><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/TSOP1738_RelayControl.zip\">Dowload the relay control program<\/a><\/h4>\n<h3>Output<\/h3>\n<p>After uploading the program to Arduino, enjoy the fun of turning the relays ON and OFF with the remote. You can easily add more relays to other free I\/O pins of Arduino and add more <strong>if statements<\/strong>\u00a0to control them in a similar fashion.<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_12689\" style=\"width: 810px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/op1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-12689\" class=\"size-full wp-image-12689\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/op1.jpg\" alt=\"IR\" width=\"800\" height=\"854\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/op1.jpg 800w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2016\/11\/op1-580x619.jpg 580w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" \/><\/a><p id=\"caption-attachment-12689\" class=\"wp-caption-text\">Controlling relays with a Sony Blueray Disc Player remote<\/p><\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0infrared (IR) communication technology, which existed long before WiFi and Bluetooth, is still a key component\u00a0in implementing major components of a typical home automation system. For example, IR technology is still used in cordless headphones, for intrusion detection in home security systems, and in handheld remotes for controlling home entertainment systems (TV, DVD, soundbox, etc), air-conditioning units, and other household appliances. Because IR technology requires a line-of-sight between the transmitter and receiver units, it can only be used for connecting devices in short range, like in a same room. In one of my previous articles, I wrote about\u00a0a simple IR<\/p>\n","protected":false},"author":1,"featured_media":12691,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[150,1464,1467],"tags":[1571,856],"class_list":["post-12688","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-2","category-featured","category-projects","tag-infrared-decoding","tag-ir-remote"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/12688","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=12688"}],"version-history":[{"count":22,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/12688\/revisions"}],"predecessor-version":[{"id":12728,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/12688\/revisions\/12728"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/12691"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=12688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=12688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=12688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}