ESP8266 Bistable Relay

As you can probably see, I’m putting together the building blocks to do something better.  Each of these articles describes one sensor and how I hooked it up to the ESP, but on their own these are not that useful.  This article is possibly most true in that department.  I have mentioned how much I like bistable relays before, and I mostly mention it to get more people to buy them because right now TE Connectivity seems to be the only people making them and I want tons of cheap ones out there to play with (there are breakouts on ebay, but seriously: more people need to love these things).  The benefit is simply that they are relaxed in two states.  There are/were ratcheting relays that have more relaxed states, or possibly move sequentially IIRC, but what I’m talking about are relays that can be light switches.  That means at least SPDT(3-way switch equivalent), and hopefully DPDT (n-way switch equivalent).  I may be using a power-hungry micro-controller, and not putting it to sleep, but dammit I will not burn current in a relay coil!

The problem with using a bistable relay as a light switch is that without sensing if the power is currently flowing you have no idea which way will turn the lights on or off.  If you remember which way the relay is currently settled you could do a ‘toggle’ to switch to the other position, but this does not help with ‘turn all the lights off’ unless you know which ones are on.  That method also takes a bit in memory because at any power up the relay could be in either position (you can read the state of a digital output to see if it is set high or low, but in this case that isn’t happening).  The answer I’m sure most people would come up with is to use a current sensor.  Either a non-invasive current transformer or a shunt resistor.  That is a fine idea, and there even exists a nice library for doing just that on the atmega 328, but I haven’t gotten around to porting that to the ESP yet.

The very clever method I saw once was to take a neon tube, a 100 ohm current limiting resistor and a photo-diode.  You wire up the resistor and neon tube to glow when the power is flowing to the light and detect it with the photo-diode, putting the whole thing in heatshrink.  This is a very simple opto-isolator, but it uses a neon tube to be compatible with high voltage.  There are detectors that can sense voltage and even digitize the frequency, but I would much rather have current reading so I’m holding out for that.  I may use the neon tube trick to get off the ground if I can’t port the current reading code fast enough, or I could throw on a 3.3v arduino pro mini and offload the analog to that.

The current implementation is just controlling a power strip with no feedback (look for the current reading or voltage detection in my power node post when I get that solved).  The relay takes the 3.3v signals and drives the coils through 2 2n7000 FETs, they’re very convenient and I’ll probably get a favorite smd one but for now I love these.  The relays are 5v coil, and get powered before the 3.3v regulator on the NodeMCU.  I have 2 pins, and while I call them On and Off, I labeled the relay with A, B, and C(ommon) so I wouldn’t forget what the pinout was (or which FET triggered what.  This setup’s limitations are that I cannot determine the state on boot, and cannot determine if it is flowing current or not.  I have listed how I could do that above, and even now I could have it switch Off on boot just in case it was on, but I haven’t.  The code assumes off on boot, but intil you trigger one of the coils that will not sync up with what the output is.

What I did for MQTT commands was to have the relay only triggered when needed.  I envision sending broadcast messages saying to turn off the lights and I didn’t want to fire the coil if it wasn’t needed.  This also allows me to keep track od the relay state so I can poll for it without changing it.  This, coupled with MyMQTT for android basically recreates the other networked power strip that I built before, but I have to be running a server for this to work.

As should be expected the code is here.

The rest of this series can be gotten through from the home page here.


5 Responses to “ESP8266 Bistable Relay”

  1. Intro to my ESP8266 adventures | Evan's Techie-Blog Says:

    […] Bistable relay […]

  2. Jean Roux Says:

    Hello Evan, this has intrigued me, and sound like what I want to do, in that one wants to still be able to switch on/off at wall (replace with a push-button?), but be able to control lights from i.e. Node-Red with a UI, or some logic (switch all lights on). Your comment:

    “dpdt bistable one (so it can man-in-the-middle a lighting circuit) and having the adc hooked up to a CT to detect the current status of the lights”

    Could you elaborate further, maybe have you published the circuit somewhere, and could you point me to typical parts? Why not use an SPST bi-stable for instance? Could you please point me in the right direction? the ADC is also important to me, one needs to know if the lights ae on or off before switching from central. Thanks!

  3. Jean Roux Says:

    Sorry, this is a copy-and-paste comment from Scargill’s site, before I knew you described it on this blog. cancel the part above about ‘have you maybe published a circuit somewhere’.

    – Have you made progress with reading if power is on using ADC?
    – Could you link to a part like on eBay for the relay you propose?
    – Still not sure why for what I want to do, SPST bi-stable will not work.

    (I want to replace light switches with an ESP inside, and a pushbutton on the panel, to enable manual switching, and switching from central node-red on RasPi)

    • abzman2000 Says:

      ok, I made progress by stumbling across this article:

      I haven’t built it yet, nor have I found out how to translate it into using the onboard ADC (which should make it cheaper). I did, however, buy some of those boards. I may get around to it this weekend.

      normal – spst
      3-way – spdt
      4-way (n-way) – dpdt

      you need at least a 3-way if you want two switches to control the same circuit independently. if you want any more sources of control you need 4-way switches (and can get away with 3-way on the ends).

      Personally, for your project, I’d go find an electrodragon switch module ($6, includes power supply and board for the esp) a bistable relay (can be spdt if the switch it’s replacing is 3-way or just a regular one) and an ADS1115 breakout board + curent transformer. You can desolder the relays on the electrodragon and use a bistable controlled by the pins each normal relay was previously controlled by, except it will latch after 100ms and you can stop draining power.

      I’m in the middle of rewiring my house, and one thing I insist on is that each wire powering an outlet or switch comes straight up from the basement, from a central junction box for the circuit. This means that I know exactly where I need to break the circuit if I want to drop in an esp8266 acting as a virtual 3-way switch. This doesn’t give a pushbutton the existing switch, but with mains wiring I’ve found that there’s not much room in those electrical boxes. I agree with the idea that me pressing a button on the wall means I want the switch to go to the opposite position, I don’t care what it is now, but if I’m reaching over to switch it then I should be able to press the same button for on that I do for off. In my house there would be a large (plastic) electrical box on the basement ceiling that houses the esp and relay, and the dumb switch upstairs would remain (because if the esp fails, the dumb switch still works). I would have pushbutton switches, but not the ones you’re thinking of.

  4. Jéan Roux Says:

    Thank you! Looking forward to your further work on this.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: