A Smart Way To Wire Smart Switches

Smart switches are fun, letting you control lights and appliances in your home over the web or even by voice if you’re so inclined. However, they can make day-to-day living more frustrating if they’re not set up properly with regards to your existing light switches. Thankfully, with some simple wiring, it’s possible to make everything play nice.

The method is demonstrated here by [MyHomeThings], in which an ESP8266 is used with a relay to create a basic smart switch. However, it’s wired up with a regular light switch in a typical “traveller” multiway switching scheme – just like when you have two traditional light switches controlling the same light at home. To make this work with the ESP8266, though, the microcontroller needs to be able to know the current state of the light. This is done by using a 240V to 3.3V power supply wired up in parallel with the light. When the light is on, the 3.3V supply is on. This supply feeds into a GPIO pin on the ESP8266, letting it know the light’s current state, and allowing it to set its output relay to the correct position as necessary.

This system lets you use smart lighting with traditional switches with less confused flipping, which is a good thing in our book. If you’re using standalone smart bulbs, however, you could consider flashing them with custom firmware to improve functionality. As always, if you’ve got your own neat smart lighting hacks, be sure to let us know!

33 thoughts on “A Smart Way To Wire Smart Switches

    1. Wouldn’t you prefer to maintain decent isolation between your low-voltage stuff and the mains? I’d even recommend a current transformer over a regulator, as you can get the same signal without any extra electrical connection at all.

      1. A resistor, a diode, and a single opto isolator. (This will pulse the GPIO input at line frequency – you could filter it to a steady signal with an additional resistor and capacitor.)

      2. Did you miss the additional PC817 opto in series to the 3.3V power supply? Regarding isolation, I would trust on the PC817 far more than on a 2-$-power-supply. And therefore the question remains: Why using the additional 3.3 V PS?

    2. At first I thought sounded a bit expensive, then I remembered my box with dozens of 5V 500ma USB power bricks for which I paid by weight.

      Find some USB dongles that transmit a unique hard-coded ping signal and you won’t even have to wire up the low voltage side.

  1. Why not wire the switch to the micro directly? There is low voltage VCC on the micro and a free pin for input. That way the micro would know the switch status all the time too and could alter the relay accordingly when needed.

    1. That’s exact what I was thinking when reading this. Wire all the switches to the ESP, and let ESP control a SSR to turn on and off anything. You could have half a dozen switches to turn things on and off, plus the IoT side…

    2. they probably didn’t want to break the light. they wanted the regular mechanical light switch side of it to always function reliably, the way it did years ago. not even a knock on the quality of their hack but this is something i’m always taking into account when i deploy one of my hacks into the shared living space. when my hack fails, i still need to be able to turn on the light. if it wasn’t a one-off prototype with bugs yet to be discovered, i wouldn’t have made it :P

  2. I’ve done a wide assortment of Smart Home deployments using Control4 to Home Assistant. I’ve used cheap Tuya Devices, Z-Wave, Zigbee and also used Tasmota/ESPHome on some Tuya Devices that use the ESP8266. I’ve even installed some expensive PoE controlled Lighting.

    The only reason why you need the 110 line is to power the Micro Controller inside the Device and to pass the Live/Hot through a Relay to the rest of the circuit. This is why you need a Neutral Wire. The Device knows if the Relay is ON or OFF by the state of the GPIO pin that is controlling the Relay. Some Devices will have another GPIO that will control a RGB LED via PWM to give a indication of the Light. Same thing can control the brightness of a LED Array in a bulb or whatever.

    A lot of the cheaper WiFi Switches can control a three way / four way circuit with a single switch as long as the switch is made for 3way and is on the beginning of that circuit. Put it anywhere else and it will turn off or end up with a Disco/Rave Light. That is achieved with a Relay that is controlled from the switch leg and tells a GPIO pin the state of that Relay’s OPEN/CLOSE state and will in turn give you a conditional output. So if the Smart Switch turns on the Light and puts GPIO1 HIGH and you go to the end of the hall and use a dumb switch to turn it off then GPIO2 goes HIGH and GPIO1 is still HIGH. Then you go back and hit the Smart Switch again it will then put GPIO1 LOW but since GPIO2 is still HIGH it will give you a ON state. It’s basically a Electro-Mechcanical OR Gate controlled by a Micro Controller.

    @fizzymagic, You would think a Resistor and Diode is all you need but not even close. That Resistor would be massive and would still be toasty. A single Diode would only make it a Half-Wave and would be a dirty DC signal and would require tons of smoothing either by a Pi Filter or a massive Capacitor. Most of these cheap WiFi Tuya based Switches and even the higher end Z-Wave/ZigBee Switches and Lights use a small crude Power Supply with a Current Limit to power the Micro Controller either at 3.3V or 5V.

    1. Depends all on power requirement. If it should be just a signal, you could use high value resistor (M ohm range) to the µC pin. If it has input protection diodes you do not even need diodes. Of course the signal is 50Hz but that does not matter, the µC can take care of that in software.
      If you want galvanic isolation use a dropper capacitor in series to the current limiting resistor and an Opto. Here you need 2 diodes.

  3. it would be more interesting to use a LDR so it actually is a feedback loop if the lamp is catually on or not, also with this you could turn it of again if nothing is changing (eg outside light)

  4. What exactly is wrong with a light switch?? Mounted on the wall or on the fixture. On, Off, Done. Not once have I ever said “gee, I wish I could flash my lights on and off over the internet.”

    1. It doesn’t have to be over the internet. A lot of the useful functions of smart bulbs can be done with timers, sensors, dimming and colour control but that’s a lot of wiring to hook up. Powerline networking could be used but wifi’s already present and cheap.

      Turning bulbs on and off when people are present, useful for bathrooms and cupboards. Turning them on at set times to wake you up on a winter morning. Changing colour on a schedule to remind you to switch from work to play. Turning off at a set time to remind you to go to bed.

    1. The ‘certification’ on this product is not valid for North America, and cannot be installed by homeowners if they do not want to affect insurance coverage. The ‘CE’ mark is NOT a certification; it is a declaration by the manufacturer that may or may not be subject to any formal certification. Australia/NZ certificates, through the registration process, may use standards that are not scoped per North American building codes.

      For mains-connected stuff in North America, you need a test certificate from a 3d-party lab that is accredited per NRTL/SCC/NOM where insurance is an issue, or if the equipment will be used in the workplace.

      1. Honestly, you’d need some pretty shitty insurance or pretty suspicious conditions for them to investigate the certifications of the wall switches. (That’s been my experience with (unfortunately) 2 very large claims, at least.)

  5. All I want is a home automation light switch that uses a traditional toggle (not Decora) switch, and the switch actually reflects the state of the what the light is (ignore the bulb might be burned out, that’s a non-issues). When I walk into a room I want to slap the switch the way I’m used to and change the state of the light to the state I expect. Off is down is off, on is up is on.

    Send a command to turn the light on from the home automation system, and the light switch physically changes state to reflect the state of the light.

  6. It’s interesting but the downsides i see with this wiring are the power leakeage of the relays (to mantain the relay switched) and the lack of a MQTT on/off state.
    Also (at least important in my country) in the case of a power failure the initial state of the lamp once the power is recovered is dependant of the last state of the swtich.

    Maybe a more or less good middle solution will be the adition of a button on the switch itself that only inverts the state of the ESP, and the ESP will report the change of state via MQTT to the server.

    1. That’s why latching relays are preferred in lighting control applications. The down-side is having to determine the relay state when your controller resets. This adds a manual switch, so you have to determine the switching state anyhow.

    1. The spam bots only understand grade 2 English, are suspicious of any word more technical than “wire” and hate URLs, it might pop up during the next 24 hours when it gets reviewed.

  7. Its all good until all these fancy gadgets stop working for one reason or another. In todays world, companies are trying to fix things that are not broken and the same is true here with these fancy relays. Just have an Electrician install the proper wiring, 3 way and 4 way light switches so that they continue to work correctly for the next 100 years. In my humble opinion, Keep It Simple. If you want a wifi enabled light switch, thats ok, just make sure it works like a regular light switch when the internet goes down or if you switch your wireless router and then you have to go nuts to reset all your wireless wifi devices. I hope my advise helps. Steve Kavallatis, Chief Electrical Instructor & Inspector.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.