Spark + Sainsmart 2CH 5V Relay

First, I’m a newb at this so please be patient… :wink:

I have my core hooked up to a Sainsmart 2CH 5V relay as seen here:

I’m having a problem “activating” the relay using the digital pin.

Core 5V -> Relay VCC
Core GND -> Relay GND
Core D3 -> Relay IN1

Relay JD-VCC/VCC Jumper Installed

Code:

int RELAY_IN1 = D3;

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

void setup() {
    Spark.function("relay", closeRelay);

    pinMode(RELAY_IN1, OUTPUT);    
    digitalWrite(RELAY_IN1, HIGH);
}

void loop() {
}

When closeRelay is called, I can see the LED on the relay toggle so it is getting the signal, but the relay never “clicks” as if it is switched.

If I physically take the lead from D3 and connect it to ground it will “click”.

What am I missing here?

Thanks,

Mike

So, your 3.3V output on D3 may not be enough to trigger your relay, considering that it is 5V.

So, experiment first with getting it to cycle with 5V from the power supply directly. If that works, your relay is OK… you can use a transistor or a MOSFET to switch the 5V with your 3.3V output pin. Google transistor switching…

I agree makes sure you power you relay module from the 5V raw not a 3V pin. Also take note if you have it wired as normally open or normally closed. I also believe there are some jumper settings on the module may want to take a quick look at the sainsmart documentation. I have used them before and they always worked well. If all that fails you may have bad relay.

OK, so like I said I’m pretty new to working with electronics and such so please bear with me…

First off, I’ve read somewhere that this relay is a LOW active relay.

Second, I failed to mention that I am using the Shield Shield that I got with my spark to power the relay.

I have a battery pack that contains 5 AA batteries connected to the Shield which has a “power header” that has the following pins:

Reset
3V3
5V
GND
GND
VIN

I have the 5V and one GND connected to the relay.

Now if I remove the core from the equation completely and use only the Shield, the only way I can get the relay to “click” on is by grounding (correct term?) the IN1 pin on the relay, by this I mean physically touch the wire I have connected to the IN1 PIN to the rail I have grounded on my breadboard.

If I understand the current design, what is happening is that I am setting D3 to HIGH when the core runs through setup which is essentially the same as sending 3.3V to IN1 all the time?

When the function closeRelay is called it sets D3 to LOW which I think means 0V (or ground) correct?

If the above is true, then what would be the difference between setting the pin LOW and physically grounding it by touching ground? Isn’t that technically the same thing?

You set pinMode to output in setup() but perhaps the voltage differential is not adequate.

You can try a transistor to switch it with 5V.

@hutt21 - The logic on the relay boards is very confusing. They are ‘active low’ which means that grounding the circuit activates the relay, but what effect that has on your circuit really depends on if you use the NC (normally closed) or NO (normally open) pin as your ‘output’. What the logic of the relay does internally is irrelevant, it is the choice of NC or NO that matters.

Even though the relay is ‘active low’, the LED lights when the signal is high, which is very confusing.

Realistically, using a 3.3V signal on a 5V relay circuit will not trigger the circuit reliably. Your circuit is probably using an opto-isolator, which is basically an LED shinning on a detector inside a chip, and the LED on a 5V design does not reliably shine bright enough to trigger the relay circuit of the detector if it only sees 3.3V.

You need a device, either a transistor circuit or a level-shifter in order to reliably control a circuit that is intended for 5V to work with a 3.3V signal.

Thanks to everyone who responded and got me pointed in the right direction. I was finally able to get the spark to trigger the relay last night using a npn transistor. I just want to make sure how I did it is correct.

I now have and external 5V power source to power the relay and have tied all the GNDs together on a single rail on my breadboard.

I have the collector connected to GND, the base connected to D3, and the emitter connected to IN1 on the relay. When D3 goes LOW, the transistor “opens” the circuit and when D3 goes HIGH the transistor “closes” the circuit at which point the relay “clicks”.

Does that make sense and is it how I should be using the transistor?

Thanks again for the help!