# Want to turn on a relay by connecting 2 wires

**URL:** <https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160>\
**Category:** Programming\
**Created:** [September 22, 2023, 5:19pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160 "2023-09-22T17:19:56Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:19pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/1 "2023-09-22T17:19:56Z")

</div>

Hello,

I am new to this arduino world and I would like to create one project. Please help me to write a program for the below requirement.

const int relayPin = 6; // Define the pin connected to the relay module  
const int firstWirePin = 2; // Define the pin for the first set of wires  
const int secondWirePin = 3; // Define the pin for the second set of wires

bool relaystatus;  
bool secondwirestatus;

void setup() {  
pinMode(relayPin, OUTPUT);  
pinMode(firstWirePin, INPUT\_PULLUP);  
pinMode(secondWirePin, INPUT\_PULLUP);  
Serial.begin(9600);  
digitalWrite(relayPin, HIGH); // Initially turn off the relay  
}

void loop() {  
// Check if the first set of wires is connected  
if (digitalRead(firstWirePin) == LOW) {  
digitalWrite(relayPin, LOW); // Turn on the relay  
} else {  
digitalWrite(relayPin, HIGH); // Turn off the relay  
}

// Check if the second set of wires is connected  
while (digitalRead(relayPin) == LOW) {  
delay(10000); // Wait for 15 minutes (900,000 milliseconds)  
digitalWrite(relayPin, HIGH); // Turn off the relay

```
Serial.print("relay");
relaystatus = digitalRead(relayPin);
Serial.print(relaystatus);

      Serial.print("secondWire");
  secondwirestatus = digitalRead(secondWirePin);
  Serial.print(secondwirestatus);
  
if (digitalRead(secondWirePin) == LOW) {
  Serial.print("inside secondWire");
  digitalWrite(relayPin, HIGH); // Turn off the relay
  delay(10000); // Wait for 15 minutes (900,000 milliseconds)
  digitalWrite(relayPin, LOW); // Turn on the relay
}
else{
   Serial.print("in else secondWire");
  secondwirestatus = digitalRead(secondWirePin);
  Serial.print(secondwirestatus);
}

```

}

// If the second set is completely disconnected, exit the loop  
while (true) {  
if (digitalRead(secondWirePin) == HIGH) {  
digitalWrite(relayPin, HIGH); // Turn off the relay forever  
break; // Exit the loop  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [September 22, 2023, 5:25pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/2 "2023-09-22T17:25:40Z")

</div>

Hi @chandu-mca06, welcome to the forum!

First of all a schematic will be very helpful, even though your setup sounds simple to me. You can even draw a schematic by hand. Ok?

---

<div class="post-metadata">

**Author:** ![xfpd](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/xfpd/32/1120236_2.png) [@xfpd](https://forum.arduino.cc/u/xfpd)\
**Post date:** [September 22, 2023, 5:28pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/3 "2023-09-22T17:28:22Z")

</div>

I would make sure you have a..  
4. Relay with the ability to use external power supply to drive the coils of the relay (you will see a jumper labeled JD-VCC) because the Arduino can not supply the energy needed to activate two relays.

Have you run the "blink without delay" sketch? That will show you how to send an enable pulse to a device.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [September 22, 2023, 5:32pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/4 "2023-09-22T17:32:41Z")

</div>

> [@xfpd](#):
>
> the Arduino can not supply the energy needed to activate two relays.

I think only one relay is involved.

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:40pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/7 "2023-09-22T17:40:11Z")

</div>

I am using a relay module which can drive by arduino supply. I tested with simple relay on and off sketch.

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 22, 2023, 5:43pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/8 "2023-09-22T17:43:40Z")

</div>

So these "sets of open wire" are effectively just a switch to ground?

 ![image](https://europe1.discourse-cdn.com/arduino/original/4X/1/7/5/17547f8361c3a44e93f2b4cf4629a812a4239b20.jpeg)

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:47pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/10 "2023-09-22T17:47:12Z")

</div>

yes that is what I mean. But we cannot stick to the ground pin any pin is fine for me but 2 sets of open wires required for me

---

<div class="post-metadata">

**Author:** ![awneil](https://avatars.discourse-cdn.com/v4/letter/a/49beb7/32.png) [@awneil](https://forum.arduino.cc/u/awneil)\
**Post date:** [September 22, 2023, 5:48pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/11 "2023-09-22T17:48:05Z")

</div>

So where's the difficulty - you just read them exactly as you would read a switch or button that connects to ground ? 🤔

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:49pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/12 "2023-09-22T17:49:14Z")

</div>

Did you go through my actual requirement?

---

<div class="post-metadata">

**Author:** ![ledsyn](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ledsyn/32/1203827_2.png) [@ledsyn](https://forum.arduino.cc/u/ledsyn)\
**Post date:** [September 22, 2023, 5:53pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/13 "2023-09-22T17:53:21Z")

</div>

Good, now to expand; What will the blue and red pairs connect to?

While at it, someone else mentioned how to drive a relay, and you don't wanna drive it directly from an Arduino pin - even if it works now!

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 22, 2023, 5:54pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/14 "2023-09-22T17:54:43Z")

</div>

BTW, never power a relay with the Arduino 5v pin.

* * *

- What is your background in electronics and software ?

- What relay are you using ?

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:56pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/15 "2023-09-22T17:56:03Z")

</div>

yes for testing purpose I am driving the relay from arduino supply but once I completed with my requirement I will use external supply for relay.

you can assume that the red and blue wire sets are just contact pins

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:58pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/16 "2023-09-22T17:58:08Z")

</div>

Yes Larry I know this but for testing purpose I am using arduino 5v to drive relay

I am a student of electronics and computer science and started learning now.

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [September 22, 2023, 5:58pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/17 "2023-09-22T17:58:54Z")

</div>

> [@chandu-mca06](#):
>
> we cannot stick to the ground pin

What means this?

Doesn't a pair of wires, when connected, complete a circuit from the input pin to ground?

a7

---

<div class="post-metadata">

**Author:** ![evanmars](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/evanmars/32/217678_2.png) [@evanmars](https://forum.arduino.cc/u/evanmars)\
**Post date:** [September 22, 2023, 5:59pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/18 "2023-09-22T17:59:07Z")

</div>

So, are you asking for somebody here to write the program for you?  
[https://forum.arduino.cc/c/community/jobs/42](https://forum.arduino.cc/c/community/jobs/42)

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 5:59pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/19 "2023-09-22T17:59:47Z")

</div>

I mean we have a choice to use any pin instead of ground pin

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 6:00pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/20 "2023-09-22T18:00:46Z")

</div>

Not exactly. I tried but the code is not working

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [September 22, 2023, 6:01pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/21 "2023-09-22T18:01:16Z")

</div>

In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the **complete sketch**.

Use the **\< CODE / \>** icon from the ‘posting menu’ to attach the copied sketch.

* * *

- What relay are you using ?

---

<div class="post-metadata">

**Author:** ![evanmars](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/evanmars/32/217678_2.png) [@evanmars](https://forum.arduino.cc/u/evanmars)\
**Post date:** [September 22, 2023, 6:02pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/22 "2023-09-22T18:02:07Z")

</div>

Maybe you can show your code and explain exactly what you mean by "code is not working."

[https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681310](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/681310)

---

<div class="post-metadata">

**Author:** ![chandu-mca06](https://avatars.discourse-cdn.com/v4/letter/c/bc79bd/32.png) [@chandu-mca06](https://forum.arduino.cc/u/chandu-mca06)\
**Post date:** [September 22, 2023, 6:03pm UTC](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160/23 "2023-09-22T18:03:35Z")

</div>

const int relayPin = 6; // Define the pin connected to the relay module  
const int firstWirePin = 7; // Define the pin for the first set of wires  
const int secondWirePin = 8; // Define the pin for the second set of wires

bool relaystatus;  
bool secondwirestatus;

void setup() {  
pinMode(relayPin, OUTPUT);  
pinMode(firstWirePin, INPUT\_PULLUP);  
pinMode(secondWirePin, INPUT\_PULLUP);  
Serial.begin(9600);  
digitalWrite(relayPin, HIGH); // Initially turn off the relay  
}

void loop() {  
// Check if the first set of wires is connected  
if (digitalRead(firstWirePin) == LOW) {  
digitalWrite(relayPin, LOW); // Turn on the relay  
} else {  
digitalWrite(relayPin, HIGH); // Turn off the relay  
}

// Check if the second set of wires is connected  
while (digitalRead(relayPin) == LOW) {  
delay(10000); // Wait for 15 minutes (900,000 milliseconds)  
digitalWrite(relayPin, HIGH); // Turn off the relay

```
Serial.print("relay");
relaystatus = digitalRead(relayPin);
Serial.print(relaystatus);

      Serial.print("secondWire");
  secondwirestatus = digitalRead(secondWirePin);
  Serial.print(secondwirestatus);
  
if (digitalRead(secondWirePin) == LOW) {
  Serial.print("inside secondWire");
  digitalWrite(relayPin, HIGH); // Turn off the relay
  delay(10000); // Wait for 15 minutes (900,000 milliseconds)
  digitalWrite(relayPin, LOW); // Turn on the relay
}
else{
   Serial.print("in else secondWire");
  secondwirestatus = digitalRead(secondWirePin);
  Serial.print(secondwirestatus);
}

```

}

// If the second set is completely disconnected, exit the loop  
while (true) {  
if (digitalRead(secondWirePin) == HIGH) {  
digitalWrite(relayPin, HIGH); // Turn off the relay forever  
break; // Exit the loop  
}  
}  
}

The relay is on by connecting first set of wires but there is no response in relay if I connect second set of wires

[Next page](https://forum.arduino.cc/t/want-to-turn-on-a-relay-by-connecting-2-wires/1171160.md?page=2)
