Zero Idle Current/Ultra Fast Contact Sensor - Espnow or WiFi Based

by vks007 in Circuits > Wireless

1017 Views, 1 Favorites, 0 Comments

Zero Idle Current/Ultra Fast Contact Sensor - Espnow or WiFi Based

Contact sensor 5.jpg

Here I present a simple yet powerful contact sensor which can be used for a door / window / cupboard to indicate its state as open or closed.

The main advantages of this circuit are:

  • It consumes zero current when idle
  • It is instantaneous in response - takes 90ms to send the message if we use espnow to send the message
  • Uses jellybean components to achieve zero idle current , no expensive and complex ICs
  • Can be modified easily to integrate with other sensors like motion sensor or any other sensor which can provide a HIGH pulse
  • and maybe many more....

The inspiration for this was the Trigboard from Kevin Darrah. His Trigboard consumes zero power when idle and can be connected to a range of sensors. But his hardware is quite complex and is not open source so I decided to build one for the specific use case of a contact sensor.

Assumptions on existing setup , pre-requisites:

  • You know how to solder components
  • You already know programming of the ESP microcontroller
  • You have an existing setup of an automation hub which hosts MQTT

Supplies

BOM

Component Qty Remarks

SPDT Limit Switch - 1

10uF electrolytic capacitor - 3

47uF electrolytic capacitor - 2

0.1uF capacitor - 3

10K resistor 1/4 watt - 3

100K resistor 1/4 watt - 2

1K resistor 1/4 watt - 1

560K resistor 1/4 watt - 1

Si2301DS P Channel MOSFET - 1 (Any P Channel MOSFET with Vgs > 2.5V will do)

BC548 NPN small signal transistor - 3 (Any NPN transistor will do)

BC558 PNP small signal transistor - 1 (Any PNP transistor will do)

push button switch - 1 (optional for manual reset)

5V to 3.3V regulator module - 1 (any would do, any 3.3V LDO can be used too)

male berg strip - 1 (optional for serial headers)

polarized battery connector - 1

Diode IN4148 / IN4007/ IN5819 - 1

ESP 12F - 1 (ESP01 will not work, any other ESP will do)

PCB perfboard (3cm X 5cm) - 1

wires, soldering parts - 1

Li-ion Battery - 1 (Any other battery with voltage >3V & < 5V)

Box - 1


Other considerations about the components:

  1. Transistors could be any NPN and PNP transistors, the resistors may be appropriately changed accordingly. I use them as a switch here so any general purpose transistor would do.
  2. The MOSFET can be any P Channel MOSFET which can work with 2.6 - 5V.
  3. The max working voltage for the circuit can be any voltage the transistors are chosen to work with while the minimum will be ~2.6V below which the ESP would not work.
  4. C1 & C2 could be varied but I had difficult in getting a clean pulse with values below and above 10uF. This could be some specific issue in my set up though.
  5. I have included the breakout of the MOSFET as most P Channel MOSFETs are in SOT-23 package. You can omit the breakout board points, if needed.

The Challenges

There were a couple:

  1. A simple reed switch works on the principle of a pull up resistor and then changing state as per its position , so that was ruled out as a sensor, I then turned to the SPDT limit switch (bump switch) which gives the flexibility of a common , NC and NO connection.
  2. I had to design a circuit to create an edge detector which will provide a pulse every time the switch changed state so I turned to the pulse circuits using transistor and capacitors. Many thanks to the author of this video for getting me started on this.
  3. I had to then latch the circuit to an ON state once the pulse was received. Electronoobs's video on a latch circuit provided me the answer.
  4. I wanted to build a watchdog feature too which will cut power after a certain limit of time. This is something which i still haven't perfected but a crude version of it is in place. more on this later
  5. Finally I wanted to use espnow to transmit the messages , there were a lot of challenges in making this happen but I wont go into all those details here.

Schematic

IMG 987.jpg
Schematic_Zero Current Contact Sensor modules.jpg

The schematic consists of very clean modular parts which are easy to understand and debug. The schematic attached shows the various modules, if you build it , you may build it module by module and test if it works fine before proceeding further. I would suggest the following order

