Mechanical Switch parallel to Micro Controller Logic

Here is the image for my requirement,want to operate Load Lamp of 230V AC with Mechanical Switch in parallel to Controller where one can 'ON' the Load Lamp from the Mechanical Switch and 'OFF' from the controller and vice versa.

Here only issue is that Mechanical Switch can't work as A FAIL SAFE option as XOR logic gate is involved.

A FAIL SAFE option is my requirement where Mechanical Switch can turn 'ON' the Load Lamp even if the controller or XOR GATE gets fried up or fail's to operate.

In this circuit have shown XOR gate but have doubt whether any XOR gate is available which can operate at 230V AC?

Or is there any way to use XOR logic through other Electronic Components?

Or I don't mind using other logic?

Please help I'm badly stuck because of this FAIL SAFE option

[Edit by @transistor. OP to update table below.]

| MECH | OPTO | LAMP | Notes
| Off  | Off  | off  |
| On   | Off  | on  |
| Off  | On   | on  |

| On | On | off |

The last status I have highlighted because if an XOR gate so available for 230V AC than in code, I can program in way such that if the device is 'ON' from Mechanical Switch and one needs to 'OFF' the Load Lamp than I just need to turn 'ON' the SCR hence as per logic of XOR gate the Load Lamp will be 'OFF'.

Or if any other Electronics component or circuit is available which helps in choosing or manipulating then only last status of logic table change.

Operating voltage, so mentioned in the circuit, for the load lamp is 230V AC and for the controller and opto coupler ia around 5V DC.

Also I need to control the Controller over the Internet.

Also can't use the 2-Pole normal Mechanical Switch as I can't change all existing switches and its a retrofitting job with limited resources.

can't use relay logic due to space constraint.

  • \$\begingroup\$ Try to list your requirements more clearly, with the operating voltage options available. question is really confusing. \$\endgroup\$ – AKR Feb 1 '16 at 8:50
  • 3
    \$\begingroup\$ I don't understand your question. Usually a fail-safe turns off if anything goes wrong. You seem to want it to fail 'ON'. I've added a table for you to edit. Please also clarify 'fried'. What is to happen if OPTO SCR fails open-circuit? And if it fails short-circuit? \$\endgroup\$ – Transistor Feb 1 '16 at 8:52
  • \$\begingroup\$ Your table is a simple OR function. Lamp on if either MECH or SCR is on. The paragraph following the table conflicts with this. Still not clear what you are trying to do. \$\endgroup\$ – Transistor Feb 1 '16 at 10:17
  • \$\begingroup\$ The area I live in has a patchy Internet Connectivity and I'm going to connect the controller with the internet so when the internet connectivity is unavailable one can use Mechanical Switch as I don't want to use Blue Tooth Low Energy or any other communication protocol. Also due to some power fluctuation there is probability that the controller could be damaged so this are the reason for which I want to keep using normal Mechanical Switch in parallel to the controller. \$\endgroup\$ – ankur Feb 1 '16 at 10:17
  • \$\begingroup\$ So is the problem "how to switch off the light if the micro / SCR gets stuck on"? If you just want a manual override you would wire the mechanical switch in parallel with the SCR. \$\endgroup\$ – Transistor Feb 1 '16 at 10:27

try to keep things simple. First of all using a XOR gate on 230V is a very bad idea. You would need opto couplers and make a level translation to make it work (there is no 230V logic).

If i understand your question correctly, you want be able to switch (light) on and off with either a switch or a uC?

There is far more simple way to tackle this: Use a normal two switch light wiring:

dual switch wiring

Please note you have to use a dual terminal switches.

Now just change one of the switches with a relay contactor like this.

IMPORTANT DISCLAIMER: NOT ALL ELEMENTS ARE DRAWN INTO ABOVE SCHEMATICS. CHECK DATASHEET OF ALL COMPONENTS, ISOLATE ALL SIGNAL THAT USER INTERACTS WITH (uC, etc.), MAKE SURE EVERYTHING IS DONE ACCORDING TO SAFETY STANDARDS!! EDIT (due to the comment that "prevents" us from using a SPDT switch): You could implement this with two relays and a optocoupler. You should try to put as much logic as possible in side the uC.

EDIT2: Updated the schematics to include some more components, like diodes and resistors you need. There might be some mistakes, check everything before implementing, you are working on 230V, be careful!

EDIT3: I just realized you can do it with single relay and still operate a mechanical switch if controller fails!

Here is the solution:


simulate this circuit – Schematic created using CircuitLab

Very simple logic behind this one. You monitor the SW1 via optocoupler (not vale of R1 is wrong, just pick the right one for your optocoupler). If there is a change on SW1 you change the P1 pin and make RLY1 to change the light. If you receive the command at the uC to change the switch on/off you just change the P1, and that's it. Relay should be ON by default (no voltage on the coil = on). If you loose power on the processor, there will be no voltage on your coil , and relay will be on all the time. Operation via SW1 is possible.

You you could do something more complicated (i think the first option is fine):


simulate this circuit

If command for switching comes from uC, check if the SW1 is on (P2 will be high). If it is on, you have to turn it off, so switch P3 (RLY2) to off (and RLY1 to off - safety measure). If it is off, switch RLY1 to on, and RLY2 to on. In any case "remember" the state in a variable.

