Reading ADC when the VDD is not fixed - Page 1
Forum section: electronics / general

author: arslan43711
author_rank: Contributor
timestamp: October 30, 2020, 05:55:55 am
content: Hi, Hopefully you are doing good.I am working on a project in which I have to measure the battery voltage(fully charged 5V and discharged voltage are 2.5V), I am using pic16f688. I have connected the battery pin to one of the analog pin. So ADC read 1023 all the time whatever the voltage is. I mean when the VDD is 3.3v it gives 1023 and when it 5v it gives 1023. I know there is no fixed voltage reference in the pic16f688.Is there any solution to this? or a way with which I can measure the battery voltage while the PIC is connected to the same battery.

author: JPortici
author_rank: Super Contributor
timestamp: October 30, 2020, 07:21:23 am
content: Then you need an external voltage reference, measure it and find out the value of VDD

author: hans
author_rank: Super Contributor
timestamp: October 30, 2020, 08:10:37 am
content: Connect VDD through a resistor to a TLV431 placed backwards as reference. Then measure this reference voltage with your VDD. Something like:Then a lower ADC code indicates a higher supply voltage. You would need to see how you'd want to do the math on that PIC processor, or perhaps calculate the 8-bit ADC thresholds manually on which a certain low-battery situation is warranted.

author: Yansi
author_rank: Super Contributor
timestamp: October 30, 2020, 08:13:08 am
content: Quote from: hans on October 30, 2020, 08:10:37 amRemove the 100nF capacitorimmediately. Unless what you intended was an oscillator.

author: MosherIV
author_rank: Super Contributor
timestamp: October 30, 2020, 08:38:55 am
content: Pick a better microcontroller with built in battery monitor.

author: arslan43711
author_rank: Contributor
timestamp: October 30, 2020, 08:55:41 am
content: I am using a 1F super cap as a battery, Micro will be mostly in sleep mode(20uA current). I think the current of this reference circuit will more than my sleep current.Anyhow maybe I will select another micro.

author: Zero999
author_rank: Super Contributor
timestamp: October 30, 2020, 09:05:56 am
content: Quote from: Yansi on October 30, 2020, 08:13:08 amThe post says TLV431, yet the schematic shows TL431. It needs to be the TLV431 and the datasheet suggests it will be stable with a 100nF capacitor and an output voltage of 1.24V. Although I agree, it's not necessary.https://www.ti.com/lit/ds/symlink/tlv431a.pdfI suggest a lower current. The TLV431 will work down to 80µA. R10 could easilly be 15k. If improved supply rejection is required, which I doubt, as it's a battery operated device, use two 7k5 resistors in series, for R10, with the tap connected to 0V, via a 10µF capacitor.

author: Yansi
author_rank: Super Contributor
timestamp: October 30, 2020, 09:35:15 am
content: I do not think the improved rejection is needed for the ADC in a PIC.Why doesn't the PIC have an internal reference?//I'd definitely swap for a different MCU type, likely even from different vendor, that has an adequate low power internal reference implemented//.20uA sleep current is quite a lot and you won't get much lifetime from a 1F supercap. They are as expensive as a coin cell battery, but the battery has couple orders of magnitude more capacity.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: October 30, 2020, 09:49:43 am
content: Quote from: arslan43711 on October 30, 2020, 08:55:41 amPower the reference from an I/O pin, so you only turn it on briefly to measure the battery voltage.there are some PICs with internal references

author: fcb
author_rank: Super Contributor
timestamp: October 30, 2020, 10:23:30 am
content: Caution if you use a capacitor across the TL431. We've found that certain brands (can't remember which) will oscillate when they have a capacitor across them. Any noise from the TL431 is going to be well under an LSB (10/12bit).Also, if you follow Mikes suggestion - definatley don't put a capacitor across the TL431 - it will slow up your reference and require more power. Also, while the datasheet specifies 1mA through a typical TL431 - they will work well with lower currents. YMMV.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: October 30, 2020, 10:25:10 am
content: For battery detection, a simple diode would probably be fine as a reference

