News:

Forum may be experiencing issues.

Main Menu

Relay meets Optocoupler Question

Started by greysun, January 06, 2024, 07:12:00 PM

Previous topic - Next topic

greysun

Hi everyone!

Feel free to tell me where this post should go. I'm pulling my hair out...

I'm building an effects switcher with an Arduino - have had the parts for years, finally doing it. I tested everything using LEDs and it worked great.

for some reason, now that I've got the relays on here, I am unable to get my relays to actually switch - this is a problem, hehe. I've got a TQ2-L-5V relay, pc817 opto and S8050 transistor.

I would assume maybe I didn't code things correctly, which is still plausible, but I have a reset for all relays on power-up to switch on and off - none work, but it did work when it was just LEDs.

Here's my diagram (attached) - can someone tell me what I'm missing? (happy to share code from Arduino, too, if anyone is up to speed there, but again - in testing, it worked - I think it's something with my opto that's just not flipping the voltage between 1 and 10 so it actually toggles the switch).

Thanks in advance, as always! :-)


mjg

The Arduino code isn't going to make any difference here, so I'd concentrate on the other parts. 

You can even take the Arduino out of it, and just apply 5v or ground to the opto to simulate what will happen when the pin goes high or low.

I'm struggling to work out the layout around that relay.  If the transistor is on, then you get 5v at one pin and ground at the other.  If the transistor is off, you get 5v at one pin, and the other pin is floating.  That doesn't seem right? 

I'd probably use a multimeter to check how voltages are changing when you switch the opto, that might help isolate the issue, and you could also try just apply your 5v and ground to the relay pins to check it works. 

I can't remember if that relay is only on when voltage is applied, and off when no voltage, or if it is one where you reverse the voltage to switch the other way.  Might be worth checking what sort of relay you have, to make sure it will work with this schematic. 

Hope some of that is useful! 

mjg

"Latching"...that's the word I was failing to remember above. 😅

Check whether your relay is latching or not.

Also, if you are open to modifications, I have done plenty of Arduino and relay effects, and I don't think you need the opto or transistor at all.  I would normally just use a latching relay, and hook it up directly to 2 of the Arduino pins.  Put one high, the other low, for 100ms or so, to switch one way, and reverse the high / low for switching the other way.  That would require changing quite a bit of your board though. 

greysun

Quote from: mjg on January 06, 2024, 09:17:00 PM
The Arduino code isn't going to make any difference here, so I'd concentrate on the other parts. 

You can even take the Arduino out of it, and just apply 5v or ground to the opto to simulate what will happen when the pin goes high or low.

I'm struggling to work out the layout around that relay.  If the transistor is on, then you get 5v at one pin and ground at the other.  If the transistor is off, you get 5v at one pin, and the other pin is floating.  That doesn't seem right? 

I'd probably use a multimeter to check how voltages are changing when you switch the opto, that might help isolate the issue, and you could also try just apply your 5v and ground to the relay pins to check it works. 

I can't remember if that relay is only on when voltage is applied, and off when no voltage, or if it is one where you reverse the voltage to switch the other way.  Might be worth checking what sort of relay you have, to make sure it will work with this schematic. 

Hope some of that is useful!

Thank you for the reply! Good call on removing the arduino from the equation while I test - will do that next (I need more breadboards!). I'm convinced I just have the mix wrong - some connection just isn't right.

The relay I have is a DPDT latching type single coil - It latches when the voltage is reversed across pins 1 and 10. I tried 9v+9v and ground+ground - no dice. it only works if I fully reverse voltage. Looking at the diagram, there's nothing that would ground pin 10 when the arduino pin sends a signal. I need to think about this...

I looked into just controlling the voltage on the Arduino instead of an opto/transistor option, which would be SO much easier, hehe, but I had read that the relay can cause voltage spikes that could fry the arduino. If you have a different experience, I'm all ears on that front.

When testing with LEDs, I was able to control a photofet for temporary muting while the relay does its thing, and use the arduino to control the timing on the photofet to match that of an analog 3pdt footswitch. It's programmable with 11 banks and 9 loops per bank, so I can control 9 effects and which is on or off per loop. If I can make it work, great - but I gotta get the relays working first! Hehe.

More to follow... I'll work on the relay circuit...

When I started this project in 2017, there wasn't much out there. There may be now, but

mjg

Yep, if it's a latching relay, you need to be able to completely reverse the voltage.  Giving 5v and ground one way will switch the relay to position 1.  Giving ground and 5v opposite will switch it to position 2.  So your current schematic is definitely not doing that. 

