How can the presence of a 5V output from a USB charger be detected using a Raspberry Pi?

Scenario: My Raspberry Pi is powered by a (USB) powerbank which is continuously being charged by a dual-port (USB) wall-charger plugged into the mains [this setup serves as a mini-UPS for the Pi].
One port of that wall charger supplies the powerbank, while the other is unused.

Problem: I would like to detect when the mains supply is off, using the Pi.

Potential solution: Since one of the 5V outputs on the wall-charger are unused, maybe I can interface that to the GPIO pins on the Pi to let it know when the mains is off.
I know that the Pi's GPIO runs off 3.3V logic, so I assume a simple voltage-divider like this will be required.
Are there additional precautions that need to be taken? Any other suggestions to achieve the same goal?

Any help would be appreciated.

  • I came across this raspi-ups.appspot.com/en/hardware.jsp which might give you some ideas Commented Mar 21, 2016 at 6:06
  • @SteveRobillard: Thanks for sharing that. I'd come across it earlier when looking into a UPS for the Pi. However, the LAN port on my Pi is being used rendering this particular solution not applicable.
    – Islay
    Commented Mar 21, 2016 at 6:20
  • I don't see anything wrong in tapping off from the 5V output. I would choose a resistor divider which gave about 3.3V if the input was say 8V (it should still be seen as high if the output is 5V). That would give some protection from (unlikely) rogue voltages.
    – joan
    Commented Mar 21, 2016 at 9:13

1 Answer 1


A simple voltage divider will "work". But how much do you trust your charger, and how much do you value your Pi?

A capacitor in parallel with either resistor helps to the stabilize the sensed voltage, which protects from short voltage spikes. (A capacitor across the upper resistor will in fact INCREASE the transient voltage - if you must do this connect to Ground.)

For longer surges, a Zener diode can give extra protection. But to prevent it from frying as well, you will need a fuse. I.e. if your Zener limits the input to 5.6V and can dissipate 1W, your fuse should be rated at 178 mA.

  • Thanks. I have noted to add a capacitor in parallel to one of the resistors. How should a Zener diode be placed [in relation to the simple voltage divider circuit]? Reverse-biased in parallel to the second resistor? Also, this is the charger I'm using by Zendure.
    – Islay
    Commented Mar 22, 2016 at 0:41
  • A 5.6 V Zener isn't going to protect you when you put in in parallel with a resistor that should be at 3.3 V ! No, such a Zener protects you from a source exceeding 5.6V. An excess voltage causes a significant current to run through the Zener, not through your Pi, at which point to fuse (which is closest to the input ) blows to isolate everything.
    – MSalters
    Commented Mar 22, 2016 at 8:19
  • Thanks, that makes sense. So a 5.6V Zener diode in parallel with the entire circuit? With a 178 mA fuse in series with that Zener?
    – Islay
    Commented Mar 29, 2016 at 4:35
  • @Abhi: Yes, but you're unlikely to find a 178 mA fuse, and besides, you hardly need any current anyway (1mA is sufficient). It was an upper limit based on a hypothetical 1W Zener. On a more general note: if it becomes too complex for you, do remember that the Pi isn't the most expensive device.
    – MSalters
    Commented Mar 29, 2016 at 7:10
  • Got it. It seems hard to find that small a fuse anyway so I'll just let the Zener diode burn out instead. Thanks for your help.
    – Islay
    Commented Apr 4, 2016 at 23:29

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.