I have a opto-isolated SPDT relay with the lamp I am controlling connected to the normally open [NO] contacts. That means when the Spark-relay combo has no power the lamp is OFF. To keep the NO contacts open (to keep the lamp OFF) the input signal must be HIGH. This I found surprising but apparently it is how most relays usually operate, is that correct?
My issue is this. When I power up the Spark-relay combo the lamp comes on for a short while because the default state of the D0 pin is LOW, until my app kicks in and raises the signal HIGH. I would prefer the lamp stays off until the program kicks in.
I’m familiar with pull-up and pull-down resistors for analog signals. Can I wire up D0 such that D0 is normally HIGH, at start up?
One way might be to put an inverter between D0 and the relay and also invert the program logic. That would mean that with no power the NO contact would remain open as currently. The inverter would ensure that the signal to the relay remained high at power-up. The default low state of the D0 would signify OFF and the program would keep it low at power up. The inverter could be a single NPN transistor with the base connected to D0 via a 4k7 resistor, the emitter connected to ground and the collector connected to the relay input. I’m assuming the relay input drives an LED and with its anode connected to Vcc.
@psb777
A Normally Open (NO) contact is the one that remains open (disconnected) from the COMMON when no power is provided, that is, when input signal is LOW, unless there is an inverter at the relay’s input. You can use an external pull up resistor to keep the default state to HIGH at all times.
You want the Arduino pin to go high at the point the device gets power in order to keep the relay from activating. The problem might simply be a timing issue as the thing is powering up. Have you tried putting an electrolytic capacitor, say 4.7microF across the input to the relay, the idea being to pull and keep the input to the relay high until Arduino had initialised?
I am puzzled, though, because, the Arduino pin starts it’s life at power-up as an input, which should not activate the relay. Does the thing flash if you power the thing up WITHOUT your software running, or if you connect the relay input to a pin that isn’t affected by the program?
The pullup resistors are controlled by the same registers (internal chip memory locations) that control whether a pin is HIGH or LOW. Consequently, a pin that is configured to have pullup resistors turned on when the pin is an INPUT, will have the pin configured as HIGH if the pin is then switched to an OUTPUT with pinMode(). This works in the other direction as well, and an output pin that is left in a HIGH state will have the pullup resistors set if switched to an input with pinMode().
Prior to Arduino 1.0.1, it was possible to configure the internal pull-ups in the following manner:
pinMode(pin, INPUT); // set pin to input
digitalWrite(pin, HIGH); // turn on pullup resistors
Maybe putting these two lines into your initialisation code would ensure that the pin doesn’t go low at start-up and then the light wouldn’t flash.
Pin LOW = relay ON
So D0 as an input at startup is 0V. That activates the relay ON.
Your inverter is a good idea.
The capacitor seems no easier than the transistor and would be unreliable as (a) you have no idea how long it is until the app on the Spark starts running - it varies because the WiFi connection must be obtained first before any app code is run and (b) you want the transition on the relay signal to be quick.
I don’t really want to have a discussion about the merits or properties of any one relay. As it is behaving properly and exactly as expected I’m not going to tell you what it is precisely to avoid that! It is a SPDT opto-isolated relay, the lamp is connected to the normally open side. Signal HIGH=OFF. What I want to work out is how to hold the signal line HIGH from power up until my program starts running.
The external pull up resistor is easier than the transistor inverter. Is this how it works? D0 starts as an INPUT so has high impedence so the pull up resistor keeps the signal at 3v3. This keeps the relay OFF. The Program starts running. I set the signal HIGH and switch it to being an OUTPUT. This keeps the relay OFF. I want to limit the current sunk thru D0 so I include a resister there too.
I'm guessing you have one of those back asswards SainSmart relay boards. You could probably modify your relay board to just work the correct way. This is not a normal way of doing things.
That said, the D0 pin should not power up LOW. It should power up as an input, i.e. HIGH IMPEDANCE aka FLOATING. Which will not be HIGH, so it appears to be LOW. Make sense? If you supply an external pull up resistor to your relay power (hopefully 3.3V to 5V), then it should not momentarily turn on during Spark Core power up. Make sure you are using 5V tolerant Digital Outputs if you are tying the pullup to 5V. D2 is not 5V tolerant.
Yup. @BDub is right: I think I have a cheap Chinese clone of a SainSmart relay. Very odd, I think. None of the keep high techniques seemed to work. And I tried everything including changing from D0 to A0. What did work, so easy, was @mstclair87’s suggestion of a transistor inverter. I used a 2N2222 and a 1K resistor (what was closest to hand) and swapped the inverted the code and all works fine.
I have the 4-relay version of this board and discovered the same issue with the inversion. While I’m okay to live with that problem until I can get some 2N2222’s, I found another problem, err, well, have some questions.
It says this is a 5v board, but the Core only supplies 3.3v. When I connect the board to the Core 3.3v and GND and toggle the relays, the red LEDs light up, but I don’t get any relay power. I then found this question and answer thread on the product page.
Would I need to power this with a 5v source for VCC and GND and then connect the D* pins on the Core to the input pins on the relay board?
Do I need to make sure I use the 5v tolerant pins?
Do I need to tie the Core GND to the 5v GND?
Since I’m still powering the Core using USB, does VIN provide any voltage that I could use to power the relay board, or would I need another source?
This board was in “production” using a spliced cell phone charger (pre-USB) and mechanical rocker switches to toggle the voltage to the relay input pins. I have it in an outdoor enclosure, and it worked fine using an ethernet cable from the relay to 4 rocker switches, but it was ugly. I think a simple web app that I can control from my devices would be cleaner and only just a little more inconvenient. I mean, who doesn’t want to use their phone to control a set of deck lights? Maybe I can use another Core inside with the rocker switches to wirelessly toggle the Core connected to the relay!
I don’t think you are getting any input from the relay board so no worries about using a 5v tolerant pin. But to be on the safe side, just use if you can.
I’m not sure but there should be 3 pins, V+, GND and input. So you can use Vin to feed the USB +5V to V+ and use the normal digital IO to on and off. You will be able to trigger it.
yes you need all 3 pins from the relay to the core. So 3 wires, V+, gnd and signal pin.
Im typing this on the go but will check back on the relay on machine late4
I bought http://www.ebay.co.uk/itm/190851456681 for ÂŁ1.10 including delivery. I chose it because it is opto-isolated meaning I need not worry about reverse voltages and induction spikes. I am driving a low current 240V lamp currently testing in prep for asserting my 2nd Amendment Rights (controlling the central heating in the face of safety police, here and elsewhere).
The relay says it is 5V and that 15-20mA is drawn. The spark is USB powered. From the Spark’s 3v3 line it has been drawing 33-37mA. I did try a 100R resister to limit the current but it does not like that, I guess because it is only 3.3V. When I’m back home Tuesday I will switch over to using Vin (thanks @kennethlimcp) and I hope then to re-install the 100R currrent limiter.
The signal line (I’ve used both A0 and D0) draws only 3-4mA. I have had the relay working reliably for days now, typically switching the lamp every few minutes.
The lamp is very close to and pointed at a Spark-TMP36 combo which will be my central heating thermostat. Thus I test my control system. I am now testing failure modes - making sure that the Spark-relay turns itself off if nothing is heard from the Spark-TMP36 thermostat for 5 minutes and making sure that if anything else breaks that the relay is off.
Ah! My previous attempt at a pull-up resistor must not have worked because the relay-signal needs to be 3mA. The resistor was too large. I had a 10K one. I’ll replace that with 1K or 470R and try again.
@wgbartley, if you see light on the status LED, then the Opto should be triggered and the relay clicking away. Try 3.3v and if it doesn’t work, try 5v.
I am assuming that green diode is an LED in the schematic above. That is causing you some problems at 3.3V since it has a Vf of 1.8 to 3.8 V depending on the exact technology, color etc… It probably works OK if that is a red LED with a lower Vf but a green would be a problem at 3.3V. If you are having trouble getting the core to drive the relay, you could try shorting out the status LED there.
I don’t think you’ll have trouble running one of these relays off of the USB power to the core, but I would not try to run too many of them at once. Usually the coil resistances are pretty low on these things, 100 ohms or so. The two power connection are separate so you can completely isolate the high voltage side from the low voltage side–a code requirement in the USA.
Forgive my ignorance, but is this in lieu of the 2N2222 to invert the relay on/off, or in addition to?
It's a red LED, but I wasn't getting any clicky off just the 3.3v. I changed the VCC and GND on the relay board to connect to VIN and GND on the left side of the Core. I have clicks and blinky (CFL) lights now!