Want to turn on a relay by connecting 2 wires

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
}
}
}

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?

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.

I think only one relay is involved.

1 Like

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

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

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

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

Did you go through my actual requirement?

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!

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


  • What is your background in electronics and software ?

  • What relay are you using ?

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

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.

What means this?

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

a7

1 Like

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

1 Like

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

Not exactly. I tried but the code is not working

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 ?

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

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