Measure PIC's own battery voltage
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 ADC component conected to ADC GP1.
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.
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.
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.
-
- Valued Contributor
- Posts: 654
- Joined: Fri Aug 19, 2016 2:09 pm
- Location: switzerland
- Has thanked: 182 times
- Been thanked: 179 times
- Contact:
Re: Measure PIC's own battery voltage
Hi picolino
That's no problem. I've done this many times.
Can you post your program?
regards
Stefan
That's no problem. I've done this many times.
Can you post your program?
Is your pic on the battery or do you have a 3.3volt psu additionally ?I have a pic 12F675 powered by a single 4.2v lithium battery.
Maybe you mean two 5k resistorsI use a voltage divider to take the sample of battery by Pin 6 (GP1/Vref) using a 5k resistor.
regards
Stefan
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.
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.
-
- Valued Contributor
- Posts: 654
- Joined: Fri Aug 19, 2016 2:09 pm
- Location: switzerland
- Has thanked: 182 times
- Been thanked: 179 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.
read in the manual:
6.5 Comparator Reference
regards
Stefan
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
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.
Thanks.
- Bachman
- Posts: 116
- Joined: Sun Sep 07, 2014 11:37 am
- Location: Hungary
- Has thanked: 9 times
- Been thanked: 53 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.
I don't recommend zener diode as a voltage reference, it's not stable.
-
- Posts: 444
- 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.
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!
Re: Measure PIC's own battery voltage
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
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: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Measure PIC's own battery voltage
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: 116
- Joined: Sun Sep 07, 2014 11:37 am
- Location: Hungary
- Has thanked: 9 times
- Been thanked: 53 times
- Contact:
Re: Measure PIC's own battery voltage
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.
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.
-
- Posts: 392
- Joined: Wed Jan 05, 2011 11:24 am
- Has thanked: 101 times
- Been thanked: 24 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
Can you go into more detail how you set the FVR im having issues with it
Bob
- Bachman
- Posts: 116
- Joined: Sun Sep 07, 2014 11:37 am
- Location: Hungary
- Has thanked: 9 times
- Been thanked: 53 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.
- Attachments
-
- PIC18F14K22_OwnVoltage_report.zip
- (20.04 KiB) Downloaded 166 times
-
- PIC18F14K22_OwnVoltage.fcfx
- (5.12 KiB) Downloaded 170 times
- AbhijitR
- Posts: 300
- Joined: Fri Nov 07, 2014 12:48 pm
- Location: Pune, India
- Has thanked: 283 times
- Been thanked: 80 times
- Contact:
Re: Measure PIC's own battery voltage
Hello! Bachman
Happy to hear you, thanks for this post, I will try that.
Abhi
Happy to hear you, thanks for this post, I will try that.
Abhi
Re: Measure PIC's own battery voltage
I tryed with trial version of Flowcode 9, and I can't see difference for this particular problem...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/
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.
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Measure PIC's own battery voltage
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".
Re: Measure PIC's own battery voltage
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?
I can use sample on VDD whith no problem, but in this case of voltage reference I have no clue, thanks in advance.
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Measure PIC's own battery voltage
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.
More modern chips (like the 12F1840) do have an internal FVR.
Re: Measure PIC's own battery voltage
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.Steve wrote: ↑Wed May 12, 2021 9:34 amLooking 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.
- Steve
- Matrix Staff
- Posts: 3424
- Joined: Tue Jan 03, 2006 3:59 pm
- Has thanked: 114 times
- Been thanked: 422 times
- Contact:
Re: Measure PIC's own battery voltage
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).
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).