Dual GPIO/Manual switch help

Thread Starter

tavalin

Joined May 14, 2015
7
Hello

I'm after a bit of a critique, and if possible an easier/alternative solution to what I've come up with. I'm a software developer by trade and although not an absolute electronics beginner I'm still learning so apologize if I don't understand any of the comments/suggestions.

I've building a device where an internet enabled mcu controls a relay. In the event that the mcu cannot connect to the internet for whatever reason, I'd like to add a manual switch that can work in conjunction with the mcu. I envisage the mcu and manual switch working like those lights and switches you find in a corridor where either switch will toggle the current status.

Here's the schematic. It currently uses a P-channel MOSFET and an N-channel MOSFET in conjunction with a SPDT manual switch.

Capture.PNG

I've verified that it -should- work using this simulator and if you look you can see my intended behaviour.

As I said critique and improvements welcome - and an alternative circuit that doesn't use p-channels MOSFETs would also be appreciated as I seem to be having a hard time tracking down suitable ones. My GPIO pins are 3.3V and most of the p-channels I seem to find have a threshold between -2 and -4V.

Thanks in advance.
 

ErnieM

Joined Apr 24, 2011
8,377
I'm not completely sure I know what you are doing but I do think that way isn't going to work.

Look, you have this device with inputs and outputs that also connects to the interwebs. Use one GPIO pin as an input from your switch, use another GPIO as an output to the relay driver.

The device can moderate between connectivity and user pushing some button or tossing some switch.

You'll need a relay driver something like this (I just hot linked this so don't take the transistor as gospel):



and something like this will detect your switch:

 

Thread Starter

tavalin

Joined May 14, 2015
7
Hi ErnieM, thank you for your reply.

Hopefully I can clarify more what I'm looking to do:

As mentioned I want the relay to be toggled by EITHER the GPIO (marked OUTPUT) or the manual switch (SPDT switch in schematic). Essentially the relay still needs to be controllable if the connection to the internet goes down.

The relay I'm planning to use is a SPST DC-AC solid state relay.

I would also like the device to know if the switch has been toggled manually so the GPIO marked INPUT is to be used as feedback, i.e. when the circuit is complete and the SSR is active it will send a HIGH signal to the GPIO which I can use to update the status, similarly if the circuit is broken and the SSR is not active I want the GPIO input to read low which I can use to update status.
 

pwdixon

Joined Oct 11, 2012
488
You don't need to tie the two functions together, use a switch input to sense the physical switch and use the processor to turn on the relay via a suitable FET either when commanded over the internet or by the switch. This assumes the processor is running at all times of course.

If you meant that the physical switch takes the place of the relay when there is no power then that's different. In that case you would need to parallel connect the switch and relay and sensing the switch would be a simple matter of 'say' using dpdt switch and using the second pair of contacts for the switch sense input for when the processor is running to indicate switch position. The switch positions would then represent relay ON or relay AUTO (ie. internet controlled).
 

Thread Starter

tavalin

Joined May 14, 2015
7
Thanks for your reply pwdixon.

Lets take the example I mentioned earlier with two separate switches (A and B) in a corridor and a light bulb.

Light is initially off.
Someone hits switch A and light turns on.
Someone hits switch B and the light turns off.
Someone hits switch A and light turns on.
Someone hits switch A again and the light turns off.
Someone presses switch B and the light turns on.
Switch A breaks and they continue to use switch B to operator the light until switch A is fixed again.

Both of these switches will toggle the status, irrespective of the status of the other.

The processor will be running at all times but it receives commands from the internet so if the connection is severed then it will no longer be able to receive commands.

If switch A (internet enabled mcu) were to break (internet down), I want the functionality of switch B (manual) to turn on and off the light (relay) to be unaffected.

The extra requirement is that I can feedback the relay status to the web, so for example if the mcu receives a command to turn relay on, it turns on and then if someone manually turns it off the mcu receives a change in signal on the GPIO INPUT that it can use to send a message back to the internet to say the relay is now off.
 

pwdixon

Joined Oct 11, 2012
488
The corridor situation uses two changeover switches. Your solid state relay will not be a changeover device, could you simplify it by using a real changeover relay instead?
 

Thread Starter

tavalin

Joined May 14, 2015
7
I assumed the corridor situation would work like the picture below:



Two switches which when toggle changed the state of the light bulb. My relay would take the place of the light bulb in this circuit.

If my circuit would not work as I expect could you explain where my understanding is incorrect please?

Thanks
 

Thread Starter

tavalin

Joined May 14, 2015
7
The solid state is either on or off, correct? i.e. when DC circuit is complete the relay is active and when DC circuit is broken the relay is off?
 

djsfantasi

Joined Apr 11, 2010
9,163
Also, your requirement that the switch still works if the other one (relay) is broken depends on how it is "broken". If it no longer operates, but the relay contacts are still good (I.e., they conduct but are stuck in one position) then the corridor example still applies. If the contacts do not conduct (the connections burnt and failed open, particularly the common contact) then the circuit will not work.
 

Thread Starter

tavalin

Joined May 14, 2015
7
Thanks for your reply djsfantasi.

In my scenario I am considering the SSR as a "load" rather than a switch. Maybe it would've been better if I didn't mention the SSR and said it was an LED instead?

