The ESPurna board, a smart wall switch with power monitoring

If you have read me, you might know I have a firmware for ESP8266-based smart switches called ESPurna. The firmware integrates with Alexa, Domoticz, Home Assistant and about any other service that supports MQTT or HTTP REST APIs. It supports a variety of devices, including almost the whole Sonoff family by Itead Studio, but also some other commercially available boards and light bulbs, and open source hardware projects as well.

But sometimes you just don’t find the proper hardware for your specific case. Maybe it doesn’t expose enough GPIOs, maybe it’s short of analog ports, maybe you need a double-throw relay,… Sometimes we manage to work around these limitations of the hardware adding peripherals or using a thin iron tip. But other times the problem is that it just doesn’t fit.

And size was the main reason I started creating my own smart switch board.

The ESPurna-H board

So the ESPurna board was born, and it got the shape of my home wall switches: 50x50x20mm. Also, I added some features that I have missed in some boards or liked from others:

  • SPDT 10A relay with NO and NC connections brought out
  • Connections for external button and notification LED
  • Optical isolation between the logic circuit and the relay circuit
  • HLW8012 chip for power monitoring


The board uses the Hi-Link HLK-PM01 (Aliexpress) AC/DC that outputs 5V to up to 600mA from an 100-240VAC source. So the board is usable anywhere in the world. There are cheaper options but this one comes encapsulated and has very good reviews. Just before the transformer there is a crystal fuse in series and a varistor across input terminals. I’m also using the trustee Songle SRD-05VDC-SL-C (Aliexpress) single pole double throw relay that supports up to 10A current. The board brings out both normally open and normally closed terminals of the relay, which is very convenient when using it with multi-way switches. The relay is switched by a 2n7002 N-channel mosfet isolated from the controller by a PC817C optoisolator.


The controller can be any ESP12 module, in the pictures it is a ESP12E module by Ai-Thinker (Aliexpress). An AMS1117-3.3 (Aliexpress) does the step-down phase to power the ESP module, there is also a RESET button and a 2×5 header with the programming GPIOs and two connections for external button and LEDs.


Finally the board is ready to add power monitoring circuitry based on the HLW8012 (Aliexpress), the same IC the Sonoff POW uses. The schematic is the same as in the POW, with a 1 milliOhm current sensing resistor and a 2350:1 voltage divider to monitor the AC voltage.

The HLW8012 requires that the power line should be tied to the circuit ground. This means that you should never ever connect the board to your computer while connected to mains. Never ever flash the board while connected to mains!

Open source hardware

The ESPurna-H board is released under the Creative Commons Attribution-ShareAlike 3.0 Unported License (CC-BY-SA 3.0) and follows the terms of the OSHW (Open-source hardware) Statement of Principles 1.0. It can be checked out at my ESPurna board repository on Github.


You can use the latest gerblers from the repository or open the project using Eagle +8.0. You can also order the board directly from OSH Park clicking on the link below:

Order from OSH Park



Please note that you will be using this board at your own risk. This product is meant to be plugged to mains and it requires a deep understanding of the perils of it. I disclaim any responsibility, risk, liability and damages arising out of death or personal injury resulting from assembly or operation of this product.


The board is ready to use an external switch or push button to trigger the relay. But I wanted to keep as much as possible of the original switch enclosure and I didn’t want to drill any holes on it.

So I started doing some tests with touch sensors like the one below. This one is a TTP223B Module Capacitive Touch Switch (Aliexpress) that sells for less than a euro shipping excluded. The sensor board fits tight in the back of the front plate of the switch and it “feels” the contact across the plastic just fine.



The switch is easy to configure from the ESPurna firmware, just define it as a BUTTON_PUSHBUTTON with no pull-up since it’s a normally low sensor.

// -----------------------------------------------------------------------------
// ESPurna
// -----------------------------------------------------------------------------

#elif defined(ESPURNA_H)

    #define MANUFACTURER        "TINKERMAN"
    #define DEVICE              "ESPURNA_H"
    #define RELAY1_PIN          12
    #define RELAY1_PIN_INVERSE  1
    #define LED1_PIN            5
    #define LED1_PIN_INVERSE    0
    #define BUTTON1_PIN         4
    #define BUTTON1_RELAY       1
    #define ENABLE_POW          1

