I’m trying to figure out if the following comparator circuit (TL331) would be reliable in the following application, or whether I would need to add anything, like hysteresis.
To explain the application, you have a microcontroller (ESP-32 in this case) powered by a 5 V wall adapter and it performs its normal operations ONLY when powered by the 5+ V wall adapter.
If the wall adapter turns off unexpectedly, then the microcontroller switches to the lower voltage battery power, backs up important variables to the internal flash storage and then enters into a deep sleep mode, where it consumes only a few microamps of current. A low quiescent current linear regulator ensures that there is a very low power draw on the battery. An external RTC can also be powered from the regulator.
I want to use a non-inverting comparator for this application, in order to send a digital signal to the MCU that the wall adapter is either ON, or OFF. I have created this using an open drain TL331 comparator and a 2.5 V fixed reference (using a TL341).
When the wall adapter voltage is above a voltage threshold of around 4.6 V (set by the voltage divider), then the open collector output turns LOW, and the MCU's input pin goes LOW.
When the wall voltage goes below the 4.6 V threshold, then the open collector output is disconnected, and the microcontroller registers a HIGH signal, through a pull-up resistor.
The non-inverting comparator and the 2.5 V reference are ONLY powered through the 5 V wall adapter, so as to prevent it from draining the battery when the microcontroller is in deep sleep. The 2 ORing diodes (D1 and D2) prevent the battery from ever powering the comparator.
An inverting comparator (with open collector output) would not work in this case, as you would need to send the MCU a LOW signal when the voltage is below 4.6 V, however, because the comparator is being powered by the wall adapter itself, the low signal will stop being sent once the wall adapter is completely switched off, as the comparator will be unpowered.
I’m wondering if such a comparator for this application would need hysteresis, or any other other circuitry to improve reliability? I’ve read that hysteresis is a bit trickier to implement for a non-inverting comparator however, so how could I do such a thing?
EDIT:
The original schematic uses diode's (1N4001) which could cause too much of a voltage drop for the 4.5 V battery. Therefore, I have changed the circuit to include a Schottky diode and a logic level P-channel mosfet, based on a PSU switching circuit I am familiar with. This will ensure only a very low voltage drop for the battery over it's entire voltage range. source of this circuit: https://electronics.stackexchange.com/a/418277/306786
UPDATE:
Based on Andyaka's suggestions, I think it would be better to go with a sub-microamp comparator (e.g. MCP6546) & reference (e.g. MAX6006), which are both continually powered by the 3.3 V low Q.I. regulator. The comparator can now be inverting, instead of non-inverting and hysteresis can be added with the resistor RH. Here is my basic thoughts for now, without doing any hysteresis calculations yet.
UPDATE 2: To calculate the hysteresis width for my latest schematic, I'm assuming that the below calculation would be suitable for my application, with a fixed reference voltage at the inverting input and an open drain comparator?