Wireless Door Sensor - Ultra Low Power

by vks007 in Circuits > Microcontrollers

2751 Views, 10 Favorites, 0 Comments

Wireless Door Sensor - Ultra Low Power

Door Sensor - Installed on door.jpg
Door Sensor - close up top.jpg
Door Sensor - PCB layout.jpg
Door Sensor - close up back.jpg

Yet another door sensor !! Well the motivation for me to create this sensor was that many that I saw on the internet had one limitation or the other. Some of the goals of the sensor for me are:

1. The sensor should be very fast - preferably less than 5 sec

2. The sensor should run off a 3.7V Li-ion battery as I have dozens of them lying around

3. The sensor should run for many months on a single charge of the battery. It should consume < 10uA in sleep mode

4. The sensor should be able to wake up for transmitting critical data like battery status even when the door is not operated for a long time.

5. The sensor should transmit data to a MQTT topic when the door is opened as well as when the door is closed

6. The sensor should consume the same amount of power irrespective of the state of the door

Working of the sensor:

The sensor has 2 main controllers. The first is tiny micro controller ATiny 13A. The second is the ESP which is usually in sleep mode and wakes up only when the ATiny enables it. The whole circuit can also be made by just the ESP by using it in the sleep mode but the current it consumes is far greater than needed for a battery to last for months so the ATTiny comes to the rescue. It only serves the purpose of waking up every N seconds , look for a door event or a health check event , if there is one, it holds CH_PD pin of the ESP to HIGH and sends the appropriate signal of the type of event to the ESP. Its role ends there.

The ESP then takes over , reads the signal type , connects to WiFi/MQTT , publishes the required messages including battery level and then powers itself OFF by bringing the EN pin back to LOW.

By using these chips this way I take advantage of the low sleep current of the ATtiny and the zero idle current of the ESP when the chip is disabled via CH_PD pin.

Supplies

Pre-req :

- Knowledge of programming a ATTiny & ESP 01

- Knowledge of soldering components on a PCB

ESP-01 (or any ESP)

ATTiny 13A - AVR

LDO 7333-A - Low Dropout voltage regulator

Resistors - 1K, 10K , 3K3

Capacitors: 100uF , 0.1 uF

Pushbutton switch , micro ON/OFF switch - (both optional)

Diode - IN4148 (or any equivalent)

Li-ion Battery

Reed Switch

A case to house it all

Solder, PCB etc

Schematics & Source Code

Schematic_Smart Door Sensor ESP01_2021-05-22.png

Schematics is as shown in the diagram attached.

I have included a P Channel MOSFET for reverse polarity protection. If you don't need this, you may omit it. Any P Channel MOSFET with a low Rds ON will do.

At present the ESP does not have the capability of OTA but that's for future improvement.

Source code smart-door-sensor

Working of the Circuit

ATTiny Working flow

The magic here happens in how the ATTiny monitors the position of the door switch.

The normal option would be to attach a pull up resistor to the switch and keep monitoring it state. This has the downside of constant current being consumed by the pull up resistor. The way this has been avoided here is that I have used two pins to monitor the switch rather than one. I have used PB3 & PB4 in here. PB3 is defined as input and PB4 as output with an internal INPUT_PULLUP on PB3. Normally PB4 is held HIGH when ATtiny is in sleep mode.This ensures there is no current flow through the input pull up resistor irrespective of the position of the reed switch. ie. If switch is closed , both PB3 & PB4 are HIGH and so no current flows between them. If the switch is open then there is no path between them and so the current is zero. When the ATtiny wakes up it writes a LOW on PB4 and then checks the state of PB3. If PB3 is HIGH then reed switch is OPEN else it is CLOSED. It then writes back a HIGH on PB4.

The communication between the ATtiny & ESP happens via two pins PB1 / PB2 connected to Tx/RX of ESP. I have defined the signal as

PB1 PB2 ====== Tx Rx

