0

How do I read a switch OR drive a relay from a single termin
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How do I read a switch OR drive a relay from a single termin

by Tinaja on Fri Apr 26, 2019 10:09 am

I am creating a device which has a terminal strip attached through which I want to be able to connect devices to be either sensed/read or driven. IOW, from the same 2 screw terminals I want to be able to see if a magnetic door switch or PIR has tripped, OR drive a relay that triggers a siren. Or maybe in input mode we're looking at a low water level switch and in output mode (on a different terminal) we're driving a pump controller or solenoid valve. Whether each terminal acts as an input or an output will be selected in software.

I'm using an MCP23017 as both an expander and to isolate the processor. In the example below, I show it connected as a drive circuit, with a relay attached to "port" 1 on the terminal strip. In this mode, I would set GPA0 to HIZ and use GPB0 to control the relay by turning on and off the transistor (technically a relay driver (https://www.digikey.com/product-detail/en/diodes-incorporated/DRDC3105E6-7/DRDC3105E6-7DICT-ND/3028443), but shown as a transistor for simplicity here). I think what I have in this mode will work.

The question is, in INPUT mode, those same 2 terminals would be connected to a simple switch. I would set GPB0 to low, turning off the transistor, and I would read GPA0 on every loop to see whether the switch was closed or not. Initially I only had R2 but I'm concerned that the MCP would see that input as "floating" when the switch was open, leading to unpredictable results, so I added R3 to pull the pin low. Now I'm wondering if R3 will cause problems with the relay in OUTPUT mode since the relay will always have a path to ground. Given what I'm trying to accomplish, can you give me insight into the following questions?

  • Do I need R3 or will GPA0 be OK without being pulled down in INPUT mode? It seems like not having R3 there would be better for the OUTPUT mode.
  • If I do need both, should R3 be on the other side of R2? (in OUTPUT mode, how will the relay drive be affected by R3 if at all)
  • Will this work at all? If you have a more stable, reliable suggestion I would love to hear it! I have physical space constraints which is why we need to double duty the terminal strip.

Image
Tinaja
 
Posts: 42
Joined: Wed Apr 18, 2012 11:25 pm

Re: How do I read a switch OR drive a relay from a single te

by zener on Fri Apr 26, 2019 7:40 pm

Tinaja wrote:Do I need R3 or will GPA0 be OK without being pulled down in INPUT mode? It seems like not having R3 there would be better for the OUTPUT mode.

I think R3 is a good idea, to act as a pull down as you stated, and it will not affect the output function. R3 will only draw less than 0.5mA through the relay coil. The relay coil likely needs 100x or 200x that much current to operate, so it is not a problem.

Tinaja wrote:If I do need both, should R3 be on the other side of R2? (in OUTPUT mode, how will the relay drive be affected by R3 if at all)

You could put it on either side, but if you move it to the other side then it will form a voltage divider, so you would need to use an analog read in that case, to read the voltage, since it would only be 2.5V which digital I/O will not like, since it does not fall within the valid range for high or low for 5V logic.

Tinaja wrote:Will this work at all? If you have a more stable, reliable suggestion I would love to hear it! I have physical space constraints which is why we need to double duty the terminal strip.
Yes it will work

zener
 
Posts: 4401
Joined: Sat Feb 21, 2009 2:38 am

Re: How do I read a switch OR drive a relay from a single te

by Tinaja on Sat Apr 27, 2019 10:23 am

Thanks for responding!

After thinking through your comment on the .5ma draw, will it work properly with this device then? https://www.digital-loggers.com/iotfaqs.html

Would I be better off using a 100k for the pull down?

The whole thing seems messy to me for some reason, but I can't think of any other way to do it :/

Again, thank you!
Tinaja
 
Posts: 42
Joined: Wed Apr 18, 2012 11:25 pm

Re: How do I read a switch OR drive a relay from a single te

by zener on Sun Apr 28, 2019 1:43 pm

Well, we are talking about different things now. You drew a relay but we are talking about a NO switch and now a high impedance input SSR type device. They say that will turn on with .2mA but they don't say the max current the will NOT turn it on. Is it .1mA, .05mA??? We don't know. You can increase the size of the pull down, but it's effectiveness at suppressing noise is reduced at the same time. In my experience 50K is about as high as you want to go. You might get away with 100K but much above that and it won't be helping a lot.

I can think of a couple of things you could do:

1) Instead of hardwiring your first pin to 5V, bring it back to another IO pin. And, change your pull down to a pull up. When you want to control an output, make your new IO pin high. When you want an input make it low.

2) If you don't want to use another IO pin you could do this (at the expense of more complicated code): Change your 5V connector pin to ground. Change your pull down to a pull up. Change your NPN transistor to a PNP. You might say: "What is the difference, all we did is change the polarity of everything and we still have the same problem we started with." The difference is you can get rid of R2 and R3 and use the built in pull up function on the IO pin. You activate it when you want an input and you deactivate it when you want an output. I believe this can be done dynamically (changed "on-the-fly").

There may be other ways to do what you want.

Good luck

zener
 
Posts: 4401
Joined: Sat Feb 21, 2009 2:38 am

Re: How do I read a switch OR drive a relay from a single te

by Tinaja on Mon May 27, 2019 1:37 pm

Thank you very much for your help on this. I've been thinking through your comments and now I'm wondering if this would be better served using a MOSFET. Would the below circuit work (switch and relay shown together just for visualization - in input mode forget about the relay, in output mode forget about the switch...)

MCP is running at 5VDC, and this circuit is meant to drive 5V relays up to 500ma or SCRs, like the one I mentioned previously.

In input mode, GPA0 is set to input with pullup, GPB0 is set to HIGH.

In output mode GPAB is set to LOW when the port is on, HIGH when off. 1n4004 is a flyback diode.

If this circuit will work, can you recommend a part number for Q2? It needs to be as small as possible, handle 5VDC up to 500ma and not generate a lot of heat. I would think one of these, but not sure what the source to drain voltage is about...

Image
Tinaja
 
Posts: 42
Joined: Wed Apr 18, 2012 11:25 pm

Re: How do I read a switch OR drive a relay from a single te

by Tinaja on Tue May 28, 2019 1:38 pm

And if I did this and ran the MCP23017 at 3.3V instead of 5V, would the 12V potential on GPA0 when it was in HIZ mode (GPB0 turned ON) cause any problems? Q3 would be 2N3904 unless there is a better way to do this.

Image
Tinaja
 
Posts: 42
Joined: Wed Apr 18, 2012 11:25 pm

Please be positive and constructive with your questions and comments.