Issue driving a 5V relay from Photon

Hi,

I apologize in advance as I feel like this question is really straight forward, but for the life of me, I can’t figure it out. I should also add that I am fairly new to this, so bear with me.

Here is what I am trying to do: I am trying to control the opening and closing of a 5V relay (JZC-11F) from pin D0. I feel that this is as basic as it comes, and yet I can’t get it to work.
Here is a quick and dirty diagram of my connections:


Here is a picture of the wiring, as I am new to Fritzing, so not sure my diagram is accurate.

D0 goes to one side of the coil, with the other side being connected to GND. I have the + side of the relay connected to the VIN and the NC side connected to an LED. I have put an LED in the D0 as well to verify that it was providing current.
The code is as simple as it gets:

pinMode(D0, OUTPUT);
digitalWrite(D0, HIGH);
delay(1000);
digitalWrite(D0, LOW);

The LED that is connected to D0 and to the coil flashes, but the LED controlled by the relay doesn’t. If I plug the coil directly into the 3.3V rail, then I can hear the relay opening and closing as I connected it and disconnect it, so I feel that it is an issue with D0 not providing enough power to the relay, but from everything I have read, this should work or am I completely wrong here?

Thanks a lot in advance for the help and my apologies if this was covered somewhere else, but I did some searches and couldn’t find anything helpful.
Cheers,
B.

You need to know that the max current any GPIO on the Photon can drive is 25mA.
This might not be enough for the relay coild to fully engage the contacts.

You should use a transistor to switch the higher current either from Vin (5V) or 3v3 (may be problematic with a 5V relay in the long run).

You can have a look at the schematics of the Particle Relay Shield how it’s done there
https://github.com/particle-iot/shields/blob/master/photon-shields/PDFs/Schematics/relay-shield-v310.pdf

You will also need a flyback diode to avoid permanent damage to the photon.

I suggest you cease experimenting until you have a diode in place, but you may already have damaged the photon beyond repair.

3 Likes

@ScruffR: I have checked and the minimum switching load for this relay is 10mA 5VDC, so, if the max current for any GPIO is 25mA, then it should work, shouldn’t it?

@andyW: I have now added a flyback diode to the circuit. Thank you for the advice, but my issue persists.

AFAICT that 10mA @ 5V is the minimum switching load not the typical and 10mA @ 5V would at least require 15mA @ 3.3V for the same wattage, but with lower voltage you'll probably need more than that to create the same magnetic field to produce the force to pull the contacts.

And don't underestimate the flyback voltage when depowering the coil (as @AndyW) has noted - another fact that's well represented in the Particle Relay Shield schematics I've linked above.

Have you tried a different pin? If D0 was already frazzled, you won't have much luck with it anymore.

@ScruffR : so you are basically saying that the Photon doesn’t generate enough current to power this relay? If that is the case, then I am surprised that it is included in the Photon Maker’s kit, which is where I got it from. I have added the diode now to protect the Photon. :smile:

I have moved the connection from D0 to D1 and updated the code to no avail. :frowning:

Are there no transistors in that kit?

There are also LEDs in the kit, but if you use them without the also included resistors you'll soon see that they'll be short lived :wink:

@ScruffR : There is one transistor in the kit, but I haven’t figured out yet how to use it, which is why I was thinking that the relay would be easier, but apparently I was wrong. For my end project though, I will need to use relays as they will be controlling powering up sensors and valves, but in light of this, I decided to purchase the Particle relay shield as it seems pretty fool proof. :smiley:

Each LED is protected by a 220ohms resistor.

Yeah, sorry the maker kit tutorial doesn’t include an example of using the relay.

I’m not even sure what transistor is provided, once we know if that is npn or pnp, we can likely help you get it set up correctly.

The relay might well be a 5V part (sorry, once again I don’t know the exact model) and so won’t actually operate off 3.3V when the additional voltage drop of the output pin circuitry is taken into account.

Once we get you working, we can point other maker kit users here who have the same question as you.

1 Like

@AndyW: The transistor included in the kit is a NPN transistor.

Sorry for the delay, I hoped to get something prettier, but instead of waiting until I get time with real software perhaps this photo will suffice (click on it for an uncropped version):

Google Photos

This assumes that the relay really is 5V as you say; it makes sense that it would be. Make sure that you connect the ground to the photon and use the Vin pin on the photon as the source of the 5V for the relay. The value of the 1K resistor between the photon’s output pin and the base of the transistor is pretty non-critical, you can probably use any value that you have on-hand up to at least 10KOhm.

Corrections/questions/better schematics welcome.

@dbblackdiamond, let us know how you get on, and here’s hoping the photon is undamaged.

1 Like

@AndyW: Thank you very much for taking the time to draw this. I think it makes sense to me and I will give this a try tomorrow to see how it works. If I can manage to make it work, I guess, I will have to choose whether I want a relay or just use a transistor as the switch. Honestly, right now, I am not too sure which one I will go with as I have been experimenting with a transistor and I kind of like the simplicity of it.

Multiple ways to skin the cat, I guess!!!
Thanks again for doing this. This is pretty awesome.
B.

1 Like

Thanks to the help of @ScruffR and @AndyW, I have managed to make everything work the way I envisioned it. I ended up using MOSFET and H-Bridge to do the switching on and off for me, as I felt it was simpler using a relay and it all works fine: I can read the value from the sensor and activate the valve based on that value!!! :smile:
I am using LEDs to simulate the activation and deactivation of the valve, while I am waiting for the valves to be delivered. The red LED simulates the valve being turned off and the green LED simulates the valve being turned on and they blink when they should, which is a bonus!!!

Here is the finished prototype. My apologies for the naked wires to the sensor, I am waiting for the heat shrink to be delivered.


Now I just need to figure out the battery/solar charging side of the project.

3 Likes

Glad to hear you got it working the way you wanted it.

Glad you got it working! I’m smiling because I have a breadboard and rat’s nest of wires sitting in front of me that looks a lot like yours. :slight_smile: Next step is sending off for the PCB.

1 Like

@picsil: LOL… I am at the crawling stage right now. PCB would be the walking stage, I am not there yet. :rofl::rofl::rofl:

1 Like

Yeah, but you’re both rolling your sleeves up and trying things, that is what really matters.

5 Likes

A 3v3 to 5v logic converter is probably your best bet. That will undoubtably make your circuit more complex, as it will mean needing to supply a 5V power rail, but it should give you all the power you need for a relay.

I’d suggest one of these: 4-channel I2C-safe Bi-directional Logic Level Converter - BSS138

Wire VIN to HV and 3v3 to LV on the breakout board, then your output pin (D0) to any of the low side inputs (A1 through A4) and the control pin of your relay to the corresponding high side outputs (B1 through B4, respectively). That should give you all the power you need to run the relay.

1 Like

@Leo_G: Thank you very much for the input. Based on @ScruffR and @AndyW’s feedback, I decided to simplify my circuit and use MOSFETs and an H-Bridge instead of the relay and so far, I am really happy with the setup. It is fast, trouble-free and simple.