Wall switch base

Another issue I had to solve was how to replace the inner mechanism of the switch with the ESPurna board so it stays put in place. The switches have a plate that is tighten to the wall switch enclosure and fixes the mechanism of the switch. The same plate has 4 pegs that clamp the switch bezel.

I decided to test doing a 3D printed plate with the same dimensions and functionality, holes to screw it to the enclosure, holes to screw the ESPurna board behind it and the pegs for the bezel. I also added a hole to reach the reset button and a window for the cables for the touch sensor and the LED.


I used OpenSCAD to design the board, code is in the “parts” folder in the repo


The 3D printed part and the original switch mechanism


The devil is in the details, but the 3D printed part did it’s job sucessfully



Once everything was ready the first installation was easy peasy. One important requirement is that you need to have both line and neutral wires on hand to power the board. Some switches only have one of them.


In this case I had a plug  and a switch on the same enclosure. I used the cables powering the plug to power the ESPurna board.20170406_163326s

The enclosure with the bezel in place. The 3D printed part keeps the hot zones out of reach and the ESPurna board safe and fixed behind it. The touch sensor is fixed tight in the back of the front plate. The two remaining cables are for the LED but I finally decided not to add it.20170406_164102s

This version of the 3D printed plate does not have the pegs to keep the front plate in place, so I decided to hot-glue the corners. It will be easy to remove and the final result is almost unnoticeable.20170406_171100s

The main difference between the original switch and the modified one (aside for it being “smart”, that is) is the user experience. Originally it was a normal two-position switch with the classical “click” feeling. Now it’s a touch switch. The touch sensor works great but it requires about 500ms contact to switch. It takes some time to get used to it.  But of course you can always ask Alexa to toggle it for you 😉

UPDATE: Check also another project based on this board, the ESPurna Smart Socket, same features, but “portable” 🙂

CC BY-SA 4.0 The ESPurna board, a smart wall switch with power monitoring by Tinkerman is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

