Help with Relay and Photon

Hello, another newbie question

I have a Photon and a four channel relay. I hook up the relay using the VIN and it turns on the relay board when I apply power. When D0 is hooked up and the code has the line below in the setup section, it also opens the relay.

pinMode(D0, OUTPUT);

Is there anyway to prevent it from opening the relay when the power is applied. I’m sure it is something simple…

Thanks

@StevieMac, can you provide a link to your “four channel relay” description or spec? Running pinMode(D0, OUTPUT); will set the pin as an output (from being a high impedance input) and set the pin to LOW. You can try and do a digitalWrite(D0, HIGH); prior to the pinMode() command to see if the pin goes HIGH on the pinMode change.

@peekay123, here is the link to the relay. https://www.sainsmart.com/relay-1/relays/4-channel-5v-relay-module-for-pic-arm-avr-dsp-arduino-msp430-ttl-logic.html

I did try adding the digitalWrite(D0, HIGH);, but it still came on.

Thanks,

StevieMac

@StevieMac, the relay board has opto-isolated inputs meaning the “input” to each relay is the anode (negative side) of a LED. The only ways I can see for this board to work the way you want ar as follows:

  1. Do a digitalWrite(D0, HIGH) right after your pinMode() command. This will turn off the relay immediately after turning it on.

  2. Add an external pull-down and inverter to D0 so that when the pin is floating (prior to setup), the input of the inverter is HIGH so it’s output will be HIGH, keeping the relay OFF. After pinMode(), D0 will be LOW, again keeping the relay OFF. When you write a HIGH to D0, the inverter output will be LOW, turning the relay ON. You can use an NPN transistor for the inverter.

You can find a good article showing different logic elements using transistors here.

@peekay123, Thanks for the article. It will give me a way to go, and learn something.

Have a great weekend.

1 Like

digitalWrite(D0, HIGH) prior setting the pinMode(D0, OUTPUT) won’t do a lot due to the sanity check in there, but digitalWriteFast(D0, HIGH) or pinSetFast(D0) should work as these functions do not care about the set pin mode and just sets the registers.

1 Like