supply connections, +ve edge detector, -ve edge detector , latch circuit (except Q4) , Q4 reset circuit, ESP, watchdog

  1. The edge detector - one each for a rising edge and a falling edge. The NC and NO switch contacts are connected to Vcc and GND respectively so that when the state changes we get an edge. The input from the COM terminal of the switch is fed simultaneously to two transistors one of which is wired as a rising edge detector and the other as a falling edge detector. The output of these transistors is also coupled together and thus provides a pulse on every change of state by the switch. This state is also responsible for the fact that because it uses capacitors, it does not use any power once they have charged or discharged.
  2. The Latch circuit - This circuit receives a pulse from the edge detector and then holds the power from a MOSFET Q3 to HIGH latching it in place. This can be reset to LOW only by grounding the gate of the MOSFET which is done via the transistor Q4.
  3. ESP8266 (or ESP32) - Once the ESP is powered ON, it sends a message and reads the state of the contact sensor via a GPIO5. It then sends the reset signal to transistor Q4 hence cutting OFF its own power supply
  4. Watchdog - Resistor R7 and C6 form a simple and crude timing circuit and is tied to the output of the MOSFET. As the circuit turns ON, it starting charging slowly and once it reaches the threshold voltage of the transistor Q4's base it turns it ON cutting power to the circuit. This is useful in case the ESP hangs or runs into an infinite loop. It ensures that the circuit wont be powered beyond a certain time. Current values on the circuit provides anywhere between 1-3 sec of delay. The current circuit design suffers from a disadvantage that the capacitor does not have a direct discharge path and so discharges slowly via R4 & C4 thus providing a variable delay depending upon if it gets enough time to discharge.

Other considerations:

  • Capacitor C4 serves to absorb the initial chatter that happens on the GPIO pin when the ESP starts off. Because a HIGH value on this pin on startup would cause the transistor Q4 to conduct and cut off power, this should be low on startup. It is because of this reason that a ESP01 cannot be used in this circuit as GPIO1,GPIO0,GPIO2 are held HIGH on startup. GPIO3 also acts as Serial on startup and so ruled out.
  • A manual reset button S1 can be put to cut off power manually, this comes handy while testing the circuit without the ESP
  • cap C2 , 0.1uF is to prevent triggering of Q2 due to noise.
  • C7, C8 is for stability of power for the ESP, you may skip it but it is recommended, values are not critical


The Hardware

Contact sensor 1.jpg
Contact sensor 2.jpg
Contact sensor 3.jpg
Contact sensor 4.jpg

Attached are pics of what I put together on a perf board. It is better if it is fabricated on a PCB but then that's for another day.

A pic also shows the final product housed in a box and mounted on a door. There are a few considerations while mounting it on the door. Its not as flexible as say a reed switch based sensor is. The limit switch has to have a few mm distance from the door so that its opening and closing makes it click well. It will require mounting the PCB in the box with screws. You can use a small piece of foam on the door edge to ensure the distance between the door and switch is not so critical. The foam will provide cushion for the distance.

In the video attached you can see the prototype I put together on a breadboard. It shows the sensor consumes zero current on idle. Hurray!

Software

For the software on the ESP there are multiple options one can choose, Although I mainly use espnow for sensors as it is fast and reliable , it is up to individuals to choose what they want depending on the existing setup (or the absence of it).

Option1: Espnow / MQTT - I have a separate instructable on starting off with espnow and all software required, please take a look at it here ==> https://www.instructables.com/Espnow-Based-Sensors-With-MQTT-Home-Assistant/


Option2: WiFi / MQTT - As espnow isn't so common (as yet) most people would probably have some automation hub set up and they would want to send the message to that system. MQTT is a common way to pass along messages and this piece of software does the same. It assumes you already have a MQTT setup up and running.

Code for WiFi / MQTT integration using Arduino IDE is attached in this section below.

Modifications for Other Sensors

The circuit can be modified to connect to any other type of sensor instead of a contact sensor. The stage to be replaced for this is the edge detector.

  • For example to use it with a motion detector , replace the edge detector with the motion sensor where the collector of Q5 is tied to the output of the sensor. The sensor should be an active HIGH sensor i.e should give out a positive pulse on activation. Of course in this case the motion sensor would always be ON and must be supplied power directly from the battery.
  • If you dont want the watchdog feature, you can remove R7 and C6. In this case the circuit will remain ON until it receives a HIGH signal from the ESP.
  • You can use any other device or controller with this circuit instead of the ESP subject to power ratings of the MOSFET
  • If you use the LifePO4 battery, its minimum and maximum voltage is within the ESP ratings so you can omit the 3.3V regulator

There could be many more, maybe others can comment.



Final Thoughts

This hardware circuit is one of the best designed one as it uses only jellybean components. A more robust one can be designed by using specialized ICs as Kevin Darrah has done but , hey it works! I have it deployed for 2 weeks now and it hasn't missed a beat.

Further , usage of espnow enables it to have an instant response. By the time I remove my hand from the door handle the status in Home assistant is already updated. It takes around 90ms for the esp to post the message to the gateway and another couple of ms for the gateway to post to MQTT.

If we use WiFi and MQTT then it takes somewhere around 3-4 sec to post the message.

As for battery life, I really cant measure that, I am using a used 18650 battery with about 700mA from full charge to 2.6V, I expect it to last a very very long time. While theoretically this is a zero current draw circuit , it may have some leakage currents in the capacitor, MOSFETs and transistors. They will be in the range of pico/nano amps.


Also, open to hear out any improvements to this circuit, I am sure they will be a couple.