Now constantly check P2. If state on P2 changes, somebody switched the SW1. Check the state of the variable or if you set the RLY1/RLY2 to ON or OFF the last time. IF the light is on, you need to switch it off (RLY2/RLY1 to off). If it's off, you need to switch it on (RLY2/RLY1 to on).

If you pick RLY2 and RLY1 in such way that RLY1 is off by default and RLY2 is on by default (0V on the relay coil) and you put an extra resistor in parallel of relay coils (10k will do), you can loose a processor, and everything will work with sw1.

I suggest you hook P2 to an interrupt handler, so you don't have to constantly check, but rather your uC will wake up on SW1 change.

  • \$\begingroup\$ One more problem is that I can't change all Normal Mechanical Switch to 2-Pole as its a retrofitting Job and has limited sources also this doesn't involve operation remotely through the Internet so.. \$\endgroup\$ – ankur Feb 1 '16 at 10:27
  • 1
    \$\begingroup\$ I don't get the internet part of your comment, but i don't think it matters (you can connect the contact relay to any microcontroller or microprocessor). ok, it that is really the case (although changing switch is the way to go, and such switch is about 3 USD/EUR) then i suggest you use as much logic as possible in the uC. Just put one contact relay after the normal switch and the other in parallel. read the voltage in between them with uC. if you read voltage, and you want to turn on/off do switch the second relay, if you don't read it but you have to read the switch the first relay. \$\endgroup\$ – ursusd8 Feb 1 '16 at 10:35
  • 1
    \$\begingroup\$ @ursusd8: Where can we add the additional components you're going to need? \$\endgroup\$ – Transistor Feb 1 '16 at 11:44
  • 1
    \$\begingroup\$ @ursusd8: Sorry that was intended for ankur. My fault. \$\endgroup\$ – Transistor Feb 1 '16 at 13:32
  • 1
    \$\begingroup\$ I don't recomend using the transistor as it does not provide an isolation from 230V! you can assemble a "death trap". If the space is a concern, use the first schematics with one relay, and use a solid state relay, it's not much bigger than a transistor (if bigger at all). check stuff like this out. Note that you must know how much current you need, and that potential transistor you could use in a death trap scenarou has to be able to withstand it. It will be about the same size as solid state relay. \$\endgroup\$ – ursusd8 Feb 1 '16 at 14:22

OR function

Table 1. OR function.

| MECH | OPTO | LAMP | Notes
| Off  | Off  | Off  |
| On   | Off  | On   |
| Off  | On   | On   |
| On   | On   | On   |

XOR function

Table 2. XOR function.

| MECH | OPTO | LAMP | Notes
| Off  | Off  | Off  |
| On   | Off  | On   |
| Off  | On   | On   |
| On   | On   | Off  |

The table in your question shows an OR table. Your diagram and paragraph after the table show an XOR function. Which do you want?


simulate this circuit – Schematic created using CircuitLab

Figure 1. OR function wiring.


simulate this circuit

Figure 2. EXOR function with relays.

As @ursusd8 says, relay logic is your only option if you can't get at the switch. No matter what you do you're going to need additional components and you're going to have to fit them in somehow.

XOR logic in the micro


simulate this circuit

Figure 3. XOR logic in the micro.

How it works

Monitor the status of the mechanical switch in the micro using a zero-cross style circuit. In this case you can reduce the power dissipation in the resistors as you only need the opto-isolator LED to turn on around peak voltage and even every second half-cycle would be adequate. Use two resistors so as not to exceed their voltage rating. D3 protects the LED on reverse polarity.

You can now carry out the XOR function with the MECHanical switch and the internet on/off signal. (I have represented the Internet on/off as an external input. It will probably already be in the micro.)

I have shown the feedback input as you have it on your sketch.


Note that in your question and comments you state that you want the circuit to "FAIL SAFE" but don't say what the safe condition is. In most cases the safe condition is to power off. Either way, you have a semiconductor device switching your light and you can't predict the failure mode of that. It could fail open or short-circuit. If you had explained the application it may have saved us all a lot of time.

  • \$\begingroup\$ if you turn lamp1 on with mechanical switch, you can't turn it off with otpo.. \$\endgroup\$ – ursusd8 Feb 1 '16 at 11:28
  • \$\begingroup\$ My question contains XOR gate truth table only also I have highlighted by BOLD. Now looking at your circuit lets take a case in which the Load Lamp is turned ON through the Mechanical Switch than one cannot turn OFF the Load Lamp from controller and vice- versa.Please help me out I'm really stuck because of this FAIL SAFE system. \$\endgroup\$ – ankur Feb 1 '16 at 11:36
  • 1
    \$\begingroup\$ To whoever down-voted, please see the trail of comments as we try to figure out what the OP is trying to achieve. The OR table in his OP changed to an XOR mid-way through this. This answer was an attempt to clarify and I can't put tables or schematics in the comments. \$\endgroup\$ – Transistor Feb 1 '16 at 12:39
  • \$\begingroup\$ Answer updated with XOR logic in the micro. \$\endgroup\$ – Transistor Feb 1 '16 at 18: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.