I've had no issues with just switching using the Arduino directly.  You will need 2 pins on the Arduino to do this, as you will be reversing which one is  High and Low to flip the relay. 

Here is a build doc for one of my projects that used a relay, schematic on page 2, have a look around the attiny chip to see what it's doing. 

https://graybloomfield.com/guitar/docs/clippy1.0.pdf

And the code that goes with it:  https://www.graybloomfield.com/guitar/docs/clippy-code1.0.zip


mjg

Another thing you could try, if you don't want to have the Arduino connected directly to the relay, is to have your opto chip controlling 2 transistors.  One will set 5v or ground, and the 2nd transistor will set the opposite.  Hook those up to your relay, and should be golden. 

Look for "transistor not gate" in your favourite search engine if you need pointers on how to do that?

greysun

Quote from: mjg on January 06, 2024, 11:47:40 PM
Another thing you could try, if you don't want to have the Arduino connected directly to the relay, is to have your opto chip controlling 2 transistors.  One will set 5v or ground, and the 2nd transistor will set the opposite.  Hook those up to your relay, and should be golden. 

Look for "transistor not gate" in your favourite search engine if you need pointers on how to do that?

Literally just had this exact thought! I'll do a little research and check out your project, as well.

I'm using an arduino mega, so I have some pins if I want to go the direct route. I may have enough parts to just double up the circuit and use 2 pins, as well! (Seems like overkill, so I'll look into the other ways).

Super helpful - thank you so much! Really appreciate the help!

mjg

Another thing for you to research - look at non inverting buffer chips, and use one of those instead of your opto and transistors.  As pedal designers we tend to use transistors and opto chips as it's traditional, I guess... but there are usually better options for just signal control. 

You may find a hex buffer chip in a dip package that is cheaper and smaller than piles of opto and transistors.  That should fulfil your need of separating the Arduino from the relay. 

greysun

Quote from: mjg on January 07, 2024, 01:07:43 AM
Another thing for you to research - look at non inverting buffer chips, and use one of those instead of your opto and transistors.  As pedal designers we tend to use transistors and opto chips as it's traditional, I guess... but there are usually better options for just signal control. 

You may find a hex buffer chip in a dip package that is cheaper and smaller than piles of opto and transistors.  That should fulfil your need of separating the Arduino from the relay.

I'll look into this, as well - I'm not as worried about parts, as I already have the parts. It's a long story - I originally built a pedalsync system, but the mute-during-relay-switch was a full second long and not subtle. I wasn't able to modify it, either, so I then turned to the arduino version I have on the breadboard now. I think in the 7 years since I started this, there are plenty of DIY switchers that have popped up, but I'm invested in this system and have the parts... gotta see it through, for better or worse, hehe.

Again, extremely helpful, so thank you! More to follow...

greysun

Quote from: mjg on January 07, 2024, 01:07:43 AM
Another thing for you to research - look at non inverting buffer chips, and use one of those instead of your opto and transistors.  As pedal designers we tend to use transistors and opto chips as it's traditional, I guess... but there are usually better options for just signal control. 

You may find a hex buffer chip in a dip package that is cheaper and smaller than piles of opto and transistors.  That should fulfil your need of separating the Arduino from the relay.

Do you have any model numbers to look for?

I did some testing today - I'm not sure I'm setting up my S8050 properly. The pc817 opto works as expected. When I run pin 3 to pin 2 of the S8050, my voltage drops from 5.8v to .7v, and then pin 3 has no voltage at all.

I was also trying to figure out what resistor values to use to make it a NOT gate, but nothing worked... can't tell if I fried the transistors or what, but I haven't done anything that would have done that, I don't think.

For giggles, I used the MV57 chips that came from the pedalsync system - they "worked" but not quite right. I may need to go the IC route if there's something out there...

mjg

A 4010 or 4050 are pretty standard buffer chips, maybe start with one of those and see whether it does what you want. 

Have you been able to confirm the relay works by itself, by just hard wiring the voltage to it?  It should only need a quick touch of voltage to switch a latching relay.  No need to hold the voltage on for long.  Then you reverse the wires to switch back the other way, again, only for a brief moment. 

greysun

Quote from: mjg on January 08, 2024, 09:03:36 PM
A 4010 or 4050 are pretty standard buffer chips, maybe start with one of those and see whether it does what you want. 

Have you been able to confirm the relay works by itself, by just hard wiring the voltage to it?  It should only need a quick touch of voltage to switch a latching relay.  No need to hold the voltage on for long.  Then you reverse the wires to switch back the other way, again, only for a brief moment.

Awesome - I'll look those up!

