A method to estimate external battery voltage - Raspberry Pi Forums


it's possible use simple resistor capacitor circuit monitor voltage of external battery - long battery voltage monitored within restricted range.

time taken charge capacitor through resistance varies according voltage, resistance, , capacitance (i choose ignore environmental factors such temperature).

if 2 of values voltage, resistance, , capacitance fixed other value may estimated.

can monitor voltage source between 5.8v , 4.6v follows.

feed voltage resistor divider circuit.

code: select all

vin --- 2.2k ---+--- 2.2k ------ ground                 |                 |                 +-- capacitor -- ground                 |                 |               gpio 
arrangement voltage @ gpio between 2.9v , 2.3v (i.e. half input voltage). note lower voltage still high enough seen definite logic 1 on pi. note 2.2k resistor limits current flow maximum of 5800 / 2200 or 2.6 ma. it's important current flow less gpio sink capability of 16 ma.

idea drain capacitor connecting gpio ground (by setting low output). gpio set input , time capacitor charge logic 1 recorded. time proportional voltage. shorter higher voltages, longer lower voltages.

recording time taken @ fixed voltages system can calibrated.

used 100 µf electrolytic capacitor first hand. electrolytic caps polarised make sure negative end connected ground. 10 µf cap may better choice (shorter time charge).

python pot cap script used measure times.

external voltage source controlled turning fiddly trim pot. voltage started @ 5.8 , gradually lowered, pausing @ 5.6, 5.4, 5.2, 5, 4.8, , 4.6.

following initial output of test.

script parameters run time (1000 seconds), gpio (4), , capacitor drain time (200 ms).

output true reading, followed charge time in microseconds, , how accurately start charge time recorded in +/- microseconds.

code: select all

dick /ram $ /code/pot_cap.py 1000 4 200 # rt=1000.0 g=4 drain=200.0 timeout=1.0 true 74274 2 true 74237 2 true 74270 2 true 74290 2 true 74250 2 true 74010 2 true 74194 2 true 74122 2 true 74242 2 true 74165 3 true 74200 2 true 74287 2 true 73988 2 true 73994 2 
results recorded file called vpc.log processed remove bad readings , add line numbers.

awk '{if ($1 == "true") print $2, $3}' vpc.log >vpc1.log
cat -n vpc1.log >vpc2.log

gnuplot used plot results.

gnuplot> plot 'vpc2.log'
vpc.png
vpc.png (33.96 kib) viewed 846 times
time taken charge logic 1 in microseconds, right sample time. may able make out times tried rest on 0.2 voltage decrements.



raspberrypi



Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code