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.

16 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.

  2. Levent ERDEM

    Dear Xose Pérez,
    is it possible Sonoff 4CH Pro 24 volt AC? (input 220 VAC-output 24 VAC or input 24 VAC-output 24 VAC)
    P.S. i need 24 VAC 400 Ma (miliamper

    1. Xose Pérez Post author

      To power the 4CH Pro you will need either 90-250VAC or 5-24VDC. The relays will only switch the voltage you feed them. Reading the datasheet for the HK3FF relays the 4CH Pro uses it’s not clear what is the minimum switching voltage. A minimum voltage a current is required to break the oxidation layer that usually diposits on the contacts with time, Usually small signal relays use special alloys to prevent contact oxidation. I would say 24VAC 400mA should be safe… but cannot really tell. Anyone?

  3. Raj

    Sorry I am still not clear about the wiring (I am a software programmer and just getting into wiring hardware). Can you please post the clear connection picture from all wires from switch to sonoff? Thanks in advance.

    1. Xose Pérez Post author

      I don’t have that switch wired anymore, sorry. I can try to be more clear here (in words). Basically you have to replace an external pushbutton with a transistor.

      The collector of the transistor will be your logical input (a Sonoff Dual has two ready available GPIOs labeled button 0 and 1, a Sonoff Basic does have GPIO14 in the header too). The emitter of the transistor will go to ground. On the base you will have a current limitter resistor (R1 in the diagram) and it will be pulled high by R2. On you switch you will connect both throws to R1 and the pole to ground. This way the transistor base will be high all the time except for the brief moment the switch is travelling from one throw to the other.

      Don’t know if the explanation helps but it’s all I can do right now…

    1. Xose Pérez Post author

      The F330 in the DUAL already sends a message via UART to the ESP8266 when the input changes… what do you mean by tricking it?

  4. Sam

    Gday from Australia Xose,

    I have zero programming knowledge but i’m pretty handy with a soldering iron.

    I was wanting to wire this up using a Sonoff Basic.

    I was going to wire across the existing switch, this should still work yes? (i’m trying to get away without flashing any custom firmware, as i’m sure i’ll quickly be out of my depth)

    Any help would be great.

      1. Sam

        Of course. Its on the control side. not sure on the voltage, but its no doubt minimal.

        Just wanting to confirm how this is wired, as i’m having issues.

        Does this circuit require power?

        I have VCC connected to one side of the existing switch, and BTN connected to the other, and where you have GND, i just have them connected together..

        Its not working, and i feel its something simple.

        Any help would be great!


        1. Xose Pérez Post author

          On the switch, you only need ground on one side and the pulled-up line on the other, it doesn’t really mind which one goes where. as long as the switch is in one of the stable states the transitor gate will be tied to ground and open (since it’s a NPN), BTN should also be pulled-up so you will see a HIGH on the GPIO. While “traveling” the pullup in the circuit closes the gate and the GPIO sees a LOW.

  5. Rubén

    Hi Xose,

    Do you think that I could use your circuit to connect a Simon type wall switch (common in houses) to the button of a sonoff basic (gpio0)? In gpio14 I already have a proximity sensor connected.

    In this way, would the wall switch work to switch the relay (toggle)?

    Thanks Xose,

    1. Xose Pérez Post author

      I can’t see why not. But with the Sonoff Basic you have direct access to the GPIO so you can easily track either edge of the transition. This article explains how to do it when you don’t have such control from the firmware side.


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