madbeanpedals::forum

Projects => General Questions => Topic started by: greysun on January 06, 2024, 07:12:00 PM

Title: Relay meets Optocoupler Question
Post by: greysun on January 06, 2024, 07:12:00 PM
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! :-)

Title: Re: Relay meets Optocoupler Question
Post by: 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! 
Title: Re: Relay meets Optocoupler Question
Post by: mjg on January 06, 2024, 09:26:00 PM
"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. 
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 06, 2024, 11:06:18 PM
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
Title: Re: Relay meets Optocoupler Question
Post by: mjg on January 06, 2024, 11:43:43 PM
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

Title: Re: Relay meets Optocoupler Question
Post by: 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?
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 07, 2024, 12:01:24 AM
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!
Title: Re: Relay meets Optocoupler Question
Post by: 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. 
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 07, 2024, 01:21:42 AM
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...
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 07, 2024, 11:33:49 PM
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...
Title: Re: Relay meets Optocoupler Question
Post by: 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. 
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 08, 2024, 10:08:44 PM
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.

(https://www.electronics-tutorials.ws/wp-content/uploads/2018/05/logic-log47.gif)

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

(https://europe1.discourse-cdn.com/arduino/original/4X/b/e/f/bef138d24dc4ed6ddfb0ff187009216e532a5244.jpeg)

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!
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 08, 2024, 10:10:25 PM
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.

(https://www.electronics-tutorials.ws/wp-content/uploads/2018/05/logic-log47.gif)

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

(https://europe1.discourse-cdn.com/arduino/original/4X/b/e/f/bef138d24dc4ed6ddfb0ff187009216e532a5244.jpeg)

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!
Title: Re: Relay meets Optocoupler Question
Post by: Bio77 on January 08, 2024, 10:17:36 PM
Would the current arrangement work with a non-latching relay? 
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 08, 2024, 10:27:44 PM
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?
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 09, 2024, 03:33:13 AM
I decided to start my breadboard from scratch just focused on getting a single relay to work. I think it helped, but I'm not out of the woods.

I wired this diagram: https://handsontec.com/dataspecs/relay/1Ch-relay.pdf (https://handsontec.com/dataspecs/relay/1Ch-relay.pdf) (see page 2) almost exactly... The only thing I changed: transistor is wired for logic NOT gate, meaning a 10k resistor between +vcc (5v, not from the Arduino) and pin 3 (collector) of the transistor S8050, and then I send it just to one side of the relay (pin 1 OR 10). This allows the voltage to invert, which is what I want (thank you, mjg!!!).

Off the S8050 transistor, I had 5.8v on the collector (pin 3), .7v on the base (pin 2), 0v on emitter (pin 1).

When I cut off "IN," I had 0v on the collector, .7v on the base, 0v on the emitter. Looking good, feels like it's what we want, BUT...

when I connected the each transistors pin 3 (collector) to pins 1 and 10, respectively, on the relay switch (TQ2-L-5v), the voltage dropped from 5.8v to .15v, which is not enough to engage the relay.

It's almost like the resistance between the coil pins (250ohm) is connecting the 2 circuits and killing the voltage. if it weren't for that, it seems like this would be working... it's almost like I need to have 2 more opto isolators, but that feels like overkill.

Any thoughts? I feel like I'm REALLY close... Thanks again for the help! It's why I keep coming back here! :-)
Title: Re: Relay meets Optocoupler Question
Post by: mjg on January 09, 2024, 06:45:53 AM
I'll see if I have time to breadboard something tomorrow for you.  Hopefully I've got a spare relay sitting around somewhere. 

I think what you need is two pins from the Arduino, connected through a transistor or mosfet each as a buffer, and then switch one pin high, other low.  Then reverse the high and low to switch back the other way. 

I think that diagram you've been looking at is a non latching relay?  That might be why it's not quite working. 
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 09, 2024, 01:39:35 PM
Quote from: mjg on January 09, 2024, 06:45:53 AM
I'll see if I have time to breadboard something tomorrow for you.  Hopefully I've got a spare relay sitting around somewhere. 

I think what you need is two pins from the Arduino, connected through a transistor or mosfet each as a buffer, and then switch one pin high, other low.  Then reverse the high and low to switch back the other way. 

I think that diagram you've been looking at is a non latching relay?  That might be why it's not quite working.

I do appreciate it, so thank you in advance if you take that step!

In the meantime, I'm seeing folks on the internets using 555 timers to create/operate latching relays - and have a ton of those on-hand.

There's this video: https://www.youtube.com/watch?v=dzcgETRXbOM (https://www.youtube.com/watch?v=dzcgETRXbOM) and also this: https://www.youtube.com/watch?v=f8O44E9RlsY (https://www.youtube.com/watch?v=f8O44E9RlsY)

I know those aren't 1:1 what I'm doing, per se, but it seems like a similar logic may be able to be employed to trigger the relay. I'll do a little sleuthing - 555 timers have always confused me and I have little experience with them, lol, yet somehow I have 16 on-hand!
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 09, 2024, 07:05:38 PM
Furthering the 555 timer, it seems like I could do something like this: https://www.eeweb.com/voltage-controlled-switch-using-555-timer/ - that appears to be a bistable mode, but there are so many variations of this I can't quite figure it out.

I'd need 2 555s, but I've got plenty and they're cheap - if I have both always set at 0v by the , I can pulse one 555 to activate it (pin 1 5v, then 0v), and the other 555 to deactivate (pin 2 5v, then 0v) it... at least that's what I'm thinking.

I mocked it up quickly in illustrator over lunch. Any thoughts on this one?
Title: Re: Relay meets Optocoupler Question
Post by: mjg on January 09, 2024, 08:53:34 PM
Using a 555 would probably work, but all it's doing there is being a voltage controlled switch.  Which is the same as a transistor or mosfet really.  Not sure it will get you anything different to what you've already tried?
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 09, 2024, 11:50:48 PM
Quote from: mjg on January 09, 2024, 08:53:34 PM
Using a 555 would probably work, but all it's doing there is being a voltage controlled switch.  Which is the same as a transistor or mosfet really.  Not sure it will get you anything different to what you've already tried?

You are probably right, and that 555 circuit doesn't work anyway. But I just can't get the other inverter circuits to work with this relay - like I said, I'm able to invert the voltage with a logic NOT gate, but once I connect each to the coil pins, the voltage drops and nothing happens when I trigger the coil, as if it's just connecting the 2 NOT gate circuits to one another through the coil instead of .

I just did a quick test using NOT gate with the s8050 - one of the NOT gates on one side of the relay coil at 0v, and direct 5v (straight from power source) on the other side of the relay coil. This setup snaps the relay when I swap the pins, so the relay works, and I know that at least the NOT gate at 0v will work. 

I then did the same test with one of the NOT gates at 5v, the other at 0v. Doesn't work when I swap the pins.

Am I missing a diode(s) between the NOT gates and the relay pins that would keep the voltage from going back to the collectors? I can't figure it out...
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 10, 2024, 12:16:59 AM
Here's the diagram as it stands now.

I have LEDs in series with the optos for testing only - but I should be able to turn one off, leave the other on, then reverse that, and it should "click" - but it doesn't.

I did a pin readout - on the 5v pin, I read 5.8v with direct power, but only .14v with the NOT gate.

Measuring direct from the collector, it's 5.8v when opto is triggered, 0v when otherwise.

I just don't get it...
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 10, 2024, 02:02:23 AM
Breakthrough! I replaced the 10k resistor from +5v with 100r, and it's working!

When the relay is triggered, it measures 4.25v to one side of the relay, and between .1 and .3v on the other. (I think it needs at least 75% of 5v to work, or 3.75v, and 200r got me 3.3v, so 100r gives me a good buffer to work from depending on the power supply)

When both optos are on, voltage is .1v each side of the relay.

When I measure the transistor pins - collector matches the relay pins - base is .7v when optos are on, and when they're off it reads -1.1v and grows slowly up toward -1.6v (I imagine it would keep moving up if I left it in that state - but I think the optos are always on except when triggered in the code).

So I guess my last question on this thread then would be: Is there a danger to the transistor using only 100r with the NOT gate circuit, given the above numbers?

If you have any info, I'd love it!
Title: Re: Relay meets Optocoupler Question
Post by: mjg on January 10, 2024, 06:26:44 AM
Glad you got it working.  I breadboarded something very similar and that worked for me.  Only thing I left out is the opto chips.  If you have a common power supply for the Arduino and transistors, you don't need the opto chips.  But if you need that power isolated from each other, put them in. 

I think with your 100r resistors at 5v you will have 50mA going through the transistor, so check the data sheet to see what they can handle.  Or try 1k and see if that still works.

I've got with transistors as a buffer, rather than an inverting buffer.  Makes no real difference, as you just invert the logic on the Arduino pins to do what you want. 
Title: Re: Relay meets Optocoupler Question
Post by: mjg on January 10, 2024, 06:32:28 AM
Here's what worked for me...
Title: Re: Relay meets Optocoupler Question
Post by: greysun on January 10, 2024, 02:27:20 PM
Quote from: mjg on January 10, 2024, 06:32:28 AM
Here's what worked for me...

I tried this exactly as you have it, and it was the same as my own - the readings were only like .15v different when triggered and I had to reduce the resistor to get the voltage right across the pins. These TQ2 relays are oddballs.

I also checked the transistor datasheet, which seems to have a 1500mA on the collector pin, so 50mA shouldn't be an issue for it... though 50mA feels high, but I think the relay pulls 40mA (which is why I didn't want to run it directly from the Arduino when I'll have so many) - if you have a minute to explain the math there, I'm all ears (not saying I came up with anything different - I've just never had to measure it before, lol. A quick google search has a lot of multimeters and whole home circuit panels, which may not apply one-to-one against this need).

Thank you thank you thank you again - I would not have gotten to something that works without your help, so I really appreciate it!