The bit I really want help/suggestions for is the DC side, the MOSFET arrangement.

Thanks
 

ebeowulf17

Joined Aug 12, 2014
3,307
As long as your processor is powered the whole time you want this to work, I think the early suggestions above are your best bet.

You're assuming the mcu always does whatever the Internet connection says and that external wiring must handle the rest. That's not necessarily the case. Letting the mcu handle everything will be much simpler, unless the code in the mcu can't be altered for some reason. Did you write the mcu code? If not, do have access to it? Can you make changes?

The way I see it you need 4 Boolean variables:
A) is valid Internet connection present?
B) on/off signal dictated by internet connection (only updates when A is true)
C) on/off signal per physical switch
D) output signal (sum of B and C)

Whenever you've got a working Internet connection, variable 1 is true(1). As long as this is true, the mcu should monitor the Internet connection and update variable 2 accordingly. When the internet connection goes down (and variable 1 is false, or 0) variable 2 should be left alone in whichever state it already was. Variable 3 is determined by the physical switch position. Finally, variable 4 is simply the sum of variables 2 & 3, and is used to determine the output from the mcu, and therefore the state of your output relay.

My explanation is wordy and awkward, but this would be really simple code.

Coded as such, you'd end up with the following states:
B:0 + C:0 = D:0 (output off)
B:0 + C:1 = D:1 (output on)
B:1 + C:0 = D:1 (output on)
B:1 + C:1 = D:0 (1+1=2, but since Booleans are only 0 or 1, then "2" wraps back around and becomes 0, so the output is off)

If at any time the internet connection is lost, nothing changes. The mcu still remembers the state of variable B (the last "switch position" the internet connection requested) and continues acting on that, and the local physical switch can still be toggled in the same way as before. When the internet connection comes back online, nothing changes unless the state requested by the internet has changed.

There are probably more elegant ways to handle this in code - I'm not particularly good at this stuff. However, I'm confident that this would work and be quite simple. It could certainly be done with IF statements instead of Boolean math, or perhaps with interrupts that simply toggle the output whenever there is a change to either input.

Although the debates over when to use microcontrollers vs when to use simple logic circuits will rage on forever, it seems to me like when the microcontroller is already in use, there's no reason not to have it do the work. This makes for a much simpler circuit.

Just my two cents. If you still prefer to handle this from the circuit end alone, there should be plenty of ways to make that work too.
 

ErnieM

Joined Apr 24, 2011
8,377
Hi ErnieM, thank you for your reply.

Hopefully I can clarify more what I'm looking to do:

As mentioned I want the relay to be toggled by EITHER the GPIO (marked OUTPUT) or the manual switch (SPDT switch in schematic). Essentially the relay still needs to be controllable if the connection to the internet goes down.

The relay I'm planning to use is a SPST DC-AC solid state relay.

I would also like the device to know if the switch has been toggled manually so the GPIO marked INPUT is to be used as feedback, i.e. when the circuit is complete and the SSR is active it will send a HIGH signal to the GPIO which I can use to update the status, similarly if the circuit is broken and the SSR is not active I want the GPIO input to read low which I can use to update status.
Interweb sends signal to device so it can turn load relay ON or OFF, fine.

Device gets signal from user switch. Thus device has switch status to do whatever it wishes with that information.

When device gets the user switch input it is smart enough to know if load relay is ON to turn it OFF, or the reverse, as appropiate.
 

Thread Starter

tavalin

Joined May 14, 2015
7
Thanks ebeowulf17 and ScottWang for you reply also.

Scott - I'm finding it difficult to understand your schematic with no explanation as to what it does and why.

However I think all of your comments have finally caused the penny to drop! Given that my mcu can control the GPIO output, I can wire the switch to an input pin on the mcu and when a change in state on the input pin occurs toggle the output pin.
 

djsfantasi

Joined Apr 11, 2010
9,163
<omitted> However I think all of your comments have finally caused the penny to drop! Given that my mcu can control the GPIO output, I can wire the switch to an input pin on the mcu and when a change in state on the input pin occurs toggle the output pin.
Yes! I think that is exactly what several people have suggested.
 

ScottWang

Joined Aug 23, 2012
7,409
Thanks ebeowulf17 and ScottWang for you reply also.

Scott - I'm finding it difficult to understand your schematic with no explanation as to what it does and why.

However I think all of your comments have finally caused the penny to drop! Given that my mcu can control the GPIO output, I can wire the switch to an input pin on the mcu and when a change in state on the input pin occurs toggle the output pin.
1. D1, D2, R1 is a Or gate.
2. C1, R2 Differential circuit to get a trigger pulse, 74HC14 is a schmitt waveform converter.
3. D3, R3, C2 is a ac to dc rectifier and filter, it is to make many pulse in and only one high output, for switch that it is a key debounce circuit.
4. C3, R4, D4 is power up reset circuit to make sure the D flip flop will output low, and it also can be avoid some power interference.
5. CD4013 D flip flop is a toggle logic to output high and low.
6. R5 is a base current limiting resistor.
7. The right side of R5 is a npn transistor.
8. And others you should know.

I already explain the individual function, if you still can't figure out the all function, just tell me then I will explain from the input to output.
If you already know the meaning then you just thinking is this circuit suit for you?
 
Top