Posts: 135
Joined: Tue Dec 18, 2012 10:34 am

Poor man's ADC

Tue May 09, 2017 11:18 am

Hi all.

I have a RPi zero powered through a PP3 9v battery and a switcher to take the voltage down. I would like to monitor the remaining power of that battery by measuring its voltage. A quick and easy way to do this would be with a resistive divider connected directly to a GPIO pin.

From what I can see LVCMOS (3.3v) is specified to report low below about 0.6V and high above about 2.6V (quite a large range).

Has anyone done any experiments to see what the Pi does within this voltage range? For this use case, I would be happy if there was a known voltage at which the GPIO stops reporting high all of the time.



Posts: 6
Joined: Thu Mar 02, 2017 3:11 pm

Re: Poor man's ADC

Tue May 09, 2017 12:23 pm

just get it to report regularly to you and when it stops the battery is too low :D

User avatar
Posts: 13742
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Poor man's ADC

Tue May 09, 2017 12:47 pm

I'd preferentially use a charge/discharge resistor + capacitor circuit. It's likely to be more accurate and easier to tune.

E.g. ... pot_cap_py

Posts: 135
Joined: Tue Dec 18, 2012 10:34 am

Re: Poor man's ADC

Tue May 09, 2017 3:50 pm

Thanks Joan (and triphil).

I suppose I cannot argue much against the ease of a solution that uses a cap and a resistor rather than two resistors but for monitoring battery voltage there is a problem with timing the charging of a cap in that we can't have the cap charging much above 3.3V to protect the GPIO so a resistive divider is still needed.

Say I divide the battery voltage by 3. That will result in a voltage of 3V at full battery capacity and 2.3V (or so) when the battery is spent - that 2.3V could be below the reliable switching voltage of the GPIO.

Nonetheless, your solution is good because I can create a battery-left indicator.

One thing I noticed from your circuit is that the cap discharges straight to the GPIO. Some quite large currents could flow for a short time (depends on the ESR of the cap). I might put a put a protective resistor in there.

Some experimentation will be needed.


Posts: 98
Joined: Wed Oct 24, 2012 9:15 pm
Location: Cheltenham, UK

Re: Poor man's ADC

Thu May 11, 2017 7:29 pm

If you include a series zener diode in the feed from the battery it will provide a (more or less) constant voltage drop to a simple voltage divider network. It will still be very necessary to include a series current limit resistor with the zener diode.

Use Schottky diodes to clamp the zener/resistor network output into the GPIO pin to the 3.3v rail to prevent over voltage into the input pin. Perhaps look-up a typical circuit application.

Choose the zener voltage to move the battery good voltage to 3.3v and battery bad voltage to around the GPIO switching point. Adding a capacitor and a discharge circuit will allow you to time the charge cycle and get a better idea of actual battery voltage.
Share and Enjoy.

Return to “Automation, sensing and robotics”