author: Yansi
author_rank: Super Contributor
timestamp: October 30, 2020, 10:27:49 am
content: Quote from: fcb on October 30, 2020, 10:23:30 amIf the datasheet specifies 1 mA minimum current, they need 1 mA minimum current and will NOT work under 1 mA. They may, but as well may not. It is stupid idea to build designs like this, when there are legitimate low current "431" based references, that operate well under 100 uA.Stability regions with parallel capacitive loads are also defined in every datasheet. No need to "find certain brands" by experimenting.

author: Mechatrommer
author_rank: Super Contributor
timestamp: October 30, 2020, 11:41:48 am
content: use 2.5V regulator such as jelly bean AMS1117-2.5V (or buck) to power mcu and use voltage divider to sense battery voltage, you'll save one (Vref input) mcu pin, and mcu will use lower power.

author: Yansi
author_rank: Super Contributor
timestamp: October 30, 2020, 12:22:19 pm
content: Quote from: Mechatrommer on October 30, 2020, 11:41:48 amAnd drain the battery by the quiescent current consumption of the LDO. Let alone the switcher.

author: wraper
author_rank: Unknown
timestamp: October 30, 2020, 12:34:40 pm
content: Quote from: Yansi on October 30, 2020, 12:22:19 pmJust use some LDO with low questient current or voltage reference IC like MAX6120. You can power it from MCU I/O pin only when actually making measurement to reduce consumption. There are no other way than using external reference without changing MCU.

author: Yansi
author_rank: Super Contributor
timestamp: October 30, 2020, 12:53:46 pm
content: I think that changing the MCU would likely be the wisest choice here.Switching an external reference on/off using a GPIO pin would be the second best one could do to keep the MCU type.

author: arslan43711
author_rank: Contributor
timestamp: October 30, 2020, 12:56:35 pm
content: I have decided to change the micro to pic16f18324 it has the same pinout so no need to change the PCB and PPS for peripherals. And it has inter voltage reference.

author: fcb
author_rank: Super Contributor
timestamp: October 30, 2020, 01:41:59 pm
content: Quote from: Yansi on October 30, 2020, 10:27:49 amDisagree that it's "stupid" - low current 431's are rarer, more expensive - the NCP431 (Farnell: 2534159) is around $0.10 in 100's, a generic 431 is around $0.014 (LCSC: C181103).Whilst low current 431's can be made to use 40uA, the dutycycle required in the OP's use case is likely to be so low that it's contribution to overall current consumption of even a 1mA TL431 would be measured in a few nA. The 100nF suggested is very much in the non-stable zone (ti datasheet: SLVS543P, fig18), 4.7nF or less, or 10uF or more. But I would argue that it isn't necessary with 10/12 bit converters.I wasn't suggesting that someone tries different brands of TL431 - what circuit might work with brand of TL431 might not work with another - I have very real experience with this.

author: JPortici
author_rank: Super Contributor
timestamp: October 30, 2020, 03:17:19 pm
content: Quote from: Yansi on October 30, 2020, 09:35:15 ambecause the 16F688 is ancient (released in 2003 or maybe earlier)luckily he was able to get a newer part (luckily relative to the fact that the OP is from pakistan)

author: wraper
author_rank: Unknown
timestamp: October 30, 2020, 03:31:20 pm
content: Quote from: Zero999 on October 30, 2020, 09:05:56 amSorry but for both Tl431 and TLV431 0.1 uf is in unstable zone for any current with VKA= VREF.

author: Zero999
author_rank: Super Contributor
timestamp: October 30, 2020, 03:37:41 pm
content: Quote from: wraper on October 30, 2020, 03:31:20 pmYou're right. I missread the graph on the data sheet.Quote from: arslan43711 on October 30, 2020, 12:56:35 pmThat's definitely the right decision. It's normally more cost effective to use a better MCU with the features you require built-in, than to add them afterwards.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: October 30, 2020, 04:12:27 pm
content: 20uA is a lot for sleep mode - I don't recall if yours is one with the low-power WDT, but you should be looking at more like 2-3uA unless you have something else drawing current

