@mehulhirpara, can you be more specific? Can you post your code so I can see what you’re doing?
Sure.
Reference Code:
#include "Particle.h"
#define RELAY_PIN D1
void setup() {
Serial.begin();
}
void loop () {
pinMode(RELAY_PIN, OUTPUT);
delay(5000);
pinMode(RELAY_PIN, INPUT);
delay(5000);
}
@mehulhirpara, the reason this works is because pinMode(RELAY_PIN, OUTPUT);
sets the output LOW by default, turning on the relay (ie activating the LED in the photocoupler). Then, pinMode(RELAY_PIN, INPUT);
set the pin as an input which is high impedance or “floating”. This disables the current to the photocoupler, turning off the relay. This works but somewhat unintuitively.
Thanks @peekay123 for the explanation. Since writing HIGH/LOW to any PIN doesn’t have any effect and current approach works well for me, I will continue with this until I add relay driver circuit.
So what you have tested with is something like the following and it does not work ?
#include "Particle.h"
#define RELAY_PIN D1
void setup() {
Serial.begin();
pinMode(RELAY_PIN, OUTPUT);
}
void loop () {
digitalWrite(RELAY_PIN, HIGH);
delay(5000);
digitalWrite(RELAY_PIN, LOW);
delay(5000);
}
it didn’t work. No difference between HIGH and LOW, relay goes into open state from the moment I defined PIN Mode as OUTPUT and it always remains there.
@mikemoy, the relay is driven through an optocoupler. The input to that coupler is the anode of a photo diode. The other side of that photodiode is a current limiting resistor going to the same power used by the relays, namely 5v. When the Photon pin is driven LOW, the pin goes to GND and the photodiode is properly biased, driving the optocoupler and turning on the relay.
When the Photon pin is set HIGH, the pin voltage goes to 3.3v. However, now the voltage across the photodiode is 5v - 3.3v = 1.7v which is enough to keep the photodiode biased and turned ON. This is why the relay never turns off. With pinMode(RELAY_PIN, INPUT)
, the Photon pin goes into high impedance mode, leaving the anode of photodiode floating, thus turning it off and the relay with it.
For correct operation using digitalWrite()
, a transistor circuit is required between the Photon pin and the photodiode.