I'd like to detect whether an external device is switched on or off. I found two pins where there should be 5V between in the first case, but the voltage may vary a bit and I don't wont to break the Pi. How should I connect the points to GPIO pins to detect whether there is voltage? Or to ask more general:

How can I use the Raspberry Pi to test whether a battery is empty (no or little voltage) or not (nominal voltage)?

If analog measuring is not possible, can one measure at least if voltage is above some threshold? This would result in a digital signal, but how to generate it and which GPIO pins to use?

4 Answers 4


With the Raspberry Pi alone you cannot, cause it has no analog-to-digital converter (ADC). You could buy this addon card http://www.abelectronics.co.uk/ and make a voltage divider with two resistors over your battery supply.

  • 1
    Thanks for clarification and link to the ADC addon card.
    – Jakob
    Sep 17, 2012 at 5:54

You want to look for or build yourself a comparator with the appropriate characteristics. In your case, you want to check a voltage is over say 3V3 and output 3V3. Make sure your comparator is tolerant of 5V.

  • Comparator :-) nice solution. Never used one of those. Will keep it in mind. +1
    – Piotr Kula
    Sep 18, 2012 at 18:27

The add on card is cool but you could use a voltage regulator that is cheap and simple.

A voltage regulator, depending on the model expects an voltage input of whatever it was designed to handle and will output the desired voltage on the output pin. In your case you want to sense 5volts but read 3volts which is safe for the Pi.

You can find ample supplies on ebay but here is a Sparkfun link to an example.

If the external device provides any current upto 10vDC then the output pin gives 3V (HIGH ON DIGITAL PIN) and you can safely read that using GPIO in python or whatever you want.

You can find them dirt cheap if you look around. You just HAVE TO MAKE SURE the output is not more than 3.3volts which is connected to your Raspberry. You could put a resistor on the output to drop the voltage a bit.. but regulators are designed to be 99.9% accurate on output.

The Raspberry Pi uses a NCP1117 3.3Volt to Drive the 3V3 power bus which gets it power from the 5V0 bus which comes from your USB power supple.

Voltage regulators are used to supply power to other devices. In this case I just used imagination to use a cheap solution and used it the other way around.

Because end of the day we just need to sense a voltage as a digital signal. It does not really matter what you use to generate it. It could be a 3.3v battery, 3.3v dc power supplyy, a buffered digital output from an Arduino, another Raspberry, a signal generator, a 555 timer, or a simple voltage regulator. It the implementation and goal that is important.

  • End of the day ...
  • 3.3v = 1
  • 0v = 0


Maximum voltage on GPIO

Maximum Current on GPIO

  • A reason for downvote would be very constructive. Maybe this answer unconventional- But if it is very flawed then please let me know.
    – Piotr Kula
    Sep 18, 2012 at 18:26
  • 2
    Interesting idea, might use it in a project of mine (test if the 12V rail is active or not)
    – mglauche
    Oct 6, 2014 at 21:27
  • Just make sure to put a pull down or pull up resistor on the Digital pin. This is a very old answer.. one of my first. But the ideology is the same.
    – Piotr Kula
    Oct 8, 2014 at 7:33

If only voltage on/off needs to be detected, a couple of standard resistors and an optocoupler should be a safe way to do it.

dc voltage sense

click the circuit / flip the switch

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.