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