Maker Pro
Maker Pro

Measuring CR123A battery using PIC A/D

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi all
I have a CR123A 3 V battery. I am trying to monitor the condition of this using a PIC 18F45k20 A/D.
This is my approach.
1. Use the Vdd and Vss of the battery as my PIC reference. This will be used to measure off load battery levels.
2. I then intend to use a voltage divider 30K top resistor and 10K bottom resistor. The centre pin to the A/D of the PIC.
This will measure the on load level.
3. Then i subtract the off load and on load levels to give me a threshold.

My question is when i use the voltage divider can i TRIState the PIC I/O line connected to the. 30K resistor so the circuit draws no current? If so is this the same as making this pin an input??? Or is there a special command for tristate?

Also how often should i check this snd how do i set an interrupt to check the A/D every so often say once every hour. I will be running at 4MHz clock frequency.

Thanks in advance.
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
If the battery is used as the reference and you are measuring the battery with the ADC, you will always get the same reading (1023 assuming 10-bit ADC)

Bob
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi Bob
Yes i thought off load i will get the same value, which is what i want.
But on load it will be different as i have a potential divider circuit.
This will give me a delta. Which as long as it is in a certain range, i know the battery is good.
Does that seem correct?
Or is there a better nethod?

I need the check as it is a PIC based project which needs to be portable hence 3V battery power. I need to conserve power and also write to EEPROM. I want to make sure there is sufficient battery before writing to EEPROM.
thanks
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
You will get a different value with divider , but it will be based only on the ratio of the resistors in the divider. What you are doing is like trying to measure the accuracy of a ruler by measuring it with itself. You need another ruler.

If your PIC has a votage reference built in, you can calculate the battery voltage by using the battery as the reference for the ADC and measuring the voltage reference output. For example, suppose the volage reference is 2.0V, and, you read 900 counts. You now know that the battery voltage is:

2.0 x 1024 / 900 = 2.275V.

Bob
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi Bob,
Thanks for your help. The PIC does have an internal VREF.
I am slightly puzzled, if I use the battery as a reference for off-load calculations.

Are you saying that I need to then re-configure to use the internal VREF (after the off-load test). Then use the A/D value for the off-load conditions i.e. say it was 900 (as per your example).

Then with a VREF of 2.0V,, it would be 2.0x1024/900 = 2.275V.
So really I don not need a on-load measurement? i.e. no voltage divider? Is that correct?
 

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
No, you use the battery as the reference, and you measure the internal voltage reference. If you used the internal reference for the ADC reference the battery would read off scale, since it is higher than the internal reference. On at least some PICs, you can select the internal reference as the input to the ADC without having to put it out on one pin and read it another.

Look at it this way. We have an unknown voltage Vbat. We also have a known voltage Vref, which will have to lower than Vbat since there is no boost converter inside the chip. How can we determine Vbat?

So, when Vbat is used as the reference to the for the ADC, the formula for a voltage that you are reading is:

V = Vbat * count / 1024

where count is the count returned by the ADC.

So if we put Vref in as the voltage read, we get:

Vref = Vbat * count / 1024

solving for Vbat we get

Vbat = Vref * 1024 / count

Since Vref is a known voltage (probably 2.048V) we can calculate Vbat by reading Vref and applying the above equation.

You could also use the voltage divider and use the internal reference as the ADC reference. Using a 1:1 voltage divider you would get:

Vbat / 2 = Vref * count / 1024

As far as measuring under load, your voltage divider does not supply much of a load, the PIC itself would be more of a load. If you want to measure it under load, measure it when the PIC has the highest load, for instance, it it is lighting LEDs, measure while they are on.

Bob
 

Rajinder

Jan 30, 2016
568
Joined
Jan 30, 2016
Messages
568
Hi Bob
Many thanks for the excellent explanation and taking time to answer my questions.
I really do appreciate your help.
I clearly understand this now.
Best regards,
Raj
 
Top