0 0 ====== WAKE_UP (Health Check)

0 1 ====== SENSOR_OPEN

1 0 ====== SENSOR_CLOSED

1 1 ====== UNUSED

Apart from sending the signal to the ESP it also send a HIGH pulse on PB0 which is connected to the ESP CH_PD pin. This wakes up the ESP. The first thing ESP does it to hold GPIO0 HIGH which is connected to CH_PD thereby ensuring its powers up even if the ATTiny takes away the PB0 HIGH. The control now is with the ESP to determine when it wants to power down.

It then connects to WiFi , MQTT , posts the message and powers itself down by writing LOW on GPIO0.

ESP 01 Working flow:

ESP flow is straight forward. It wakes up and reads the values of the Tx/Rx pins to determine what type of message is to be posted.Connects to WiFi and MQTT , posts the message and powers itself down.

Before powering down, it again checks the values of the input pins to see if they have changed since it last read. This is to take care of a quick opening and closing of the door. If you don't have this check then is some cases you may miss the closing of the door if it is closed within 5-6 sec of opening. A practical scenario of the door being opened and closed within 2 sec or so is well captured by the while loop which keeps posting the messages as long as the current state of the door is different from the previous one. The only scenario it may miss to record all the open/close event is when the door is repeatedly opened/closed within 4-5 sec window which is a very unlikely case - probably a case of some child playing with the door.

Health Check

I also needed a way to have a health check message from the ESP where it sends the battery level of the ESP as well to ensure that the sensor is working fine without manual inspection. For this the ATTiny sends a WAKE_UP signal every 12 hrs. It can be configured via the variable WAKEUP_COUNT in the ATtiny code. This is very useful for doors or windows which are rarely opened and so you may not get to know if something is wrong with the sensor or its battery ever.

In case you don't need the health check functionality then the whole concept of using the ATTiny isn't needed. In that case you can find other designs people have created where the supply to the ESP is fed via a MOSFET and so you can achieve zero current draw when the door is not being operated. There are other things to be taken care of like the current draw to be the same in door open and door close position - for that I somewhere saw an design which made use of a 3 state reed switch instead of the usual 2 state.

Power Measurements and Battery Life

I have measured the current consumption of the circuit and it takes ~30uA when sleeping and around. Going by the datasheets of ATTiny , it should be around 1-4 uA for the entire circuit including quiescent current of the LDO but then my measurements shows 30. The MOSFET and LDO consumes insignificant current.

So a 800mAH battery should last for a long long time. I don't have exact stats but I have been using it on 2 of my doors for more than a year now and each 18650 cell with around 800mAH left in them last for about 5-6 months on my main door which opens and closes at least 30 times a day. The one on the roof door which opens only a few times in a week, it lasts 7-8 months.

Future Improvements

1. The ESP does not acknowledge the delivery of the MQTT message. The program can be improved by subscribing to the topic it publishes the message to confirm delivery or a Async MQTT library can be used to post a message with QoS 1.

2. OTA update: The ESP code can be modified to read a MQTT topic for an update and so enter into a OTA mode to receive a file.

3. ESP01 can be replaced with ESP-12 to get access to more input PINs and so can attach more sensors to the same. In that case communication via the 2 bit method is not possible. This can then be improved to implement I2C communication between ATtiny & ESP. This is a bit complicated but workable. I have it working in another set up where a ATTiny sends rotary encoder values to the ESP over I2C line.

4. The current circuit monitors the internal Vcc of the ESP , If we use ESP12 then this can be modified to read the actual battery level via the ADC pin.

5. In future I will also post a modification to this which can be used as a standalone sensor without the need for a MQTT or any home automation system. The sensor will operate standalone and can make a phone call when triggered - of course it needs an internet connection for this.

6. And the list goes on...

7. Reverse battery protection - DONE (Actual device pictures are old ones and so do not reflect the MOSFET)