pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Wiring relay to Pi

Fri Mar 29, 2019 2:18 am

I have a Pi 3 B+
Would the pi be able to power a 3V relay?
Anyway, I put an external 3.3V source to power the relay, shared the GND of the source with the GND of the pi. I am using GPIO 21.
I wrote a few lines and I got the relay to click, but when I tried again I noticed the red light on the pi went off. As I wasn't sure if the relay is overloading the pi, i decided to stop testing. I noticed the red LED would go off once in a while and come back, but the pi works fine. I put another pi and found that i get the same thing with the red LED (i didn't use a relay on the other pi), so I guess this is normal.

My question is, does my setup sound right or do I need to add some kind of protection in between. From what I understand the relay has a built in opto-isolator.

Orange +3.3V of power source to Vcc relay
Brown GND of power source to GND Pi to GND relay
Yellow GPIO21 to In of relay
IMG_3516.JPG
IMG_3516.JPG (144.06 KiB) Viewed 1430 times
The attachment IMG_3516.JPG is no longer available

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Fri Mar 29, 2019 9:35 am

My first question would be what is the coil voltage on the relay ? ( I bet it will not be 3.3v )

second is the relay turned on when you set the gpio to low.

and lastly no the red LED on the pi should not go out at any point when using the relay or not so something is not right probably your power supply is not suitable.

also not sure your relay has an opto isolator but cant see clearly to be sure so you maight want to post a better picture of just the relay board.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Fri Mar 29, 2019 1:21 pm

GPIO.output(gpio_relay_port, True) turns on
GPIO.output(gpio_relay_port, False) turns off

The light on the relay goes on, but it doesn't look its fully powered. I once heard a click after a few trials.
I can't find the exact specs for this relay, but a similar one suggests that coil is indeed 3v.
I can try a different power supply later and see, just in case it is fried. The output measures 3.3v though. They take 12V input. I do use these types of power supply for arduinos and nodemcu and they are usually great.
relay.JPG
relay.JPG (84.9 KiB) Viewed 1386 times
More Info
http://sun-light.com.sg/index.php?route ... ct_id=3877
https://upverter.com/parts/Songle%2520R ... C-3F-5VDC/

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Fri Mar 29, 2019 2:03 pm

So the relay is indeed 3v so your 3v PSU should power it ok , but it does not have an opto isolator on it, so it may be drawing to much current from the gpio.

Not on my pc at the moment but later when I am I will draw you up a diagram to add on.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

PhatFil
Posts: 1367
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Wiring relay to Pi

Fri Mar 29, 2019 2:30 pm

the link provided suggests the 3v relay coil could draw 15 - 20ma when activated while the pi gpio can supply 16 ma,

the dim led would suggest your relay board is drawing closer to 20ma.

might be worth determining the led draw and if in the 4-5ma range pulling it may resolve the issue

Otherwise I think you will need some additional circuitry perhaps a transistor to switch power sourced from the 5v Voltage in passed through a voltage divider to drop to 3v3?

https://electronics.stackexchange.com/q ... spberry-pi
https://learn.sparkfun.com/tutorials/vo ... viders/all

I dont know anywhere near enough about electronics to explain, i just learn the mantra that with DC the draw MUST ALWAYS be less than the supply capacity in order to keep the magic smoke contained in the supply ( in this case your pi).

User avatar
NGC6543
Posts: 75
Joined: Thu Feb 28, 2019 7:30 pm
Location: Lancashire, UK

Re: Wiring relay to Pi

Fri Mar 29, 2019 3:57 pm

I agree with PhatFil. Pulling the LEDs might well solve the issue by lowering the current required; depends if they used low current (2mA) LEDs or not.

The relay is driven through the transistor that can be seen on the board. My guess is the transistor is connected common collector, rather than in the usual switch arrangement, to give a high input=on. It will drop 0.7V so the relay is only seeing 2.6V from a 3.3V input. That makes it pull in a bit weakly but probably still within its minimum pull-in voltage.

If the GPIO voltage then drops a little due to the high current draw, that will make the electromagnet even weaker though, and it will struggle. Which sounds like what is happening with it sometimes making it but often not.

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Fri Mar 29, 2019 8:27 pm

So here is the diagram to add an opto isolator between your pi and the relay board.

Image

I would suggest a 4N25,26,27,or 28 or a 4N35,36 or 37 as the opto isolator.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Sat Mar 30, 2019 1:21 am

