Trouble with 2Channel Relay

Newbie here so apologies in advance. I have a photon wired to a 2Channel relay to open/close my garage door. It was working perfectly fine for a few hours and I left the house. When I came home my neighbor told me my garage door was opening and closing repeatedly. No matter how I wire this thing (use 1 relay or both relays) or write the code the relay will just randomly close and open again. Is this some sort of power issue? I am using USB to power the photon.

Thanks in advance!

Which relay are you using?

It is your standard 2Channel 5V relay shield. Couldn’t tell you a name brand but they are all almost identical. Sorry I can’t be more descriptive :grin:

Ok… Don’t you remember where it is bought? The reason i am asking is to see what is the configuration on the relay for the on/off control circuit.

Is it active high or low? Trying writing a simple code that sets it high/low to turn off the relay and see if the random switching still occurs.

this is the one I bought awhile back

I know HIGH closes the relay and LOW opens it back up so the circuit is disconnected.

What is weird is that it was working perfectly fine for quite a few hours then kind of just went on the fritz closing and opening at random.

This is my code:

int relay1 = D0;

void setup()
{

Spark.function(“relayOne”, relay1Pulse);

pinMode(relay1, OUTPUT);

}
void loop()
{

}

int relay1Pulse(String command){
digitalWrite(relay1,HIGH);
delay(1000);
digitalWrite(relay1,LOW);
return 1;
}

I wonder if the current through the relay is too high and damaged the relay…

Does it still toggle randomly? Might be good to disconnect the door motor and see what happens :slight_smile:

yea i have it disconnected now just sitting in my lap. The relay will just randomly close and open back up even if I do not call the relayOne function (though that works too :stuck_out_tongue:) I really don’t want to pay $30 for the RelayShield as that will push the cost of this project over some off the shelf options. If I get rid of my garage code and flash Tinker to my photon the relay does NOT randomly open and close

Can you try to wire the signal pin for the relay directly to GND (LOW) and see if that still happens?

sorry trying to understand. I know there are 2 GND pins on the photon but didn’t realize they are different.

right now I have:
VIN going to VCC on the relay
GND (next to VIN) going to GND on the relay
and GND (next to D7) going to IN1 on the relay

…doesn’t click like this

The GND pins are all the same.

Can you try another Pin like D1 and see what happens?

Yup same problem. Interestingly enough I tried using the Blynk app and I can create a momentary button that toggles the relay to make it work. No random opening and closing. This isn’t an ideal solution though since I cannot integrate IFTTT proximity recipes :confused:

I wonder if your 3.3V pins are enough to properly switch your 5V relay… but you say it works properly using BLYNK, so it seems it may.

you could try transistor switching 5V or level shifting and rule out anything electronic.

I had some issues getting a relay to toggle off correctly. I suspect your pin is floating and the relay has a drive transistor which is a very sensitive FET. Try this:

int relay1 = D0;

void setup()
{
	Spark.function("relayOne", relay1Pulse);
	pinMode(relay1, INPUT_PULLDOWN);
}
void loop()
{

}

int relay1Pulse(String command){
        pinMode(relay1, OUTPUT);
	digitalWrite(relay1, HIGH);
	delay(1000);
	pinMode(relay1, INPUT_PULLDOWN);
	return 1;
}

The pulldown will sink out and residual voltage on that pin so the transistors gate cannot float high.

I’ve had some of the exact same issues. My wife woke up early on Saturday morning to the garage door going up and down. Funny how toggling - in a loop - means it will continue to loop and continue to toggle. :wink:

I’m pretty much a newbie too, so much of this is a guess (although our garage door going up and down all night was not a guess!). But, could the brackets in your first code example be causing part of the problem? From this page the setup and loop functions each need to be contained in open/close brackets:

// actions are performed in the functions "setup" and "loop"
// but  no information is reported to the larger program

void setup()
{
  // ...
}

void loop()
{
  // ...
}

The brackets in your code don’t seem to be enclosing the functions. I don’t have the vaguest notion if that’s causing the issue, but I thought it might be worth a look.

In my garage door expedition Part B, I am using Tinker with the Photon Relay Board. I just push the respective Digital Write Button to go HIGH and then push it again to go low: the poor man’s version of a momentary. Someday, when I understand more, I’d like to improve that situation.

I wish you all the best.

Mike

Good thinking though not quite right this time. Functions can be defined in the top level scope so this won’t be his issue. His control flow is like this (pseudo code)

define a variable relay1 globally for all functions

begin setup function (executed once by the system)
	register a relay1Pulse with the cloud API
	set initial state of relay1 to output
end setup function

begin loop function (called many times by the system)
	do nothing!
end loop function

begin relay1Pulse function (only called remotely via the cloud API)
	relay on
	wait one second
	relay off
	reply to the caller with a value 1 to show them we succeeded
end relay1Pulse function

The control flow doesn’t enter functions as they are defined.

Hi, Totally not helpful for the actual relay issue. However my garage door remote was a 3V version. So I shorted the button on the remote and then have the photon to give me 3V to the battery contacts for 1/2 a second and go low again. Works like a charm.
HTH
Gerry