Reading the supply voltage again....

PFM

New Member
Hello All,

Well I have read several threads on the subject and get the idea you cannot read a battery supply voltage with the Picaxe because the supply is the ADC reference. Well I refuse to give up so easily. I hope I can explain my idea well enough to get help with the math, the part that is not working.

The power supply is (3) 1.5 VDC "C" cells or a base voltage of 4.5 VDC and will run the design to 3.0 VDC. I have added a 2.5 VDC reference to the circuit, attached to AD 2. I have also attached a single cell (of the three) to AD 1.

The 2.5 VDC reference should not change it's output as the cells decay. The plan was this read the 2.5 volt reference the result was 144 bits, then read the single cell 1.5 volts result was 87 bits. So the math looks like this 2.5 / 144 = 0.01736111 * 87 = 1.5104. If we assume the cells will all decay evenly the result could be multiplied by the number of cells, three in my example.

As the supply voltage sags the result for the 2.5 volt reference will increase and so will the results for the "nominal 1.5 VDC" cell. The end result should be the ability to check the supply source with the on board ADC(s).

Now we all know the math above will not work as typed on the Picaxe. I have tried this:

Test_Batteries:


b1 = 0 'Clear Variables
b2 = 0
w5 = 0

readadc 2,b1 'Test Battery needs a AD2 is 2.5 volts
readadc 1,b2 'Single Cell Read
pause 1000

