Smart wall switches and push buttons

One might think that one of the typical uses for a smart wireless switch (like Sonoff devices) is to be embedded behind a normal wall switch so it becomes a “smart” wall switch. It may seem obvious but it’s not that straight forward. There are several things that get in the middle.

  • Most (all?) the boards have momentary push buttons while wall switches are (normally) toggle switches
  • Most of the available boards in the market are SPST, even those with SPDT relays often only provide terminals for COM and NO, not NC. I only have one one-throw switch at home, all the others are one-way-two-throw and are being used as part of a multi-way switch.

First problem can be easily solved in code. Instead of detecting one edge of the button signal (usually the rising edge since most push buttons are configured with pull-ups) you can detect both edges.

Second problem is harder since it depends on the hard-ware (ehem). But there are a few boards out there with SPDT relays like the Itead 1CH board I recently covered, the e_Goto Wifi Relay Switch Module [Aliexpress] or the Your Cee ESP8266 5V WiFi Relay Module [Aliexpress]. There are also two-relay boards you can use for a multiway switch by syncing the relays in opposition mode using ESPurna firmware for instance (one and only one relay on). The Electrodragon ESP Relay board is one such boards. Another one is the Sonoff Dual.


The Electrodragon ESP Relay board and Itead’s Sonoff Dual

Tricking the Dual

Recently a user of ESPurna asked me how to use the Sonoff Dual with a wall switch. No problem since you can change the firmware to support toggle twitches, right? Wrong. There is a problem with this device because there is a second microcontroller on board that drives the button (and the relays), a Silicon Labs F330. You can control the relays from the ESP8266 sending the appropriate instructions to the F330 and it reports the button events via the same serial line, but only rising edges.

That means you can’t have long click events for instance (since you don’t know when the user pressed the button, only when she released it). But it also means it doesn’t work for toggle switches (wall switches).

Well, it does not work easy, but you can make it work. The trick is to add a little additional circuitry to force the wall switch behave like a momentary push button. At least for those that work in multi-way switch configurations.

If your wall switch is a single-pole-double-throw (SPDT) device it has not 2, but 3 possible states. The third one being when the pole is moving from one output to the other. Its floating. If we connect the pole to ground and the two outputs together to a NPN with a pull-up the wall switch will normally tie the NPN gate to ground and prevent current from flowing across it, except when the pole is moving, during that few milliseconds the output is pulled up by the resistor and the NPN allows the current to flow. You have a push button!

The schematic of the solution

The schematic of the solution. BTN has a hardware pull-up.

It works!

It adds a bit of complexity but it’s working great. I was afraid there was some debounce code that would remove the signal shape (when the pole moving from on contact to the other) but it is very sensitive. A slight push will make the pole lose contact and the relay to trigger, even if it does not travel all the way to the other contact. So you might actually want to add some hardware debounce to the solution.

img_20170206_214431xThe Dual has a header to connect BUTTON0 and BUTTON1. Any of those will work since they are directly managed by the F330. You also have GND and 3V3 (in a different header).


CC BY-SA 4.0 Smart wall switches and push buttons by Tinkerman is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

4 thoughts on “Smart wall switches and push buttons

  1. drbrains

    I just bought a touch-wall-switch which is based on the PSB-B04.

    I originally was thinking about ESP-Easy so I posted some pictures there:

    Lucky for me I got the 2 gang version and it works just like the SonOFF-dual. Managed to reflash the e-Welink firmware to your Espurna. Based on the story how to add a switch I was assuming it would just work OOTB. Took some digging in your code to find out I had to set button-action otherwise the manual changes (user using the button on the wall) would not work.

    1. Xose Pérez Post author


      Glad it worked! Yes, the post in about using a toggle switch in generic, not ESPurna specific. In ESPurna 1.X you have to define the BUTTON_MODE to BUTTON_SWITCH for it to work.

      1. drbrains

        Actuall in your Button code in the Dual specific part, you only define actions for button 2 not for 0 and 1 cause they are not connected in a sonoff dual I assume. I will get some more wall switches, maybe 3 gang versions, so I need to adapt your code as SonOff Triple anyway;)

        Ps: please delete my second post (20/2/17 05:51). The site returned an error but seemed to have stored my initial post after all

        1. Xose Pérez Post author

          You are right. Buttons 0 and 1 are directly attached to the F330 in the Dual so you cannot change their behaviour, but you can still get the notifications of their events and do actions, i.e. the synchronisation between the two relays is possible.


Leave a Reply (all comments are moderated, be patient)