Looks easy. I wish there was a way to use the more common 5V or 12V relays. I think if I knew i had to use an external power supply, i would've probably chose a 5V relay with a logic level converter.
Anyway, what is the difference between 4N25, 26, 27. The specs show the CTR is different, but I am not sure what that is.

Also how is the transistor on the chip powered; aside from the 3V, don't I need a GND wired as well?

User avatar
rpdom
Posts: 15184
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Wiring relay to Pi

Sat Mar 30, 2019 6:18 am

You could use a 5V or 12V coil relay with that circuit, just replace the 3V PSU with an appropriate voltage one.

The transistor in the opto is powered from the power supply and it is grounded via the relay board back to the power supply. It shouldn't be grounded to the Pi - that is the whole point of using the opto isolator, there is no electrical connection between the Pi and the relay circuit. If the relay goes bang and throws mains voltage back into the circuit it will cook the transistor, but the Pi will be unharmed. Usually these devices can cope with up to 5000V across them before it will spark across to the input side.

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Sat Mar 30, 2019 7:53 am

CRT stands for " current transfer ratio "

https://www.renesas.com/eu/en/products/ ... y/ctr.html

but you don't need to worry any of the suggested opto isolator should work just fine.


If you decide to change your relay board for a 5v or 12v one just be aware its more common for those relay boards to be active low, which presents a different problem when using them with the pi because the 3.3v gpio can't switch them off.

viewtopic.php?f=91&t=83372&p=1225448#p1225448

but you can get around this using the same opto isolator but wired differently.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Sat Mar 30, 2019 1:08 pm

rpdom wrote:
Sat Mar 30, 2019 6:18 am
The transistor in the opto is powered from the power supply and it is grounded via the relay board back to the power supply.
What I am struggling to understand is where the transistor negative is. I can see the optical diode has a + and -, the transistor only has +3v. In my mind the other side of the transistor is output. To me it looks like we're missing the -ve. Of course all this will be cleared up when I buy the chip today, but it would be nice to understand as well. I am assuming pin 6 is not connected to anything as in the diagram.
pcmanbob wrote:
Sat Mar 30, 2019 7:53 am

If you decide to change your relay board for a 5v or 12v one just be aware its more common for those relay boards to be active low, which presents a different problem when using them with the pi because the 3.3v gpio can't switch them off.
Thanks for the heads up.

PhatFil
Posts: 1367
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Wiring relay to Pi

Sat Mar 30, 2019 2:35 pm

While there are many very good reasons to wire relays directly to your pi (ie learning) I would suggest that its something that can easily be circumvented and made a LOT easier with a nominal investment in slightly more sophisticated h/w.

FYI I have a Pi-0-w hosting mosquitto and node red. This device controls about 30 relays and other devices wirelessly. Many smart sockets and devices that contain an exp8265/6 soc can often be simply reprogrammed to respond to local Mqtt commands and config.
Currently its even possible to reprogram one family of devices OTA without any physical intervention (tuya h/w / smart-life app compatible devices) https://github.com/arendst/Sonoff-Tasmota/wiki

Smart relays can be sourced from $5, in a smart socket format $10-15, 4x relay packages circa $20 etc..

EXpansion of a hard wired relay system involves system down time, a system based on wireless mqtt communication can be expanded by flashing a new device and configuring it to join your network a 10-20 minute task without disrupting service at all.

Just my take..

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Sat Mar 30, 2019 2:43 pm

pi9 wrote:
Sat Mar 30, 2019 1:08 pm
What I am struggling to understand is where the transistor negative is. I can see the optical diode has a + and -, the transistor only has +3v. In my mind the other side of the transistor is output. To me it looks like we're missing the -ve. Of course all this will be cleared up when I buy the chip today, but it would be nice to understand as well. I am assuming pin 6 is not connected to anything as in the diagram.
There is no transistor negative , the transistor is just being used as switch that you can switch on and off electrically or in this case optically.

https://www.explainthatstuff.com/howtra ... swork.html

pin 6 on the chip is the base leg of the transistor, but as we are turning it on optically we don't need to use the transistor base so just leave it unconnected.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Sun Mar 31, 2019 1:15 am