To answer your relay working question: yes, it works - I can get it to snap every time I reverse the voltage. So now I'm just trying to figure out how to do that electronically, and nothing so far has worked - but I might be onto something with the parts I have...

First: my failure... I tried to work with (2) S8050 transistors (and later, some 2n3904 just to see if I got the same result), a la the diagram below, to flip the output signal. What I was noticing is that my 5v signal became .7v once it went through the transistors. I'm also finding that connecting pins 1 and 10 on the coil has some resistance (small, like 150ohm ish), so that's creating voltage weirdness when I try to do a high/low with 2 NOT gate setups. I then test the transistors to be sure they're working, and they are according to my multimeter. No snap to the relay, just sadness.



Second: my (possible) win? I then went the mosfit route - I have some BS170s, so I used this circuit to test LEDs:



I setup 2 instances of this. I used a temporary push button switch for testing on each one and was able to get the LEDs to work when I touched the button, although they stay on sometimes after I release it, so I feel like the mosfets are holding a charge or something - or it's related to me having a 5v input and not a 12v like the diagram. I have no idea how to test it, so that's where I am currently. The output voltage after the LEDs are on is like 2.4v ish, which should be enough to trigger the relay, but I need to do a little more testing there.

My assumption is that I would throw a 5v signal on pins 1 and 10 of the relay (aka, the coil), and then connect each separately to the drain on the mosfets, but I feel like I'm missing an element here.

OH - the reason I don't want to just power these from the Arduino is that I will have 11 relays at a minimum, and possibly as many as 20 - each pulls 20mA when operating, and if all of them go at once (which happens when I turn on the device), it's too much pull for the Arduino (I think it said 200mA is the most it should have to worry about).

I've read that these are quality relays, and at $3/pop, aren't expensive but also aren't inexpensive. I just need something that can reliably invert voltage across pins 1 and 10, and that's proving... a situation. hehe. ;-)

Any thoughts or advice, I am open to it!

greysun

Quote from: mjg on January 08, 2024, 09:03:36 PM
A 4010 or 4050 are pretty standard buffer chips, maybe start with one of those and see whether it does what you want. 

Have you been able to confirm the relay works by itself, by just hard wiring the voltage to it?  It should only need a quick touch of voltage to switch a latching relay.  No need to hold the voltage on for long.  Then you reverse the wires to switch back the other way, again, only for a brief moment.

Awesome - I'll look those up!

To answer your relay working question: yes, it works - I can get it to snap every time I reverse the voltage. So now I'm just trying to figure out how to do that electronically, and nothing so far has worked - but I might be onto something with the parts I have...

First: my failure... I tried to work with (2) S8050 transistors (and later, some 2n3904 just to see if I got the same result), a la the diagram below, to flip the output signal. What I was noticing is that my 5v signal became .7v once it went through the transistors. I'm also finding that connecting pins 1 and 10 on the coil has some resistance (small, like 150ohm ish), so that's creating voltage weirdness when I try to do a high/low with 2 NOT gate setups. I then test the transistors to be sure they're working, and they are according to my multimeter. No snap to the relay, just sadness.



Second: my (possible) win? I then went the mosfit route - I have some BS170s, so I used this circuit to test LEDs:



I setup 2 instances of this. I used a temporary push button switch for testing on each one and was able to get the LEDs to work when I touched the button, although they stay on sometimes after I release it, so I feel like the mosfets are holding a charge or something - or it's related to me having a 5v input and not a 12v like the diagram. I have no idea how to test it, so that's where I am currently. The output voltage after the LEDs are on is like 2.4v ish, which should be enough to trigger the relay, but I need to do a little more testing there.

My assumption is that I would throw a 5v signal on pins 1 and 10 of the relay (aka, the coil), and then connect each separately to the drain on the mosfets, but I feel like I'm missing an element here.

OH - the reason I don't want to just power these from the Arduino is that I will have 11 relays at a minimum, and possibly as many as 20 - each pulls 20mA when operating, and if all of them go at once (which happens when I turn on the device), it's too much pull for the Arduino (I think it said 200mA is the most it should have to worry about).

I've read that these are quality relays, and at $3/pop, aren't expensive but also aren't inexpensive. I just need something that can reliably invert voltage across pins 1 and 10, and that's proving... a situation. hehe. ;-)

Any thoughts or advice, I am open to it!

Bio77

Would the current arrangement work with a non-latching relay? 

greysun

Quote from: Bio77 on January 08, 2024, 10:17:36 PM
Would the current arrangement work with a non-latching relay?

It might? I think the latching relay allows the Arduino to rest a bit more - a non-latching style would need to have constant power to stay open during a loop/effect need, right?