HELP Please... The right way to use diodes stop backfeeding on uC DIO lines.

Thread Starter

ben.scammell

Joined Mar 9, 2015
8
Imagine a standard DIO on a microcontroller.

If this pin is configured as an output, I'd like it to switch a relay using a darlington driver (such as a ULN2803).

If this pin is configured as an input, I'd like an opto-isolated input.

I've attached a schematic, and would welcome feedback on how better to do it.

This is for a mixed IO board for an Arduino running of I2C via a PCA9534.
 

Attachments

Dodgydave

Joined Jun 22, 2012
11,307
if your using a Uln chip, you dont need the opto, just put the input to uln chip via a 1K resistor and diode,to your arduino output.
 

Thread Starter

ben.scammell

Joined Mar 9, 2015
8
if your using a Uln chip, you dont need the opto, just put the input to uln chip via a 1K resistor to your arduino output.
I think i've failed to explain.. the OPTO side is for when the DIO is configured as an input, and the ULN side for when it's an output. I'm trying make a board that can have 8 outputs (as relay) or 8 opto-isolated inputs depending on how you configure the uC pins.

I am trying to avoid the DIO back-feeding the opto-isolator when it's configured as an output and high, with the two diodes in the middle.

Thanks in advance for your help.
 

BillB3857

Joined Feb 28, 2009
2,571
First, I will admit that I'm no Arduino expert. One potential problem I think I see is that when the opto is not active, diode D3, along with resistor R2, will pull the I/O pin higher than 3.3v, which, from what I've read, is a no-no. I would turn D3 around and change the code as needed. If, on the other hand, you want the relay to be sensitive to the opto condition without first being evaluated by the Arduino, an additional diode would be required. Anode toward the opto and cathode tied to the input of the ULN. At least, I think so.??

After thinking about it a while, the way you had it would be better except for having the R2 pull-up being tied to 5v. Tie it to 3.3v to avoid the overvoltage on the I/O pin. Turning D3 around, like I originally suggested, would cause a different problem.
 
Last edited:

MrChips

Joined Oct 2, 2009
30,824
You don't need 5V supply for the opto-coupler load resistor. Use 3.3V.

Use jumpers to select input or output. Remove the diodes.
 

MrChips

Joined Oct 2, 2009
30,824
If you don't mind the relays clicking on opto-coupler activity, simply connect without the diodes.

When selected for relay output there ought to be no activity on the opto-coupler.
 

Thread Starter

ben.scammell

Joined Mar 9, 2015
8
If you don't mind the relays clicking on opto-coupler activity, simply connect without the diodes.

When selected for relay output there ought to be no activity on the opto-coupler.
Thanks,

Is there any issue of the DIO being high (to drive the relay) back-feeding the opto-coupler?
 

Thread Starter

ben.scammell

Joined Mar 9, 2015
8
If you don't mind the relays clicking on opto-coupler activity, simply connect without the diodes.

When selected for relay output there ought to be no activity on the opto-coupler.
If it was an issue... Is there any way of stopping the opto-coupler side driving the relay?
 

MikeML

Joined Oct 2, 2009
5,444
The only way of retaining the previous output state while using the DIO for input is to put a latch (flip-flop) between the DIO and the relay driver. That way, you can configure the DIO to out, write to the latch (which defines the relay state), turn the DIO around to input and then read the input state. You only have to configure the DIO to output the next time you need to change the relay state.
 

MrChips

Joined Oct 2, 2009
30,824
You can disable selected relays but then you would need extra ports and circuitry to do that, which defeats the whole purpose.

Jumpers are still the easiest solution.
 

Thread Starter

ben.scammell

Joined Mar 9, 2015
8
The only way of retaining the previous output state while using the DIO for input is to put a latch (flip-flop) between the DIO and the relay driver. That way, you can configure the DIO to out, write to the latch (which defines the relay state), turn the DIO around to input and then read the input state. You only have to configure the DIO to output the next time you need to change the relay state.
I'm not worried about latching the outputs.

I just want it so if the pin is an input, and you connect an input to the opto-coupler it reads a high or low, and if the pin is configured as an output then the relay switches depending on the pin state... and I'd rather that when the pin is being used as an input, it doesn't drive the output side (i.e. the relay clicking as the input changes state).

Can this be done?
 
Last edited:

Thread Starter

ben.scammell

Joined Mar 9, 2015
8
You can disable selected relays but then you would need extra ports and circuitry to do that, which defeats the whole purpose.

Jumpers are still the easiest solution.
But the design isn't fit for purpose with jumpers, so while easy, it's not the right answer for me. Thanks for your help though.
 

djsfantasi

Joined Apr 11, 2010
9,163
Umfortunately, I don't have Eagle and cannot see your schematic. Can you export it to a .PNG or other graphic format? By the way, .JPGs aren't preferred, as they lose detail.

Are you short of pins on the Arduino and that is why you're using one pin as both input and output?
 

jpanhalt

Joined Jan 18, 2008
11,087
I am pretty sure I don't understand your question, because I don't see the problem when a PIC with output latches is used:

upload_2015-4-23_14-57-1.png

As an output (TRISx,n =0) the PIC writes to the latch LATx. Changing the pin to input (TRISx,n=1) doesn't change what the latch was set at, regardless of whether the input is high or low. At least that was the way it seemed to work in simulation, which means you could read the latch before changing TRIS to see what the previous output was.

John
 

djsfantasi

Joined Apr 11, 2010
9,163
I am pretty sure I don't understand your question, because I don't see the problem when a PIC with output latches is used:<omitted>
As an output (TRISx,n =0) the PIC writes to the latch LATx. Changing the pin to input (TRISx,n=1) doesn't change what the latch was set at, regardless of whether the input is high or low. At least that was the way it seemed to work in simulation, which means you could read the latch before changing TRIS to see what the previous output was.
But he's not using a PIC; he has an Arduino. And I'm not a PIC expert, so I can't translate what you said into "Arduino"ese.
 

Papabravo

Joined Feb 24, 2006
21,228
I think the reference was to an EXTERNAL latch which would hold the output value, while the pin was changed to an input and sampled. FWIW I don't think there is a way to use diodes to do what you want for the simple reason that they are unidirectional and a CMOS input requires both sinking and sourcing in a driver.
 
Top