sertxd ("2.5 volt regulator is ",#b1,ret,lfeed)

sertxd ("single cell voltage is ",#b2,ret,lfeed)

W5 = b1 * 10 'keep all references in same power

b3 = 25 // w5 'use 25 for 2.5 as decimal values do not work return the remainder

b3 = b3 * 100 ' get some decimal places

sertxd ("Volts per bit is ",#b3,ret,lfeed)

w6 = b3 * b2 'multiply decimal volts per bit * number of bits of one cell

sertxd ("W6 is ",#w6,ret,lfeed)


I hope this idea may help others with a battery supply that needs to be monitored as well as get some help with my wayward math.

Thanks in advance for any help.

PFM
 

BCJKiwi

Senior Member
Hi,
You say "I have tried this" but you don't share your results!

I'm not sure about "As the supply voltage sags the result for the 2.5 volt reference will increase and so will the results for the "nominal 1.5 VDC" cell."

Since the 1.5V cell is 1/3 of the supply, as it's voltage decreases, then so will the supply so won't it still produce a readadc of 87?

If you make the change to a separate reference voltage then it changes the whole approach.
You should only need to read the reference voltage with readadc to work out the change to the supply voltage. This is how calibadc is supposed to work but see the thread on issues with that (http://www.picaxeforum.co.uk/showthread.php?t=8559)

Suggestion;
When you install fresh batteries, run the readadc on the reference voltage and store the result in eeprom.

You can the compare later readadcs of the reference voltage returned when using the decaying batteries, with the value stored in eeprom to see what proportion of the original voltage has been lost. You don't need to readadc the supply voltage again.
 
Last edited:

PFM

New Member
BCJKiwi,

OH if the results will help here they are. VCC nominal 4.5 has degraded to 4.05 (DVM), single cell 1.35 (DVM). ADC read on the 2.5 volt reference gives 156 bits, 1 Cell gives 85 bits.

Math is 2.5 / 156 = 0.016026 * 85 = 1.36 VDC

I think the design works OK.

The calibadc command is not available on the 18X part and I am not sure it would help as the battery supply voltage degrades.

The math is where my trouble is the // division does not return .016066 even when doing

25 // 1560 returns 25, 25 / 1560 gives back 0 as expected.

Thanks again for your help,

PFM
 

BCJKiwi

Senior Member
The modulus operator does not really work the way one might expect as leading zeros get dropped in the modulus - see the discussion in the programing editor feedback (http://www.picaxeforum.co.uk/showthread.php?t=8564) fallen into this trap myself.

However that's the way it works.

See Hippy's excellent post here
http://www.picaxeforum.co.uk/showthread.php?t=8092

I still hold to my comment "Since the 1.5V cell is 1/3 of the supply, as it's voltage decreases, then so will the supply so won't it still produce a readadc of 87?"

The whole issue with readadc is that it uses the supply voltage as it's reference.
If the supply voltage is read with readadc it will always return 255 regardless of the actual supply voltage. You are reading 1 battery out of three and seeing a small change ((87-85)/87 = 2.3%) but the result you are really looking for (against the 2.5V reference) is showing a change of (156-144)/156 = 7.69%.

You can verify this by using readadc across the whole set and changing the set from 3 to 2 batteries. Readadc will give 255 on either a 2 or a 3 battery set.
It may be the initial charge on your set of batteries is dropping off a little unevenly across the set.
 
Last edited:

lbenson

Senior Member
Good idea about using a 2.5 volt regulator to monitor your battery voltage. Can you not accomplish this, though, with a single ADC read? For instance, as your battery voltage drops, you should get increasing ADC readings on the 2.5V input like the following.

Code:
Battery
Voltage Ref  ADC
4.5     2.5  142
4       2.5  160
3.5     2.5  183
3       2.5  213
2.5     2.5  256
Then your readout, to two decimal places, of the present battery voltage will be as follows:
Code:
readadc 2,b2        ' read fixed reference of 2.5 volts
w0 = 250 * 256 / b2 ' scale up: 2.5v * 100 * 256 / value read
b2 = w0 / 10 // 10  ' first decimal place
b3 = w0 // 10       ' second decimal place
w0 = w0 /100        ' units value
SerTxd("Battery voltage is: ", #w0, ".", #b2, #b3, CR, LF )
As your battery voltage approaches the output of the regulator, you will not have enough voltage to stay in regulation, so you must perform some action before then (I assume that is the point of your doing this). Have you found a good low dropout part? I see an LM2937ES-2.5, which has a .2V dropout at about 100ma output current. Would you have to have a continual current through your regulator? That might draw down your battery. You might be able to use a picaxe pin to turn on the current to the regulator, do the ADC read, and turn it off again, repeating every 10 minutes or however often you thought necessary.
 
Last edited:

hippy

Ex-Staff (retired)
Providing the "voltage reference" you use does not change when the PICAXE power supply does, reading the reference voltage will allow the supply voltage be determined.

Thus a separate 1.5V AA cell ( or 2.5V cell ) to the 3 x 1.5V AA cells used to power the PICAXE would work.

If the 1.5V cell were part of the 3 x 1.5V cells powering teh PICAXE it wouldn't work, because the cells would ( assume they will ) discharge at the same rate, so whatever voltage that cell has it is one-third of the supply, so the reading would not change.

The problem with a separate 1.5V cell is that you don't know it is, or always will be, 1.5V as it will discharge over time. That may be quite some time but nonetheless it will change.

The solution is to create a fixed voltage which can be derived from any power supply so even as the power supply varies the reference doesn't. A cheap but simply solution is a diode or LED plus resistor. The R from +V to diode or LED anode, the diode or LED cathode ( pointy-end ) to 0V. Take the R to diode or LED connection point as the voltage reference.

The advantage of using a LED is that it also serves as a power-on indicator and a LED+R wired this way is often already available on-board the circuit. It just needs a wire adding to give the reference.

It's not perfect, a LED+R isn't as stable as a specifically designed reference voltage source but is good enough for most case. A good diode or zener diode may do better. One could also use a 3V3 or 1V8 voltage regulator. This technique for measuring supply voltage has been discussed in the past ( not just recent days ) on the forum.
 

hippy

Ex-Staff (retired)
A handy tip for detecting and handling battery low ...

If it's only low battery one's interested in, select any arbitrary voltage reference ( LED+R say as above ), lower the picaxe supply to the point that 'battery low" should be acted upon and see what a ReadAdc of the reference gives. Battery okay is above that reading, battery low is at or below that reading. It becomes a simple read and compare to check if the battery is okay or not.

No need to worry about calibration factors or what the reference or supply voltage is to some number of decimal places.
 

moxhamj

New Member
I have used Hippy's approach. Set up a reference, eg a red led and a 10k resistor (or even 100k if you don't want to be wasting power, though an 'on' led is usually useful). Put the picaxe on a variable power supply (a really handy workbench item - buy one or build one out of a 317). Set up a loop to read the voltage on the led with readadc and debug and note the values as the volts go down. Takes less than 3 minutes to set this up and determine the value that corresponds to flat batteries. No maths needed.
 

sghioto

Senior Member
If the design will operate at 3 volts then just use a low voltage dropout 3 volt regulator IC powered by the 4.5 volt battery pak. Next use a simple voltage divider across the battery pak tied to the ADC input to monitor. As the battery voltage drops so will the ADC reading.



When the battery gets to about 3.2 volts the ADC reading should be around 136
 

PFM

New Member
lbenson,

I tell a friend of mine to "Get out of the box" and work through the problem. Well I was in the box this time, thanks for nudging me out of the box. The single ADC read on the 2.5 reference voltage worked just as you described. I did a simple fuel gauge serial output for a couple of ranges and tested it all works well.

So the answer is you can test a battery supply for the Picaxe.

Hippy,

The LED and resistor would work OK for the reference voltage IF I was not trying to get MAXIMUM battery life out of this design. The little LM336-2.5 just sips current as it really is not sourcing any current. The project should run reliably for 6 months on 3 C cells, maybe more.

I am getting older but this little Picaxe is fun and helping to shake off the old cob webs, this board and support is what makes it SO good.

Thanks all,

PFM
 

lbenson

Senior Member
PFM,

I misunderstood the part you were using, and thought it was a voltage regulator, not a voltage reference diode. Thanks for introducing me to a new part.
 

PFM

New Member
IBenson

No problem, your help was worth more than that. All is working well now. Tonight I add some code to make sure the DS1337 has not lost it's mind and this one is about ready for serious testing.

Regards,

PFM
 
Top