6
\$\begingroup\$

I have a light switch that switches 240v (UK) mains. Instead of using that to directly control a light, I want to use it as an input to a controller board so I can also automate the lighting. (It's low voltage LED lighting driven via a transformer although that doesn't really affect my question).

My question is what is the best and safe way to use switched 240v mains power as an input to a low voltage microcontroller circuit? It would be best to use the existing switch to switch a low voltage control signal but I can't change the wiring at at this time so the only input I have to my control is a switched 240v mains power line.

I have other power available where the controller is so I'm not intending to power this via the original light power, I just want to see if I can use it as a control line without extensive rewiring work for now.

\$\endgroup\$
  • \$\begingroup\$ I'm interested to know what other people would do for this; my way would be a half wave rectifier, a cap to ground big enough to hold the voltage at a suitable d.c. level, into a big enough resistor (in terms of value and power rating) and into an opto to feed the input to the micro. Can't say I'm confident enough to say this is the best way to do it though! \$\endgroup\$ – DiBosco Sep 12 '17 at 11:10
  • \$\begingroup\$ @DiBosco - I have written an answer below. I have been using this circuit for a while and this appears to be the best in terms of PCB area requirement and cost. Plus, it provides optical isolation. \$\endgroup\$ – Whiskeyjack Sep 12 '17 at 11:20
  • 2
    \$\begingroup\$ I'm interested in how you can wire the controller board to the light fitting, but not the switch? Usually in UK wiring, the wire goes from the switch to the light fitting (ie it's connected supply => fitting => loop to switch), so you can often disconnect the switch at the fitting, and run a new cable from the switch to the controller at the same time as you're running cabling from the build to the controller. Or is there something else in play? \$\endgroup\$ – psmears Sep 12 '17 at 16:21
9
\$\begingroup\$

I have been using this circuit for a long time now:

220V to uC

300 K resistor on high side limits the current to a safe level. Make sure it satisfies the voltage ratings. As pointed out by @Jeroen3, a 0805 resistor wont be enough for the job.

Optocoupler is K814P. Usually 817 series is single direction and 814 is dual direction. Ex - PC817 and PC814.

RC network on output side smoothens the ac output and maintains a contant DC voltage on output pin which can be fed into any microcontroller.

When you close the switch, you will get an instant HIGH on OUTPUT pin but when you open the switch, it will take a while (300 ms or so) for the OUTPUT pin to go LOW due to the 0.1 uF capacitor. If you want to decrease this time duration, play around with the values of R and C. Reducing them will give you a faster response.

\$\endgroup\$
  • 1
    \$\begingroup\$ Neat. What's the name for this type of opto that conducts in both directions? Do you have an example part number? \$\endgroup\$ – DiBosco Sep 12 '17 at 11:22
  • \$\begingroup\$ Keep an eye on the resistor power and transient rating when doing this. An 0805 SMD resistor won't like this! \$\endgroup\$ – Jeroen3 Sep 12 '17 at 11:23
  • 3
    \$\begingroup\$ When sizing the R12 resistor for current limiting keep in mind that the 240V is RMS voltage, the peak voltage is about 1.4 times that. \$\endgroup\$ – ratchet freak Sep 12 '17 at 11:35
  • 4
    \$\begingroup\$ You will find that in most designs where safety and reliability are of concern the R12 resistor is implemented with at least 2 or even 3 resistors where a short circuit failure of one will still operate and the voltage and power specifications are not exceeded. Two shorted resistors would then probably exceed the power rating of the last and cause it to fuse open circuit this may be a risky design move but could be practice in some places. I would prefer to have a pair that can each handle the full mains voltage and power dissipation. \$\endgroup\$ – KalleMP Sep 12 '17 at 14:37
  • 2
    \$\begingroup\$ @Trevor But you'd need to keep R17 as a pull-down resistor, to avoid the output floating unpredictably when the optioisolator is off. That's unless the microcontroller has its own pull down. \$\endgroup\$ – Simon B Sep 12 '17 at 15:48
2
\$\begingroup\$

Try using a fly wire wrapped around the wires to your mains device attached to the base of a darlington pair. I just grabbed some components out of my electronics box. the transistors are 2N3904 npn, R1 is 330k and C1 is 1.5 MicroF.

With an 8W LED bulb and 230VAC I get the following at the GPIO pin (Pi Zero W). Measuring using cheap Multimeter.

  • Switch Off: 0.02 V
  • Switch On: 2.6V

Amps measured at 3.3V rail is:

  • Off: 0
  • On: 7.7 micro Amp

Expect higher figures if using the 5v rail from the MCU.

Hope this helps.

enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ Will this still work if the lightbulb is unplugged? I kind of doubt it. Honestly I doubt this would be reliable at all, it feels like it would be extremely susceptible to noise... \$\endgroup\$ – Hearth Mar 31 at 15:06
  • \$\begingroup\$ No - it relies on a current flowing so you would need a load. It is absolutely reliable for me and is not susceptible to noise with no false readings to the MCU. \$\endgroup\$ – Tony Jewell Apr 21 at 16:02

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.