I got the components. I chose the 4N26 244F (whatever that 244F is) because it specifically mentioned 4N26 on it. The 4N25 had some other numbers. I got 470R (1W) I don't think the wattage should make a difference since I am on low voltage.
Long story short, it doesn't work. I verified that when I trigger the output on GPIO21 (the one I chose), i get 3V on my multimeter. I checked the voltage between the In and GND on the relay and it is always getting 3V reglardless of whether the software has relay on or off. Between In and Vcc it voltage is always 0 regardless of whether the software is on or off.
I am using an analog multimeter for this task because my digital one sometimes takes too long and at the time of doubt i don't fully trust it.

When wiring the chip I assumed
1 (dot) 6
2 5
3 4

any ideas why it is getting a constant 3v regardless of the input?
The specs show forward voltage is 1.2-1.5V. I tried putting a 1.5V battery on the LED side to see if I can trigger the relay, but it didn't fire up. I measured the voltage after the 470R and it is 3V. Is it possible the LED is cooked? or
Should the wirings for 4 and 5 be reversed may be?

Testing the LED on pins 1 & 2
+ in my left hand, - in my right hand.
Yellow pin 1, Orange pin 2
IMG_3528.JPG
IMG_3528.JPG (154.96 KiB) Viewed 1201 times
IMG_3527.JPG
IMG_3527.JPG (153.63 KiB) Viewed 1201 times

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Sun Mar 31, 2019 5:28 pm

I did some more trials directly with the relay since I can't figure out why the opto isolator doesn't work. With an external power supply I am able to consistently turn the relay on and off with no issue to the pi. No flickering LEDs on the PI or the relay. The red LED on the Pi doesn't go off (I am sure that red LED is not just a power ON indicator because it goes off at random even with other Pi 3B+s that I've never connected to the relay.

Anyway, I put my multimeter to measure the amps, and I am getting 1mA with no load on the relay. I don't know if having a load on makes a difference. My guess is it wouldn't and shouldn't.
I also tried a 5V relay, I get 1.92mA. From what I read, a maximum of 16mA can be drawn from each GPIO not exceeding 51mA from all GPIOs combined, so it looks like I am safe for now. The Songle 5V relay clearly mentions a built in optoisolator on it, but it is not the relay i was planning to use for this project.

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Sun Mar 31, 2019 5:44 pm

From you picture I cant see the breadboard clearly enough to see if you have it connected correctly.

But if you have a 3v supply you should be able to test it without the pi.

So connect 3v+ to one end of the resistor and the other end to pin 1 on the 4N26.

Now connect pin 2 on the 4N26 to ground ( ground being the other side of your power supply.)

now connect pin 5 on the 4N26 to the 3v+ on the power supply , with the meter set to volts connect the red lead to pin 4 on the 4N26 and the black lead to ground.


your meter should read 3v , if you disconnect the 3v from the resistor your meter should red 0V


So here's an example

Image

red lead on right is connected to 3v psu and pin 5 on the chip , meter is connected to pin 4 on the chip and ground on psu
resistor is connected to pin 1 on the chip and pin 2 on the chip is connected to ground , no 3v on the resistor so meter reads 0V

Image

now you can see I have connected 3v to the resistor and now the meter reads 3v.

and here is a close up of the chip and connections

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Sun Mar 31, 2019 5:47 pm

pi9 wrote:
Sun Mar 31, 2019 5:28 pm
I am sure that red LED is not just a power ON indicator because it goes off at random even with other Pi 3B+s that I've never connected to the relay.
The red LED should be on all the time your pi is powered if its going out the your power supply voltage is dropping below 4.65V which is not good and can lead to SD card corruption.

I suggest you need to invest in a better power supply or one with thicker cables.

the official psu is the best.
https://www.raspberrypi.org/products/ra ... er-supply/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Sun Mar 31, 2019 7:23 pm

I replaced the tablet power adapter that I was using (rated for 2.1A) with an adapter that I had bought with the pi (rated 2.5A) and that took care of the red LED issue, so good news here :)
I tested the opto isolator with the multimeter, that worked too. When I wired it to the relay, it didn't work. Where should I place my multimeter to check the voltage? Between Vin and GND, i get 3V, between Vin and Vcc I get 0v regardless of relayON or relayOff (software wise)