author: Doctorandus_P
author_rank: Super Contributor
timestamp: October 31, 2020, 12:12:31 am
content: Even 2uA sleep mode is a lot for "modern" microcontrollers.Just did a quick search for:https://html.duckduckgo.com/html?q=ultra+low+power+microcontrollerAnd within a few minutes I found:https://www.st.com/content/st_com/en/products/microcontrollers-microprocessors/stm32-32-bit-arm-cortex-mcus/stm32-ultra-low-power-mcus/stm32l5-series.htmlWhich claims:17 nA in shutdown mode3 µA in stop mode with full SRAM and peripheral states retention with 5µs wake-up timeDown to 62 µA/MHz in active modeThat is a difference of 2 orders of magnitude!But when chasing the low current ghost you have to consider the whole design. Quiescent current of a voltage regulator or even just a leaky elco can completely dominate current consumption.

author: rstofer
author_rank: Super Contributor
timestamp: October 31, 2020, 12:47:41 am
content: Table 14-8 Parameter AD08 has a requirement that the source resistance, as seen by the ADC, be, at most, 10k Ohms.http://ww1.microchip.com/downloads/en/devicedoc/41203d.pdfThe Comparators have an internal Vref that is ratiometric with Vcc. I haven't thought through what this means but you might be able to get just a "Good"/"Low" indication.

author: nctnico
author_rank: Super Contributor
timestamp: October 31, 2020, 01:32:59 am
content: Quote from: arslan43711 on October 30, 2020, 08:55:41 amUse a low power reference instead of an ancient one. For example the REF3025. Power it from an I/O pin when needed but keep the turn-on delay in time in mind. But as others noted: the PIC you selected has a very high power consumption.

author: Dabbot
author_rank: Regular Contributor
timestamp: October 31, 2020, 01:35:16 am
content: Quote from: rstofer on October 31, 2020, 12:47:41 amIt's just an internal resistor ladder between VDD and ground. See page 64 of the PDF you linked. It's not an independent reference.

author: rstofer
author_rank: Super Contributor
timestamp: October 31, 2020, 02:13:25 am
content: Quote from: Dabbot on October 31, 2020, 01:35:16 amAnd that's why they call it 'ratiometric'.

author: wraper
author_rank: Unknown
timestamp: October 31, 2020, 02:28:33 am
content: Quote from: rstofer on October 31, 2020, 02:13:25 amAnd that's why it will result in the same reading regardless of voltage if you try to measure Vdd. What's the point of making ratiometric measurement of voltage which is used as ratiometric reference of ADC? Their ratio will always remain the same regardless of Vdd.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: October 31, 2020, 11:26:06 am
content: The PIC16F153xx for example has an internal reference, programmable to 1.024,2.048 or 4.096V.Sleep current typ 60nA with everything off, 0.9uA with a wake-up timer running

author: arslan43711
author_rank: Contributor
timestamp: October 31, 2020, 05:55:47 pm
content: The reason for using this micro is availability in the local market.

author: IDEngineer
author_rank: Super Contributor
timestamp: November 01, 2020, 04:59:12 pm
content: Be careful relying on the "voltage references" in MCU's. They generally have decent repeatability in the same device, but can have poor absolute accuracy from device to device.I researched this rather deeply a while back and the final official word from the manufacturer (in this case Microchip, for their PIC series) was +/-7% voltage accuracy. (You get better than that from vanilla voltage regulators, and binned regulators can get under 1%.) There are some more recent units in which they are laser trimming the voltage reference so one might expect better per-device accuracy. Interestingly, they also write the correction they applied into a nonvolatile, readable area on-chip... not sure how I'd use that information unless I was trying to bin "near-process" parts for some reason.

author: Yansi
author_rank: Super Contributor
timestamp: November 01, 2020, 05:00:59 pm
content: Digital calibration is easy, so why bothering to implement a reference with good absolute accuraccy?

author: wraper
author_rank: Unknown
timestamp: November 01, 2020, 05:43:58 pm
content: Quote from: Yansi on November 01, 2020, 05:00:59 pmTo avoid another manufacturing step. There are $0.30 MCUs with +/- 1.8% reference in entire operating temperature and voltage range.https://eu.mouser.com/ProductDetail/Silicon-Labs/EFM8BB10F2I-A-QFN20?qs=G1ssh2j%252Bqz4q0w05PnPgQQ%3D%3D