I am designing a device that uses an MCP23017 running at 3.3V to either check a "port" on a terminal strip as an input from a motion sensor or drive a relay/SCR/indicator from the same "port". I'm space limited so the terminal strip needs to be dual-purpose and each "port" function will be selected in software whenever the device boots up. It will, of course, not be an input AND output at the same time...

In "input" mode, GPA0 would be in input mode with pull-up enabled, and GPB0 would be LOW. In "output" mode, GPA0 would be HIZ and GPB0 would control the output state. Q3 would be 2N3904 (unless you have a better suggestion) and I need advice on a part number for Q2.

The relay and switch on the other side of the terminal strip are just there for visualization purposes to save space. In input mode pretend the relay isn't there, in output mode pretend the switch isn't there. They would not be connected to the same terminals in the real world.

Given the diagram below, can you offer some insight into the following questions?

  • Is this even feasible?
  • In output mode, would the 12V potential on GPA0 cause any problems over time? (It's in HIZ)
  • In input mode will there be much leakage from the mosfet and will that affect sensing the input state in any way?
  • Is 1N4004 the right choice for flyback diode?
  • Can you recommend a P channel mosfet that is as small as possible, can handle up to 500ma and won't generate much heat?
  • Are there any other issues you can spot or advice/recommendations you can give me?

I will likely add a resettable fuse on each mosfet drain as well.

enter image description here

  • \$\begingroup\$ What is connected to the TL32PO pin 2? \$\endgroup\$ – Voltage Spike May 28 '19 at 18:44
  • \$\begingroup\$ fake sensor .... \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 May 28 '19 at 18:51
  • \$\begingroup\$ That switch is just representative. One pin connected to GND, the other connected to GPA0 when the entire circuit is in "input" mode. \$\endgroup\$ – Daemach May 28 '19 at 18:53
  • \$\begingroup\$ Nothing fake about it. I've already built the same basic circuit using a low side switch with output mode as 5VDC and it works. I think 12 will give me more versatility and I want a high side switch so I can use the internal pull-up on the MCP23017 (if possible). \$\endgroup\$ – Daemach May 28 '19 at 18:56
  • \$\begingroup\$ So no Open collector sensors? \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 May 28 '19 at 19:13

Safely drive a relay OR sense a switch from a single terminal?

I would suggest your circuit is not "safe".

While there exists a path from the input pins to VDD through an intrinsic diode in the MCP23017, you should NEVER use this as a method to clamp the input. If you are doing just one input/output, then the current may be limited by the 10k Ohm resistor, but it is not a good design choice.

I'd suggest a simple change can make you schematic "safe" under all circumstances.


simulate this circuit – Schematic created using CircuitLab

In the schematic above I've simply used Q1 to clamp the input low when you turn on the 12V supply to the relay.
The AO3401 I've shown as M1 would be a suitable choice for currents up to > 2A.

Be aware that when you are using GPA_0 as an input, that the input signal should not got above 3.3V ….and whatever that signal is, it is still impressed across the relay, so it is a low impedance input. You might want to fix that problem too.

If you are going to have multiple input/output pairs set up like this you might consider further changes. You could set up an active 3.3V clamp for the inputs.
Solving the problem of having the relay coil permanently across the signal line is somewhat more complex.

Update: You could consider an approach such as this:


simulate this circuit

In the circuit above a TL431 supplies an accurate clamp voltage. Then the BAT54 (you could use BAT54C) diodes provide a clamp threshold at right about 3.3V. Even if the input signal rises to 50V this clamp would sink the current rather than the intrinsic diodes in the MCP23017.

  • \$\begingroup\$ Thanks Jack. It would be either a switch or a relay, input or output. Never together. I was just trying to save space by not drawing 2 schematics where the only difference was a switch or relay connected to the same pins. And I will have the same circuit duplicated 8 times - I'm not sure what you mean by an active 3.3v clamp... \$\endgroup\$ – Daemach May 29 '19 at 0:51
  • \$\begingroup\$ Also, in input mode the GPA0 would be pulled high internally. The switch on the terminal strip would pull it low. \$\endgroup\$ – Daemach May 29 '19 at 0:54
  • \$\begingroup\$ Wouldn't D1 also clamp the output to the relay? Seems like a direct path to ground for the mosfet \$\endgroup\$ – Daemach May 29 '19 at 1:41
  • \$\begingroup\$ @Daemach No, D1 only clamps the GPIO pin to a low voltage. M1, when on would supply 12V to both the relay and the right hand side of R2 so there would be current flow through R2 but the GPA_0 input pin would be at about 1V. \$\endgroup\$ – Jack Creasey May 29 '19 at 2:20
  • \$\begingroup\$ That makes sense. What's the improvement you would suggest for doing this x8? And thank you for the input! \$\endgroup\$ – Daemach May 29 '19 at 2:56

It seems OK so far. Initialization, intermittent cable connection or faults, not-considered. ALthough you have not defined your max output current. The port is rated at 25mA max and with your driver ... I assume is more than 25mA ? or 50mA? 100mA? 1A? for a 12V relay , SSR or ?

They have NCh arrays low RdsOn , not sure about Pch arrays

20V Pch 100 mΩ @ -2.5V cheaper if you buy 1k MOQ https://www.digikey.ca/product-detail/en/diodes-incorporated/DMG2305UX-13/DMG2305UX-13DIDKR-ND/4251545

I think you mean it to be XOR port (input XOR Output). OR could cause contention. ... like a R/W bus for status/control but not on the same time or same port. Then it is simpler to ground switch for control ( low side).

The port must never see > +/-0.2V outside rails to avoid shoot-thru failure. So the 10K series current limits to the ESD diodes on all pins , OK.

Therefore a 1N4001 relies on the 10k series to reduce the port voltage with internal Schottky diode protection. So it is OK. BAT54 is an alternative.

Simultaneous I/O

An alternative is to incorporate an external board wired in parallel for up to 2x 8 ports. Where the sensors (if connected) would also drive the relays using 5mA ( but not used) This eliminates EMI from conducted coil spikes and makes radiated noise outside the box and also gives visual feedback with LED.

SMD OPTOCOUPLER isolation , driving ability , stable performance ; trigger current 5mA; The module voltage is 5V, 9,12 V, 24V are available ; **strong text**

Now you have an "Inclusive" OR Relay with Opto Isolation and external wiring terminal block (also cheap). this means the sensor can drive the 5mA opto switch and Port Rx input at the same time or the relay can be driven by the Port Tx. It also allows you choose independent hi or lo side control and allows you to expand to 16 ports.

  • \$\begingroup\$ Agreed. I do want, and am trying to achieve, XOR. But I have no idea what your circuit represents. The MCP23017 is controlled by ESP32. On startup I set GPA0-7 to either HIZ (for output mode) or INPUT w/ pullup for input mode. It would never try to be an input and output at the same time. \$\endgroup\$ – Daemach May 28 '19 at 19:01
  • \$\begingroup\$ The bi-directional symbol is the 50 Ohm uC port Tx and with internal pullup on input. The BAT54's are additional BEMF protection. The non-inverting driver is OFF at initalization Hi or Low -side ( your choice.) \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 May 28 '19 at 19:05
  • \$\begingroup\$ I don't have enough free uC ports. I need to use the MCP23017 IO expander (or similar). This isn't a communication bus, it's simple HIGH/LOW ports. \$\endgroup\$ – Daemach May 28 '19 at 19:12
  • \$\begingroup\$ So you are using 16 ports to control 8 I/O's \$\endgroup\$ – Tony Stewart Sunnyskyguy EE75 May 28 '19 at 19:15
  • \$\begingroup\$ Correct. Because I can't think of a way to do it with 8 safely or without taking up more room. 500ma per port max, but more likely 120ma @12VDC \$\endgroup\$ – Daemach May 28 '19 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.