64 thoughts on “The ESPurna board, a smart wall switch with power monitoring

  1. Sacha Telgenhof

    Great job! Especially the touch sensor part is cool. Unfortunately my house has old style wiring meaning all switches don’t have a neutral line :(. Otherwise I could definitely use your board.

    For that reason I am designing a touch switch as well using an ESP8266 (so a WiFi switch).

    1. Xose Pérez Post author

      I thought about opening a tindie store. If there are more people interested I might do, but not sure of the kind of product. It could be the board only, a solder-it-yourself kit, a partially soldered kit (with the small SMD parts) or the complete board. At the moment I solder them manually which takes quite some time, maybe I should request a quote to get them populated at the fab…

      1. RJ

        Great job! at that size, it should just fit behind a standard UK switch too, as long as the wall box isn’t a shallow one.

        I would definitely be interested in pre-soldered boards, I have a lot of switches i’d like to automate. soldering all those bits would take me forever

        1. RJ

          Also.. if you are considering getting these prefabbed to sell, if you could fit another relay on there and keep the board size under 65mmx65mm it would be suitable for a standard 2-gang UK light switch. (and 4 relays for a double width switch..)

          I think there’s a market for these, in the UK at least, since the only real option for retro-fit is zwave modules, and zwave is a lot of extra hassle.

      2. Héctor

        FYI: I would consider to get up to 8 or 10 units (finished product) that I could easily install to replace the standard switches (in a Spanish house). I feel comfortable with the software part (using home-assistant here in a raspberry pi with mosquitto MQTT) but I’m not used to solder things.
        Anyway, if you don’t manage to get a final product, thanks for sharing and kudos for this nice project!

        1. Xose Pérez Post author

          Right now it’s not my intention to sell it a a finished product. Maybe as a kit or only the PCB with the small SMD parts soldered in place. I’m trying to get some quotes from PCBA services.

  2. SuperToma

    That’s a wonderful job and thank you for sharing !
    Did you do all the weldings yourself ?
    I’m ok in development but I’m not used to do micro electronic 🙁

    1. Xose Pérez Post author

      Thank you! The welding is far from perfect but now that I have some experience I’m confident with parts down to 0805. Sometimes when the legs of an IC are too close one to each other I try using solder paste and a heat gun. Anyway it’s really satisfying when everything works as expected 🙂

  3. danimtb

    Great job!! I think it is something necessary in the IoT field and I haven’t found a sonoff like divice that suits my needs except from Itead Sonoff Touch. Have you tried that one? Is esp8285 based

    1. Xose Pérez Post author

      I have not tried it myself. The main drawback it has (from my point of view) is that it does not support multiway switches since it features a simple SPST relay. Aside from that it’s a great product. Of course if fully replaces you current switches and I’m not sure it will fit my home wall holes…

  4. Pingback: ESPurna-H is a Compact Open Source Hardware Board with ESP8266 WiSoC, a 10A Relay, HLW8012 Power Monitoring Chip

  5. Pingback: ESPurna smart socket - Tinkerman

    1. Xose Pérez Post author

      That’s a good question. I don’t have my computer with me right now but if I recall correctly those traces are 120 millis width. I think that’s ok for 10A if you use 2oz/ft2 copper thickness. Else you can always add extra tin (the traces are exposed).

      1. LinAdmin

        I wrote about “wires” (20170406_162459s.jpg), coloured red, yellow etc. and not about the printed circuit.

        1. Xose Pérez Post author

          That switch in particular controls an LED ceiling bulb so the board is switching a few watts load. But you are right, the blue wire there is maybe 1mm diameter so it can handle about 2A.

  6. Giovanni C.

    not sure if this is the right place but, I’m trying to replicate the circuit with a board I ordered via OSHPark but I can’t find easily the 0.001ohm 2512 resistor (only from China which takes 45 days for me); any chance I can use a 0.01ohm 2512 (which I can source locally) adapting somehow the cricuit/sketch?

    1. Xose Pérez Post author

      You can easily adapt the code to use a different shunt resistor value, just modify the POW_CURRENT_R setting in the ‘code/espurna/config/sensors.h’ file or use auto calibration if you are using ESPurna or my HLW8012 library.

      But changing the resistor has other consequences you have to be aware. First, the HLW8012 has a limit on the current measurement pins of 30.94mV RMS ( With a 10mOhm shunt you won’t be able to measure currents higher than 3A. On the other hand it also increases your precision.

      Also, a ten times bigger shunt dissipates ten times more power (P=I*I*R), but with a limit of 3A that’s 90mW.

  7. Pingback: A Smart Switch Board For The ESP8266 | Hackaday

  8. threegerbilsinacoat

    The pour that extends under R42, R43, and R44 means you are effectively relying on the solder mask to provide isolation. It would probably be good to pull the pour back from those resistors.

      1. cot.s.ton

        Maybe that could be used for the espurna sensor connection? Adding a PIR or Light sensor would round this out beautifully for replacing wall switches.

          1. Xose Pérez Post author

            Right. I added a couple of VCC and GND pins to its side. I normally use it with touch button modules like the one in the post. But you can use it with normal push buttons or wall switches.

  9. Christian Mai

    Maybe you could use the ACS712 to avoid having the connection between mains and MCU domains?

    1. Xose Pérez Post author

      Actually that’s what I plan to do. The ESPurna-H board uses HLW8012 but I want to do another version (ESPurna-A) with the ACS712. It will probably only do current sensing but isolation could be a plus.

      1. NdK

        Might be interesting to test using two ACS712, one for current and the other for voltage (just using a resistors ladder, like the one used for HLW8012, but no need for the last 1k). Probably the SNR will be quite low, but it’s probably worth testing.

  10. Pingback: ESPurna-H, A Compact Open Source Hardware Wireless Power Wall Switch - Electronics-Lab

  11. Sandy

    Is this ready to sell kind of product? Did you tested it? If yes.. can you please let me know problems you are facing by using it.

    1. Xose Pérez Post author

      I’m not planning to sell this in any way. It would probably not pass any certification in most countries. This is a “makers” project and anyone using it should know about what she’s doing. It’s ASIS.

  12. Sandy

    When are you planning to update this? Are you planning to update espurna framework to 1.8 anytime soon?

  13. Feri Beke

    Hi, first of all, I must say, it’s very nice project (and something I’m looking for), very good work, and thank you for sharing!
    I’m just going to order some boards, but I have some questions/concerns. Am I correct, that all the switched load will go through the 1mOhm shunt resistor? It could be up to 10A, right? Can this 1W resistor handle that big current, or am i totally not understanding the principle? 🙂 I see, the power dissipation will be low enough (10*10*0.001), but somehow i can’t understand (it’s just me probably, sorry), this resistor is part of the “wire”, that should have enough thickness to handle that big current, is this resistor “thick” enough, to handle 10A (altough i guess, I never will swith 10A loads, not even 1A probably)? Thanks for enlightening me 🙂

    1. Xose Pérez Post author

      Thanks! Well, shunts that’s what they are for. A 1W / 0.001Ohm shunt has a current rating of (I=sqrt(W/R)) aprox 30A.

  14. Lapinho

    Hi, very nice project. For a long time i was looking for something like this.
    I have some question related to 10A relay, that current rate is for resistive loads right ?
    If we have other types of loads (incandescent or fluorescent lamp) it can handle really less current.
    And about temperature raise inside wall ? Should the fuse be replace with some kind of thermal fuse ?

    1. Xose Pérez Post author

      Right, the 10A are for resistive loads but actually I would not recommend going further than 5A whatever the load is. In my first design there was a 73C thermal fuse in series with the other one but finally I was carried away by reducing BOM after noticing most of the other solutions do not use one…

  15. Ed Winter

    Looks great. Was just contemplating building a ‘sonoffPOW’ with actually that same psu module in mind, but seems fruitless now as you already did most of the work 🙂
    I was wondering though, in the circuit your R42 is zero ohm basically a wire bridge. Doesnt that make R44 and C42 redundant?

    1. Xose Pérez Post author

      The HLW8012 required the line to be tied to ground, that resistor is a jumper like you say. It’s there in case you don’t need all the power consumption circuitry, you can leave this out too.

    1. Xose Pérez Post author

      The opto does hot help in its original reason: achieving some isolation since the HLW8012 needs line to be grounded as reference. What I might do for the next iteration is to get back the opto between the HLW8012 and the ESP8266. I think that would make some sense…

  16. AntiFan

    I looked at the Sonoff switches, but sadly, they are too large for my house’s switch installations …
    Apart from the light switches (some single, some double) I have lots of dual switches for my window shutters … any chance of seeing a parts list for a dual switch that will fit the same measurements?
    As for the programming for the shutters, the switches are mechanically blocked from both being engaged at the same time – guess an ESP software would have to ensure the same …

  17. AntiFan

    On a side note, how about using solid state relays instead of mechanical ones? I’ve used them in another project, and they work just fine … with a 8-pin DIL package, they’re most likely even smaller with 2 SSR than the one relay on the current design …

    1. Xose Pérez Post author

      it would be quite simple to replace the current relay with two SSR. They give you slightly more flexibility (you can have both ON or OFF) at the price of supporting smaller loads in general. ESPurna firmware already supports several sync mdoes, including “zero or one relays on” and “one and only one relay on” which could fit your needs.

    1. Xose Pérez Post author

      I have ran through that too… my best advice: solder a thick wire between the two contacts and forget about the thermal fuse. I know it sounds drastic but…

      1. Frank

        I once ordered a small thermal fuse, let’s see how they fit. If it works, I will send the model number.

        I noticed something else. The varistor I ordered via your aliexpress link, unfortunately, this burns when directly through. I have now installed this EPCOS B72210S0271K101 and the works.

    1. Xose Pérez Post author

      How many GPIOs does it need? The board exposes one raw digital pin but it could be easily modified to have two.

    1. Xose Pérez Post author

      Out of convenience. Since you have to wire a neutral line to the board to power it and get voltage readings you get a second neutral connected internally to get a neutral out too. This way you don’t have to add an external jumper.

    1. Xose Pérez Post author

      I still have some H boards and I’m testing other HLW chips (with serial protocol). But if I was to do another version I would think of a CSE7766 chips maybe… (ESPurna-C?)

  18. huynhngocthienvuong

    You have a great project!!!
    I wonder that what happen when i change the Line and Neutral . Will it Work?


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

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