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

Post Reply
picolino
Posts: 9
Joined: Sun Feb 16, 2020 7:38 am
Contact:

Measure PIC's own battery voltage

Post by picolino »

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 ADC component conected to ADC GP1.
Image

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

Image

...

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
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

Post by stefan.erni »

Hi picolino

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


Can you post your program?
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: 9
Joined: Sun Feb 16, 2020 7:38 am
Contact:

Re: Measure PIC's own battery voltage

Post by picolino »

Hi Stefan.

Program is simply like this.

Image

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
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

Post by stefan.erni »

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.
read in the manual:

6.5 Comparator Reference


regards

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

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

Re: Measure PIC's own battery voltage

Post by picolino »

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.

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

Re: Measure PIC's own battery voltage

Post by Bachman »

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
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

Post by EtsDriver »

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: 9
Joined: Sun Feb 16, 2020 7:38 am
Contact:

Re: Measure PIC's own battery voltage

Post by picolino »

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?
Image
Image

Thanks!!

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

Re: Measure PIC's own battery voltage

Post by Steve »

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.

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

Re: Measure PIC's own battery voltage

Post by Bachman »

Today I've learned a new trick. :)

Setup the ADC:
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
Vref_requirements.png (36.18 KiB) Viewed 2867 times
Table.png
Table.png (170.41 KiB) Viewed 2867 times

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

Re: Measure PIC's own battery voltage

Post by siliconchip »

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

Bob

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

Re: Measure PIC's own battery voltage

Post by Bachman »

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
Photo.jpg (54.94 KiB) Viewed 1808 times
Attachments
PIC18F14K22_OwnVoltage_report.zip
(20.04 KiB) Downloaded 24 times
PIC18F14K22_OwnVoltage.fcfx
(5.12 KiB) Downloaded 25 times

User avatar
AbhijitR
Posts: 207
Joined: Fri Nov 07, 2014 12:48 pm
Location: Pune, India
Has thanked: 209 times
Been thanked: 50 times
Contact:

Re: Measure PIC's own battery voltage

Post by AbhijitR »

Hello! Bachman

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

Abhi

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

Re: Measure PIC's own battery voltage

Post by picolino »

Steve wrote:
Tue Jan 19, 2021 11:28 am

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/
I tryed with trial version of Flowcode 9, and I can't see difference for this particular problem...

So, supossed I use Flowcode 9 (my trial still active), Have this version a comparator? I didn't see. Thanks.

Apart. I think I need a basic guide of how to connect the pins and selects the ADC in Flowcode, I searched but wihout luck.

I have a mini pump charge (arrived after 3 months), so I have 5 volts stable, but with the same problem, consume a lot, so I think I can enable - have 5v stable - take the samlple of the battery - disable... In deed If I enable and disable this pump charge, the consume is pretty decent. But my problem is, if I connect this 5V stable in Pin6 (Vref), how I tell to Flowclode to know that in this pin I have a Vref? I suppose, then in pin 7 I can connect one ADC to measure the battery... I be a little bit confuse in how to tell to Flowcode asume this stable refference in Pin6 Vref and make the measure.

Thanks.

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

Re: Measure PIC's own battery voltage

Post by Steve »

In v9, add a potentiometer to the panel and select it. In the component properties, "FVR" will be one of the choices under "Vref options".

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

Re: Measure PIC's own battery voltage

Post by picolino »

Steve wrote:
Tue May 11, 2021 9:16 am
In v9, add a potentiometer to the panel and select it. In the component properties, "FVR" will be one of the choices under "Vref options".
Thanks Steve.

I use a 12F675 that have a Vref in pin 6 (AN1), but in Vref option didn't see that FVR. Any suggestion?

Image

I can use sample on VDD whith no problem, but in this case of voltage reference I have no clue, thanks in advance.

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

Re: Measure PIC's own battery voltage

Post by Steve »

Looking at the datasheet, that device does not have an internal FVR that can be used for analogue inputs. It has an internal comparator reference, but I'm not sure if that is supported in Flowcode (but you can always use C code if you need it).

More modern chips (like the 12F1840) do have an internal FVR.

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

Re: Measure PIC's own battery voltage

Post by picolino »

Steve wrote:
Wed May 12, 2021 9:34 am
Looking at the datasheet, that device does not have an internal FVR that can be used for analogue inputs. It has an internal comparator reference, but I'm not sure if that is supported in Flowcode (but you can always use C code if you need it).

More modern chips (like the 12F1840) do have an internal FVR.
Ok, so, 12F629 can't measure voltage with reference voltage using Flowcode without C code. No matter what version os Flowcode use. Is correct? Thanks.

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

Re: Measure PIC's own battery voltage

Post by Steve »

For that device, you can use an external voltage reference for measuring voltages without C code.

Using the chip's comparator is relatively easy in C code - you should only need a couple of lines to setup the CMCON and VRCON appropriately, and then another line to read the COUT value from CMCON. All the rest can be done using flowchart icons.

The values of the registers depends on your requirements, but CMCON=0x04 sets it to compare GP1 with the internal reference (which is set by VRCON). Read COUT by setting a variable to (CMCON & 0x40).

Post Reply