TL;DR - I need help on making a relay with voltage comparator which accepts two reference voltages.


My project needs to run a Raspberry Pi on solar power. Solar panel will be connected to a power bank which will then be connected to Raspberry Pi.


Running on solar means there won't be enough power left in the power bank for the Pi. In this case I will shut it down and turn it back on when there is enough power. This process needs to be automatic since the project will be installed at a remote place.

Since Raspberry Pi doesn't have ability to detect power from the power bank directly I'm planning to install a circuit between Pi and the power bank. I will use a MCP3008 - Analogue to Digital Converter to detect the voltage level of the power bank. The schematic diagram will look like this. Photo by https://github.com/joachimvenaas/gbzbatterymonitor I will run a script to shut down Pi if it detects only 5% of power is left.


After shutting down Pi, I want to close the circuit between Pi and the power bank completely so I'm planning to use a relay for this. I think Relay with LM393 like this will do the job.

The problem is I want to switch off the relay when the voltage equals to 5% power of the battery and switch it on automatically when the battery reaches 25% of its power. The module I consider can set only one value of voltage.

So is there any way I can set one value of voltage for the relay to switch off and another value for it to switch on?

  • \$\begingroup\$ Welcome. In a word-NO. You need dedicated comparators for each function. Use 2 modules or buy a LM339 quad comparator, some transistors and one relay. But work-load wise it is best to buy what is already made. \$\endgroup\$
    – user105652
    Aug 12, 2020 at 10:13
  • \$\begingroup\$ @VTNCaGNtdDVNalUy Thanks for the heads up. I thought of using two modules but I can't think of a way to connect them. Even in series or parallel since one reference voltage is higher only that module will make the switch on and off and the lower reference module will never work I guess. \$\endgroup\$ Aug 12, 2020 at 10:42

2 Answers 2


So is there any way I can set one value of voltage for the relay to switch off and another value for it to switch on?

You can use a comparator with hysteresis like this: -

enter image description here

This circuit was designed to be a low power (sub 1 uA) comparator that works around 3 volts. It uses R5 to set the hysteresis: -

enter image description here

V3 (the battery) is ramped up from 1.5 volts to 4 volts and the comparator output switches high at about 3.1 volts and then switches low at about 2.9 volts.

If you fiddle with the values a bit you'll get what you want but concentrate on the mid-point between 5% and 25% for the actual set-point and let the hysteresis give you the margins you want.

For instance, if I change R5 to 1 Mohm I get this: -

enter image description here

  • \$\begingroup\$ So does hysteresis region give out the boundary values for high and low voltage which will turn on or off the relay? \$\endgroup\$ Aug 12, 2020 at 10:50
  • \$\begingroup\$ Without hysteresis control (R5 and R4 in the circuit), the comparator would turn on and off at the same set-point. Hysteresis spreads that set-point to produce an upper and a lower boundary set-point. \$\endgroup\$
    – Andy aka
    Aug 12, 2020 at 10:54
  • \$\begingroup\$ Thanks for explaining this. I will try out this approach and update on my results. \$\endgroup\$ Aug 12, 2020 at 11:08
  • \$\begingroup\$ @user8703143 I realized there may have not been enough detail in the schematic so I updated it to make it clearer. \$\endgroup\$
    – Andy aka
    Aug 14, 2020 at 8:56
  • \$\begingroup\$ Thanks for being helpful. As you can see on my other question I'm scratching my head how to stable voltage reference. Since my voltage reference also depends on the battery will your circuit's V2 stay at the same voltage if V3 changes with time? (Edit: I saw your other answer and now I realize you use voltage reference. Now I get this part.) \$\endgroup\$ Aug 14, 2020 at 9:07

It's easy to compute hysteresis for any 2 thresholds, but it's hard when to detect battery power [capacity level] or State of Charge (SoC) if you do not have an accurate means of measuring this. Using voltage alone means you need lots of hysteresis. This is because of battery ESR and a double-layer charge effect in all batteries which results in memory of returning to a resting voltage after being charged or discharged. It may be pretty accurate using the resting voltage but that requires switching off your load and so it may cause a cycling problem. However, if your experience using est. 5% and 25% for Off & On works that must be measured in opposite states. i.e. Turn Off when On at 5% SoC. then Turn On when off being charged at 25%.

It is due to the difference in ESR for the primary and resting capacitance and the operating current that causes this apparent voltage drop and the memory effect to return to the resting voltage. This ESR rises with age and rapidly below 5% SoC, which makes a simple voltage measurement delicate to choose for these thresholds vs a Coulomb counter method, perhaps.

Also you can consider a power FET instead of a power Relay, which also has hysteresis and EMI effects.

Can you define the actual thresholds?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.