Electrical circuit math

lbenson

Senior Member
Could someone assist me with some math, probably simple if one knew what one were doing? I want to measure a battery level on a small picaxe module using the method +V->R->LED->0V, with readADC10 at the R/LED junction (as the battery goes down, the voltage forward on the LED remains the same, so the proportion that it is of the total voltage becomes higher, resulting in higher ADC readings). My R is 22K (to minimize battery drain), and the data sheet says that the typical Forward Voltage of the LED is 1.6V (at IF = 1mA).

Using a bench power supply to dial down the voltage, I get the following readings.

Code:
5V5 294
5V  320
4V5 355
4V  394
3V5 452
3V  524
So the circuit represents a function (Dippy's Al Gore Rhythm) such that f(voltage) = readADC value. I'm sure that Ohm and 22K and 1V6 apply somehow, but I can't puzzle it out. What formula approximates this curve? I could produce a table, but what fun is that?

Of course, if there is a better way to measure the battery drain, I'd be happy to entertain suggestions.
 

BeanieBots

Moderator
If the 1.6v was a proper reference voltage, then it would be quite simple. Unfortunatley, the LED volt-drop will vary with a supply voltage (non linearly) so it's not very easy to make an equation.

For FIXED reference:

ADC value = (255/Vcc)*Vref or (1023/Vcc)*Vref for "ReadADC10

Rearanging gives:
Vcc=(N/ADC_Value)*Vref where N=255 or 1023

You might get something reasonable if you replace Vref with a function based on Vcc and then solve as a quadratic.

EDIT:
Actually not too far out for your results but try again at a different temperature and actually measure the LED volt-drop.
 
Last edited:

vttom

Senior Member
FYI - I think 22k is much too big. The problem is that the resistor limits how much current flows through the LED. With too big of a resistor, not enough current goes through your LED and it will not be a very good constant voltage reference.

Since we know V=IR, we can turn that around so that R=V/I. We want to make sure I=1mA for the smallest expected voltage across the resistor.

So, this all works out to: R = (3-1.6)/0.001 = 1.4kOhm.
 

gbrusseau

Senior Member
(ADC READING)=775-[90*(BATTERY VOLTAGE)]
(BATTERY VOLTAGE)=8.2-[0.01*(ADC READING)]
This is a linear approximation.

A non-linear but closer approximation can be
(ADC READING)=1523/(BATTERY VOLTAGE)+16
(BATTERY VOLTAGE)=1669/(ADC READING)-0.2

Both equations are based on your table and as BB mentioned, the diode voltage drop is not constant.
 
Last edited:

hippy

Ex-Staff (retired)
Another way to do it is with a lookup table. Between 3V and 5V there are less than 256 values so that would fit into a PICAXE which doesn't have shared data and code. Even with data and code or a wider range of values you can make the table smaller, then calculate the value between the above and below values linearly. The more data points the better.
 

Dippy

Moderator
If the device is going to be used in the big wide world then you'll also have to factor in a temperature correction. I know LEDs are cheap, but as a reference you may as well use a piece of putty.

With our combined salaries of £5000 per hour it would be cheaper to buy (and use) a precision voltage reference. Or even a prec current reference with a couple of things to get a steady voltage.
 

lbenson

Senior Member
Thanks for the suggestions and warnings.

First, this is not going into the wide world--strictly personal use. Second, for the critical time period for me--through the winter months in Canada--I can be pretty assured that the temperature will vary by only a degree or three from 5 degrees centigrade (at least, that is what the overall system will be trying to assure). I should perhaps have said that I'm only looking for a rough estimate with accuracy within about 2 tenths of a degree in the range of 3V to 5V--that's only 11 .2-difference values for a table, but space is always tight on the 14M and 08M, and table processing code takes away even more.

vttom--you could well be right about the LED's being a poor constant voltage reference at 22K (or, per Dippy, at any current), but a continuous 1mA would be a higher power budget than I would want for this function. I did not see any significant variation in the ADC10 reading unless I turned the voltage dial on the power supply. Since I will want to convert to 2-tenths of a volt, with hysteresis, variation may not be significant. Mainly I want this to be able to monitor remotely how the batteries (3 or 4 AAs (caveats noted)) last over the course of 7 or 8 months.

I will play a bit with the equations of BB & gbrusseau and see what I find out.
 

BCJKiwi

Senior Member
As per Dippy's suggestion - use a real voltage reference.
A range of Voltage reference chips are available from Futurlec for < US $1 - eg LM285Z-2.5.
2.500V 1.5% tolerance, -40 to 85degC, temp compensated, TO-226 3 leg package, uA drain.
 

lbenson

Senior Member
Battery level measurement with LM336 reference diode

I repeated this but used an LM336Z-2.5 2V5 reference diode ($.36US at Jameco) instead of the LED, with the adjustment pin not connected. With this I get the following readADC10 readings:

Code:
5V5 378
5V  385
4V5 389
4V  408
3V5 440
3V  494
2V5 561
On one pass I made reading every three seconds, and out of 10 readings at each voltage, the value read differed by no more than one. I repeated this and got slightly different readings below 4V5 but still no more than a difference of one at any given setting (my power supply only shows .1V differences, so there is some slop in the settings).

Since nothing is done on my sensor module depending on the battery value, perhaps all I really need to do is send the raw ADC value on to the monitoring program, and let +it+ warn me if a battery is getting low. There will be more program and table space available there.
 
Top