Wireless Door Sensor - Ultra Low Power

Introduction: Wireless Door Sensor - Ultra Low Power

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.


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

Step 1: Schematics & Source Code

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

Step 2: 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.

Step 3: 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.

Step 4: 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.

Step 5: 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)

Be the First to Share


    • Crayons Challenge

      Crayons Challenge
    • Fandom Contest

      Fandom Contest
    • Microcontroller Contest

      Microcontroller Contest



    1 year ago

    Hi, thank you very much for post it... Can i ask more info about LDO 7333-A... What is it and What is it uses for here? Thanks again!


    Reply 1 year ago

    LDO is used to drop the battery voltage to 3.3V. As I use a 18650 battery for this , it is necessary. Although it still doesnt prevent overdischarge of the battery , so that part is manual.


    2 years ago

    Very nice description,I was waiting for it since I read the one from @Wim3d :)

    Question: you state the power consumption is below 10 uA, but looking to the datasheet (http://ww1.microchip.com/downloads/en/devicedoc/doc8126.pdf), it seems that the idle-mode consumes 24 uA. Has the Attiny 13A a deep-sleep model as well?

    Thanks and keeps going the good work :)


    Reply 2 years ago

    Thanks for looking into this.
    The power you stated is in Idle mode. I am using AT13 in power down mode. You should be looking at the table 18.1 or Chart in section 19.4 for power down mode with WDT enabled. It says the supply current Icc is in the range of 2uA for a Vcc of 3.3V. The LDO also takes some quiescent current in my circuit. It is also typically 4uA. And as I mentioned, my current measurement wouldn't be very accurate but should be in the ball park. I have read other posts on the internet where folks claim they have achieved 1-4uA power down currents with AT13.
    One useful link on low power AT13 here : https://homecircuits.eu/blog/low-power-picopower-attiny13a/


    Reply 2 years ago

    Thanks a lot for the clarifications - I was confused by the fact that you call a sleep function and I am not too much familiar with such micro-micro controller ;-) the smallest I play with is the EPS8266 :D, definitively a bigger step to take now!
    I will let you know how my experiments go...


    2 years ago

    Nice project and nice description!


    Reply 2 years ago