# Read self battery voltage using PIC microcontroller [duplicate]

I'm working on a battery powered project where I have to constantly monitor the remaining battery level of the battery that is powering the mcu. I know it can be done using arduino and its internal vref. Now, The PIC18F26J50 I'm using also consists of a Band Gap Reference(vref). But I Don't know how to proceed or it's even possible using PIC. Is there a way to monitor the battery level in PIC using this method (or another method)?

## marked as duplicate by brhans, Dmitry Grigoryev, SamGibson, laptop2d, ThreePhaseEelJan 19 '17 at 4:29

This part has a internal band-gap voltage reference that the A/D can read directly. See section 20.0 on page 341, the CHS field in the ADCON0 register. By using AVdd and AVss as the reference voltages and reading the 1.2 V bandgap voltage, you can calculate what the AVdd reference actually was.

For example, if you get a reading of 365 that you know corresponds to 1.2 V, then you know that the full scale reading of 1023 would be 3.36 V, which is the AVdd voltage.

It takes a divide to actually compute the AVdd voltage from the internal reference reading, but often you don't actually need to do that. For example, you can pre-compute the readings that corresponds to battery low, battery really really low, etc.

• Oh duh. It's so obvious once someone else shows it. Something to keep in mind is the band gap reference on the PIC18 apparently has a tolerance of 10%. So your 1.2V could be between 1.08V and 1.32V. Depending on how accuractly VDD needs to be measured, a discrete voltage reference might be the preferred approach. microchip.com/forums/m460921.aspx – CHendrix Jan 18 '17 at 13:17

As @CHendrix helpfully notes, the particular chip you are using does not have an internal reference of guaranteed accuracy (the 10% mentioned is anecdotal, from talking to the factory, and not on the datasheet- though 'likely' conservative), so I would suggest using an external reference with accuracy limits specified.

You have the option of providing the external reference to the ADC (in which case your measurements are straightforward- just measure the divided battery voltage and multiply by a constant) or you could provide a (perhaps switched) external reference and measure that voltage, and calculate the battery voltage from that as Olin suggests.

The cheapest references available are shunt 2.5V references such as the ubiquitous TL431 (about a penny in volume and accurate to about 1% depending on grade) but they consume ~1mA. There are many, many other options (do a parametric search for series and shunt references).