Test 2
I connected the relay as per the drawing and tried to trigger it manually by connecting the resistor to +3 and pin 2 to GND. Didn't work.
I unplugged the Vcc from the relay and left every other wire as is. When I wired the resistor to +3, the meter shows 3V on pin 4, I remove the wire from the resistor, it goes back to 0. ie it works as it should. As soon as I connect Vcc back to the relay, the output on pin 4 becomes 3 more or less. If I connect the +3 to the resistor, it goes up by a few point something volts, I remove it goes down by a few point something volts. What does this mean?

I can attach a video if needed.

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Mon Apr 01, 2019 7:45 am

Did you try testing the opto isolator as I showed you without connecting it to your relay ?

try posting some pictures like I did showing your opto isolator and how its wired, then I can check it correct .

Yes always better to use the power supply that correctly design and rated for the pi.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Mon Apr 01, 2019 12:41 pm

Yes. I tested it like you said and it worked. Will post pictures later when I get home.

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Wed Apr 03, 2019 12:19 am

Please see the images below
IMG_3536.JPG
IMG_3536.JPG (137.54 KiB) Viewed 1068 times
IMG_3537.JPG
Relay wiring
IMG_3537.JPG (103.61 KiB) Viewed 1068 times
IMG_3532.jpg
Voltage level with the relay connected
IMG_3532.jpg (136.25 KiB) Viewed 1068 times
I removed the purple wire from the Vcc on the relay and put the meter alone and ran my software. The voltage goes from 0 to close to 3v as the software triggers it. It looks to me like the voltage level is not enough or something.

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Wed Apr 03, 2019 12:20 am

One more image
Current is a constant 0.58mA reglardless of state
IMG_3534.JPG
IMG_3534.JPG (134.13 KiB) Viewed 1068 times

pcmanbob
Posts: 6917
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Wiring relay to Pi

Wed Apr 03, 2019 11:19 am

Ok .

So we know the opto isolator is working and you have it connected correctly.

So are you sure the relay requires 3v on the In1 pi to turn it on, have you tested it by just connecting 3v to that pin ?

but the relay will not operated when using the opto isolator to connect 3v to the In1 pin.

So try changing the resistor to 270 ohms that should make the LED brighter and the transistor turn on fully giving you 3V at the output.

The 0.58mA being drawn by the relay card even when off is probably just what's being drawn by the LED that is always lit on the relay board.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
ptimlin
Posts: 88
Joined: Tue Jan 08, 2019 7:44 pm

Re: Wiring relay to Pi

Wed Apr 03, 2019 9:38 pm

The problem is not enough current for the relay coil. Optocouplers have what is known as a current transfer ratio. Meaning, the output current available will be some ratio of the input current driving the LED.

The 4N26 has a transfer ratio of about 50% (output current available is half of what you drive the input)
The forward voltage on the input LED is typically 1.3V.

So with 3.3V applied to the Opto's LED through a 470 ohm resistor, you get a current through the LED of (3.3 - 1.3)/470 = 4.25mA which means at best you can drive somewhere a bit over 2mA with the output of the Opto. Your relay module states 15-20mA to activate the relay so you are only offering about 10% of the required current to the relay. You can not do this with the 4N26.

Even if you drive the relay coil directly with the Pi's GPIO pin, since the relay module spec is a wishy-washy range (rather than a minimum current guaranteed to turn it on) it may or may not work depending if the module needs closer to 15mA or closer to 20mA, you are right up against the GPIO pin current limit. See my suggestion here...
viewtopic.php?f=37&t=237013&p=1449739#p1449739
for one way to "convert" the GPIO pin's output ability to drive the relay where the ULN2003LV does the grunt work of sinking the required current needed.

pi9
Posts: 124
Joined: Wed Jun 10, 2015 3:32 pm

Re: Wiring relay to Pi

Thu Apr 04, 2019 1:45 am

The problem is not enough current for the relay coil.
Thanks. Now I understand the problem, however I don't really understand the explanation in your link. I mean I am not sure how to wire it. Also, since I am abandoning the 3v relay (which I originally bought because I thought I could wire directly to the pi), I'd rather go for a 12V relay at this point. Part of the reason is I don't want to deal with two many power supplies/adapters. Just one adapter for the pi and one powerful one for the relay and whatever components that will be needed, but again, I am not sure what to use or how to wire it. Any hand drawn diagram would be greatly appreciated.

Apart from this, I did try experimenting with different value resistors on the pi side of the opto isolator and it wouldn't work. I had to give it a shot anyway :)

Return to “Automation, sensing and robotics”