## Measure PIC's own battery voltage

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

picolino
Posts: 6
Joined: Sun Feb 16, 2020 7:38 am
Contact:

### Measure PIC's own battery voltage

Hi.

I tryed a lot of things but with no luck. I using Flowcode V5

I have a pic 12F675 powered by a single 4.2v lithium battery.

I need to measure voltage of this battery to change 3 states of a simple led:

If voltage is over 3.7v (read as a byte, for example 227) led is on.
If voltage is minor to 3.69v (read as a byte, for example 226) led is blinking.
If voltage is minor of 3.2v (read as a byte, for example 200) led blinking faster.

I use a voltage divider to take the sample of battery by Pin 6 (GP1/Vref) using a 5k resistor.

I use a simple component to call ADC and set variable as a byte.
Then I evaluated this variable to make I want.

...

But not work. In deed never I can make work this using single battery to feed pic and evalauating same battery.

Some idea? Thanks a lot.

stefan.erni
Valued Contributor
Posts: 650
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 178 times
Contact:

### Re: Measure PIC's own battery voltage

Hi picolino

That's no problem. I've done this many times.

I have a pic 12F675 powered by a single 4.2v lithium battery.
Is your pic on the battery or do you have a 3.3volt psu additionally ?
I use a voltage divider to take the sample of battery by Pin 6 (GP1/Vref) using a 5k resistor.
Maybe you mean two 5k resistors

regards

Stefan

picolino
Posts: 6
Joined: Sun Feb 16, 2020 7:38 am
Contact:

### Re: Measure PIC's own battery voltage

Hi Stefan.

Program is simply like this.

Pic (and led) is powered direct by battery.

I used a voltage divider to get a different value of battery and not get always 1...

R1 39k and R2 47k to obtain 2.26V from Battery at 4.15v (as you can see my Vref voltage is 226 x10mV)

From pic pin 6 (GP1/Vref) I take the sample of this voltage divider with a 5k resistor.

I hope this help.

Thanks.

stefan.erni
Valued Contributor
Posts: 650
Joined: Fri Aug 19, 2016 2:09 pm
Location: switzerland
Has thanked: 182 times
Been thanked: 178 times
Contact:

### Re: Measure PIC's own battery voltage

Hi picolino

You are using Vref as reference Voltage on pin 6
and which pin is for the AD input?

You need a stable voltage for vref (not dependent on the battery) that can be done with a small Ic with 3Pin, or you use a regulator for the PIC for 3.3volt and use VDD as Vref.

or to test the battery you can use the included comparator but I think flowcode don't support it.

6.5 Comparator Reference

regards

Stefan
PIC_Vref_2021-01-07_11-24-58.png (16.66 KiB) Viewed 2504 times
PIC_AD_2021-01-07_11-24-58.png (33.52 KiB) Viewed 2504 times

picolino
Posts: 6
Joined: Sun Feb 16, 2020 7:38 am
Contact:

### Re: Measure PIC's own battery voltage

Ok, so, first of all, is needed one fixed voltage equal or below 5V in order to this work, is correct? Without one fixed voltage, is impossible made measure. Maybe (and simply [In my case is not critical the accurate of measure]) with one zener...

Thanks.

Bachman
Posts: 87
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 8 times
Been thanked: 37 times
Contact:

### Re: Measure PIC's own battery voltage

Yes, you need a fixed reference. It can be external or if you choose another controller (eg. PIC12F1840), can be internal.

I don't recommend zener diode as a voltage reference, it's not stable.

EtsDriver
Valued Contributor
Posts: 442
Joined: Tue Apr 15, 2014 4:19 pm
Location: Kajaani, Finland
Has thanked: 345 times
Been thanked: 227 times
Contact:

### Re: Measure PIC's own battery voltage

I've used the LM4120 voltage reference to have stable measurement reference on some situations when i need good reference for 2-12V input to be sure that it is running ok...

The external reference is good, as the PIC's internal voltage might drop when the regulator starts to go down with input voltage, if using any.
Ill just keep the good work up!

picolino
Posts: 6
Joined: Sun Feb 16, 2020 7:38 am
Contact:

### Re: Measure PIC's own battery voltage

Bachman wrote:
Fri Jan 08, 2021 8:31 am
I don't recommend zener diode as a voltage reference, it's not stable.
Hi.

I made a test with zenner and really is not an option. I need to use a maximum of battery (4.2V to 3V), zenner can't fix voltage in this ranges. I decided to use a charge-pump LTC3200ES6-5 to obtain 5V and use Option VRef VDD (I just wait to arrived to make a test). In deed I tryed used a Powerbank circuit to obtain 5V, but performance is less than 50%, drain battery a lot

Bachman wrote:
Fri Jan 08, 2021 8:31 am
It can be external or if you choose another controller (eg. PIC12F1840), can be internal.
Oh, it's a good option this 12F1840. But how can I enable it in Flowcode? I still seeing just 2 options, VDD and VRef, this 12F1840 have 3 options, how I can enable FVR in Flowcode?

Thanks!!

Steve
Matrix Staff
Posts: 3302
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 109 times
Been thanked: 418 times
Contact:

### Re: Measure PIC's own battery voltage

picolino wrote:
Tue Jan 19, 2021 12:16 am
But how can I enable it in Flowcode? I still seeing just 2 options, VDD and VRef, this 12F1840 have 3 options, how I can enable FVR in Flowcode?
We no longer support v5 of Flowcode. FVR is supported in Flowcode v9 for the 12F1840, and there are many other benefits from using the latest version. More info is here: https://www.flowcode.co.uk/

If you need to stay with Flowcode v5, then I think you will need to pay attention to the device datasheet and create custom C code for the ADC component, but that's something I can't help you with.

Bachman
Posts: 87
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 8 times
Been thanked: 37 times
Contact:

### Re: Measure PIC's own battery voltage

Today I've learned a new trick.

Vref+: Vdd (AVdd)
Vref-: Vss (AVss)

Setup FVR to 1.024 V

Measure FVR voltage by the ADC module. Because FVR is fixed, you can calculate the PIC's own voltage. Tested with PIC18F14K22, surprisingly accurate. Important! With this PIC, using 1.024 or 2.048 V internal Vref, Vdd can't be lower than 2.5 V, see datasheet! As you can see the attached pictures, the results are not linear and the internal Vref can be drift, but far good enough to detect low voltage if you're using a fixed value to compare the ADC results.
Vref_requirements.png (36.18 KiB) Viewed 1836 times
Table.png (170.41 KiB) Viewed 1836 times

siliconchip
Posts: 303
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 82 times
Been thanked: 22 times
Contact:

### Re: Measure PIC's own battery voltage

Hi bachman
Can you go into more detail how you set the FVR im having issues with it

Bob

Bachman
Posts: 87
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 8 times
Been thanked: 37 times
Contact:

### Re: Measure PIC's own battery voltage

Sorry guys, forgot the example. Here it is, tested on real HW. Flowchart written in Flowcode v6 (I don't have newer), but you can find the required C codes in the report file.
Photo.jpg (54.94 KiB) Viewed 777 times
Attachments
PIC18F14K22_OwnVoltage_report.zip
PIC18F14K22_OwnVoltage.fcfx

AbhijitR
Posts: 205
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 204 times
Been thanked: 50 times
Contact:

### Re: Measure PIC's own battery voltage

Hello! Bachman

Happy to hear you, thanks for this post, I will try that.

Abhi