{"id":29723,"date":"2019-01-15T11:45:57","date_gmt":"2019-01-15T11:45:57","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=29723"},"modified":"2023-04-23T11:11:30","modified_gmt":"2023-04-23T11:11:30","slug":"guide-for-relay-module-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/","title":{"rendered":"Guide for Relay Module with Arduino"},"content":{"rendered":"<p>This article shows how to control mains voltage with the Arduino using a relay module. We make a brief introduction to the relay module and build a simple project example with the Arduino. The example we&#8217;ll build shows how to control a relay module with an Arduino and a PIR motion sensor.<!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-80923\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/guide-relay-module-arduino-featured.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"1200\" height=\"675\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/guide-relay-module-arduino-featured.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/guide-relay-module-arduino-featured.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/guide-relay-module-arduino-featured.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/guide-relay-module-arduino-featured.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>By the end of this tutorial, you should be able to control any electronics appliances with your Arduino using a relay module.<!--more--><\/p>\n<h2>Introducing the Relay Module<\/h2>\n<p>A relay is an electrically operated switch that can be turned on or off, letting the current go through or not, and can be controlled with low voltages, like the 5V provided by the Arduino pins.<\/p>\n<p>Controlling a relay module with the Arduino is as simple as controlling any other output as we&#8217;ll see later on.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-29724\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module.jpg?resize=210%2C197&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"relay-module\" width=\"210\" height=\"197\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module.jpg?w=210&amp;quality=100&amp;strip=all&amp;ssl=1 210w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module.jpg?resize=150%2C141&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module.jpg?resize=80%2C75&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module.jpg?resize=107%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 107w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module.jpg?resize=160%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 160w\" sizes=\"(max-width: 210px) 100vw, 210px\" \/><\/p>\n<p>This relay module has two channels (those blue cubes). There are other models with one, four and eight channels. This module should be powered with 5V, which is appropriate to use with an Arduino. There are other relay modules that are powered using 3.3V, which is ideal for ESP32, ESP8266, and other microcontrollers.<\/p>\n<p><strong>Get a relay module<\/strong>:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/5v-2-channel-relay-module-optocoupler\/\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">5V 2-channel relay module<\/a><\/li>\n<li><a href=\"https:\/\/www.banggood.com\/2Pcs-5V-1-Channel-Level-Trigger-Optocoupler-Relay-Module-For-Arduino-p-1366337.html?p=MA240439985285201910\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">5V 1-channel relay module<\/a><\/li>\n<li><a href=\"https:\/\/www.banggood.com\/5V-8-Channel-Relay-Module-Board-For-Arduino-PIC-AVR-DSP-ARM-p-74110.html?p=MA240439985285201910\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">5V 8-channel relay module<\/a><\/li>\n<li><a href=\"https:\/\/www.banggood.com\/BESTEP-1-Channel-3_3V-Low-Level-Trigger-Relay-Module-Optocoupler-Isolation-Terminal-For-Arduino-p-1355736.html?p=MA240439985285201910\" target=\"_blank\" rel=\"noopener nofollow noreferrer\">3.3V 1-channel relay module<\/a><\/li>\n<\/ul>\n<h2>Relay Pinout<\/h2>\n<p>The following figure shows the relay module pinout.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-80914\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/12\/relay-module-pinout.jpg?resize=712%2C310&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"712\" height=\"310\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/12\/relay-module-pinout.jpg?w=712&amp;quality=100&amp;strip=all&amp;ssl=1 712w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/12\/relay-module-pinout.jpg?resize=300%2C131&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 712px) 100vw, 712px\" \/><\/p>\n<p>The six pins on the left side of the relay module connect high voltage, and the pins on the right side connect the component that requires low voltage\u2014the Arduino pins.<\/p>\n<h2>Mains voltage connections<\/h2>\n<p>The high-voltage side has two connectors, each with three sockets: common (COM), normally closed (NC), and normally open (NO).<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29725\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=372%2C342&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"relay-labeled\" width=\"372\" height=\"342\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?w=372&amp;quality=100&amp;strip=all&amp;ssl=1 372w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=150%2C138&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=300%2C276&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=80%2C74&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=220%2C202&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=109%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 109w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=163%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 163w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-labeled.jpg?resize=259%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 259w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/p>\n<ul>\n<li><strong>COM<\/strong>:&nbsp;common pin<\/li>\n<li><strong>NC&nbsp;(Normally Closed):<\/strong>&nbsp;the normally closed configuration is used when you want the relay to be closed by default, meaning the current is flowing unless you send a signal from the Arduino to the relay module to open the circuit and stop the current.<\/li>\n<li><strong>NO&nbsp;(Normally Open):<\/strong>&nbsp;the normally open configuration works the other way around: the relay is always open, so the circuit is broken unless you send a signal from the Arduino to close the circuit.<\/li>\n<\/ul>\n<p>If you just want to light up a lamp occasionally, it is better to use a normally-open circuit configuration.<\/p>\n<h2>Pin wiring<\/h2>\n<p>The low-voltage side has a set of four pins and a set of three pins.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-29728\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=395%2C425&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"img_6361\" width=\"395\" height=\"425\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=139%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 139w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=279%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 279w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=74%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 74w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=205%2C220&amp;quality=100&amp;strip=all&amp;ssl=1 205w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=93%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 93w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=221%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 221w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=386%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 386w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=453%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 453w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/IMG_6361.jpg?resize=553%2C595&amp;quality=100&amp;strip=all&amp;ssl=1 553w\" sizes=\"(max-width: 395px) 100vw, 395px\" \/><\/p>\n<p>The set at the right&nbsp;consists of <strong>VCC<\/strong> and <strong>GND<\/strong> to power up the module, and input 1 (<strong>IN1<\/strong>) and input 2 (<strong>IN2<\/strong>) to control the bottom and top relays, respectively.<\/p>\n<p>The second set of pins consists of <strong>GND<\/strong>, <strong>VCC<\/strong>, and<strong> JD-VCC<\/strong> pins. The JD-VCC pin powers the electromagnet of the relay.<\/p>\n<p class=\"rntbox rntclblue\"><strong>Note: <\/strong> notice that the module has a jumper cap connecting the VCC and JD-VCC pins; the one shown here is blue, but yours may be a different color. The jumper cap allows you to choose whether the circuit is physically connected to the Arduino circuit or not, and you can choose to have it on or not. With the jumper cap on, the VCC and JD-VCC pins are connected. That means the relay electromagnet is directly powered from the Arduino\u2019s power pin, so the relay module and the Arduino circuits are not physically isolated from each other (this is the configuration we&#8217;ll use). Without the jumper cap, you need to provide an independent power source to power up the relay\u2019s electromagnet through the JD-VCC pin. That configuration physically isolates the relays from the Arduino with the module\u2019s built-in optocoupler.<\/p>\n<p>The connections between the relay module and the Arduino are really simple:<\/p>\n<ul>\n<li><strong>GND<\/strong>: goes to ground<\/li>\n<li><strong>IN1<\/strong>: controls the first relay&nbsp;(it will be connected to an Arduino digital pin)<\/li>\n<li><strong>IN2<\/strong>: controls the second relay&nbsp;(it should&nbsp;be connected to an Arduino digital pin if you are using this second relay. Otherwise, you don\u2019t need to connect it)<\/li>\n<li><strong>VCC<\/strong>: goes to 5V<\/li>\n<\/ul>\n<h2>Example: Controlling a Lamp&nbsp;with a Relay Module and PIR Motion Sensor<\/h2>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-29745\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=473%2C382&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"dsc09897\" width=\"473\" height=\"382\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=150%2C122&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=300%2C243&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=80%2C65&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=220%2C178&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=123%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 123w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=185%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 185w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=294%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 294w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=512%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 512w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/DSC09897.jpg?resize=601%2C487&amp;quality=100&amp;strip=all&amp;ssl=1 601w\" sizes=\"(max-width: 473px) 100vw, 473px\" \/><\/p>\n<p>In this example, we create a motion sensitive lamp. A lamp lights up for 10 seconds every time motion is detected.<\/p>\n<p>Motion will be detected using a <a href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\" rel=\"noopener noreferrer\">PIR motion sensor<\/a>. If you are not familiar with the PIR motion sensor, you can read the following post:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-with-pir-motion-sensor\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino with PIR Motion Sensor<\/a><\/li>\n<\/ul>\n<p>To control the lamp with mains voltage we&#8217;ll use a <a href=\"https:\/\/makeradvisor.com\/tools\/5v-2-channel-relay-module-optocoupler\/\" target=\"_blank\" rel=\"noopener noreferrer\">relay module<\/a> in normally-open configuration.<\/p>\n<h2>Safety warning<\/h2>\n<p>Before proceeding with this project, I want to let you know that you\u2019re dealing with mains voltage. Please read the safety warning below carefully.<\/p>\n<p class=\"rntbox rntcred\"><strong>Warning: <\/strong> when you are making projects that are connected to mains voltage, you really need to know what you are doing, otherwise you may shock yourself. This is a serious topic, and we want you to be safe. If you&#8217;re not 100% sure what you are doing, do yourself a favor and don&#8217;t touch anything. Ask someone who knows!<\/p>\n<h3>Parts required<\/h3>\n<p>Here&#8217;s the needed parts for this example:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/5v-2-channel-relay-module-optocoupler\/\" target=\"_blank\" rel=\"noopener noreferrer\">Relay Module<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>&nbsp;\u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\" rel=\"noopener noreferrer\">PIR Motion Sensor&nbsp;<\/a><\/li>\n<li>Lamp Cord Set&nbsp;(<a href=\"http:\/\/ebay.to\/2feHELc\" target=\"_blank\" rel=\"noopener noreferrer\">view on eBay<\/a>)<\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h3>Code<\/h3>\n<p>Copy the following code to your Arduino IDE and upload it to your Arduino board.<\/p>\n<p class=\"rntbox rntcred\"><strong>Warning: <\/strong> you shouldn&#8217;t upload new code while your Arduino is connected to the relay.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos\n  Complete project details at http:\/\/randomnerdtutorials.com  \n*********\/\n\n\/\/ Relay pin is controlled with D8. The active wire is connected to Normally Closed and common\nint relay = 8;\nvolatile byte relayState = LOW;\n\n\/\/ PIR Motion Sensor is connected to D2.\nint PIRInterrupt = 2;\n\n\/\/ Timer Variables\nlong lastDebounceTime = 0;  \nlong debounceDelay = 10000;\n\nvoid setup() {\n  \/\/ Pin for relay module set as output\n  pinMode(relay, OUTPUT);\n  digitalWrite(relay, HIGH);\n  \/\/ PIR motion sensor set as an input\n  pinMode(PIRInterrupt, INPUT);\n  \/\/ Triggers detectMotion function on rising mode to turn the relay on, if the condition is met\n  attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);\n  \/\/ Serial communication for debugging purposes\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \/\/ If 10 seconds have passed, the relay is turned off\n  if((millis() - lastDebounceTime) &gt; debounceDelay &amp;&amp; relayState == HIGH){\n    digitalWrite(relay, HIGH);\n    relayState = LOW;\n    Serial.println(&quot;OFF&quot;);\n  }\n  delay(50);\n}\n\nvoid detectMotion() {\n  Serial.println(&quot;Motion&quot;);\n  if(relayState == LOW){\n    digitalWrite(relay, LOW);\n  }\n  relayState = HIGH;  \n  Serial.println(&quot;ON&quot;);\n  lastDebounceTime = millis();\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Arduino_Relay_PIR_Guide.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h4>How the code works<\/h4>\n<p>First, we create variables to hold the pin the relay IN1 pin is connected to and to save the relay state:<\/p>\n<pre class=\"language-c\"><code class=\"language-c\">int relay = 8;\nvolatile byte relayState = LOW;<\/code><\/pre>\n<p>The PIR motion sensor is connected to <span class=\"rnthl rntcyellow\">pin 2<\/span>:<\/p>\n<pre class=\"language-c\"><code>int PIRInterrupt = 2;<\/code><\/pre>\n<p>We need to create some auxiliary variables to handle timers with the PIR motion sensor. The <span class=\"rnthl rntliteral\">lastDebounceTime<\/span> variable saves the last time motion was detected. The <span class=\"rnthl rntliteral\">debounceDelay<\/span> saves how much time the lamp should remain on after motion is detected (here we&#8217;re setting 10 seconds = 10000 milliseconds)<\/p>\n<pre class=\"language-c\"><code class=\"language-c\">long lastDebounceTime = 0;\nlong debounceDelay = 10000;<\/code><\/pre>\n<p>In the&nbsp;<span class=\"rnthl rntliteral\">setup()<\/span>, we set the relay as an OUTPUT and turn it off by default:<\/p>\n<pre class=\"language-c\"><code class=\"language-c\">pinMode(relay, OUTPUT);\ndigitalWrite(relay, HIGH);<\/code><\/pre>\n<p>Because we&#8217;re using a normally open configuration, there is no contact between the COM and NO sockets unless you trigger the relay. The relay is triggered when the input goes below about 2 V. That means if you send a LOW signal from the Arduino, the relay turns on, and if you send a HIGH signal, the relay turns off;&nbsp; it works with inverted logic.<\/p>\n<p>Set the PIR motion sensor as an interrupt:<\/p>\n<pre class=\"language-c\"><code class=\"language-c\">pinMode(PIRInterrupt, INPUT);\n\/\/ Triggers detectMotion function on rising mode to turn the relay on, if the condition is met\nattachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);<\/code><\/pre>\n<p>Whenever the PIR motion sensor is triggered, it calls the <span class=\"rnthl rntliteral\">detectMotion()<\/span> function declared at the end of the code to turn the relay on:<\/p>\n<pre class=\"language-c\"><code class=\"language-c\">void detectMotion() {\n    Serial.println(\"Motion\");\n    if(relayState == LOW){\n        digitalWrite(relay, LOW);\n    }\n    relayState = HIGH;\n    Serial.println(\"ON\");\n    lastDebounceTime = millis();\n}<\/code><\/pre>\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we check whether 10 seconds have passed since the relay is on. If that condition is true, we can turn the relay off.<\/p>\n<pre class=\"language-c\"><code class=\"language-c\">if((millis() - lastDebounceTime) &gt; debounceDelay &amp;&amp; relayState == HIGH){\n    digitalWrite(relay, HIGH);\n    relayState = LOW;\n    Serial.println(\"OFF\");\n}<\/code><\/pre>\n<h3>Schematic<\/h3>\n<p>Assemble all the parts as shown in the schematic diagram.<\/p>\n<p class=\"rntbox rntcred\"><strong>Warning: <\/strong>do not touch any wires that are connected to mains voltage. Also make sure you have tighten all screws of the relay module.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?quality=100&#038;strip=all&#038;ssl=1\" target=\"_blank\" rel=\"noopener noreferrer\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-29730 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=700%2C466&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"schematics\" width=\"700\" height=\"466\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=150%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=300%2C200&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=80%2C53&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=220%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=225%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 225w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=358%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 358w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/Schematics.png?resize=623%2C415&amp;quality=100&amp;strip=all&amp;ssl=1 623w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p>The lamp is connected to the relay using a normally open configuration. The Arduino controls the relay through <span class=\"rnthl rntcgreen\">pin 8<\/span> (<span class=\"rnthl rntcgreen\">pin 8<\/span> is connected to the relay IN1 pin). Finally, the PIR motion sensor is connected to <span class=\"rnthl rntcyellow\">pin 2<\/span>.<\/p>\n<h2>Demonstration<\/h2>\n<p>After uploading the code and wiring the circuit, you can test your setup.<\/p>\n<p>When motion is detected, your lamp lights up. If there isn&#8217;t motion for 10 seconds, the lamp turns off.<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29742\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/relay-module-gif.gif?resize=480%2C270&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"relay-module-gif\" width=\"480\" height=\"270\"><\/p>\n<h2>Wrapping Up<\/h2>\n<p>Controlling a relay module with the Arduino is as simple as controlling an output &#8211; you just need to send HIGH or LOW signals using an Arduino digital pin. With the relay module you can control almost any AC electronics appliances (not just lamps).<\/p>\n<p>We hope you&#8217;ve found this guide useful. If you like this project, you may also like our premium Arduino course:<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\">Arduino Step-by-step Projects: Build 25 Projects<\/a><\/li>\n<\/ul>\n<p>We have more than 60 free tutorials and projects with the Arduino. If you&#8217;re looking for a guide for a specific module, we probably have what you&#8217;re looking for.<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-arduino\/\">60+ Arduino Projects and Tutorials<\/a><\/li>\n<\/ul>\n<p>Finally, you can also get access to our <a href=\"https:\/\/randomnerdtutorials.com\/download\/\">FREE resources here<\/a>.<\/p>\n<p>Thanks for reading.<\/p>\n<p><em><span style=\"color: #808080;\">January 15, 2019<\/span><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article shows how to control mains voltage with the Arduino using a relay module. We make a brief introduction to the relay module and build a simple project example &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Guide for Relay Module with Arduino\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/#more-29723\" aria-label=\"Read more about Guide for Relay Module with Arduino\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":80923,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,267,2,302,269,230,264,215,10],"tags":[],"class_list":["post-29723","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-0-guide","category-guide-project","category-guide","category-project","category-sensors","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2019\/01\/guide-relay-module-arduino-featured.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/29723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=29723"}],"version-history":[{"count":4,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/29723\/revisions"}],"predecessor-version":[{"id":130648,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/29723\/revisions\/130648"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/80923"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=29723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=29723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=29723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}