Reading a 9V battery on a PIC is nothing new. The most simple and straightforward way is with a voltage divider. I'd like to keep the divider resistances cranked up into the hundreds of kohms so I'm not killing my battery unnecessarily, however I looked at the datasheet for the PIC18F4550 and the max "recommended" analog input impedance is 2.5kohms. This means I can stick 2 5Ks for my divider, but 900uA is a lot to waste on just checking the battery. What can I do to my design (passively) to minimize battery drain? I've considered active solutions such as a software controlled pfet or a buffer, but board space and budget are a bit of a luxury, so I'll only do it if I have to. I'm also wondering if I'm I being concerned over nothing.
The reason the ADC needs a low source impedance is because it has a switched capacitor input. Basically, whenever the ADC 'samples' the voltage on the pin, a small capacitor is connected, charged up, and then disconnected. If the impedance is too large, charging the capacitor up will draw enough current to create a voltage drop large enough to affect the reading.
If you need to read a high speed signal, the best option is to add an amplifier of some sort to provide a low source impedance to the ADC. However, if you are looking at a relatively slow signal there are a couple of other options.
One solution to this is to increase the sample time - the length of time the capacitor is connected to the pin. The chip usually has a limit on how long this time can be, though.
Alternatively, you can add a decent sized capacitor in parallel with the ADC input pin. This will decrease the amount of droop that occurs when the ADC sampling capacitor charges up as most of its charge will be drawn from the capacitor instead of through the resistor.
There are about 4 ways of connecting a voltage divider to an A/D and dealing with max input impedance requirement.
- Use small enough resistor. This is what the O.P. is already doing.
- Put an OpAmp buffer between the divider and A/D input. OpAmp should have high input impedance and low output impedance. [As already mentioned by Alex.]
- Use a larger resistor and add a capacitor from the analog input to ground. [As already mentioned by Alex.] The capacitor should be mush larger than the one in sample and hold. You will be inadvertently making an RC filter, but this still works if the signal is slow. A combination of 10kΩ and 0.1μF worked well for me.
- [last but not least] Switch off the voltage divider with a MOSFET switch, and use relatively small resistors. This was you can eliminate the leakage pretty much completely when you are not measuring. This is a common technique for battery measurement.
Replace R1 and R2 with the values you need. The schematic was originally posted in this thread.
Bitrex's idea would work if the PIC digital pin was configured as "open drain" then clamped to 2.7V with a zener to protect it from the 9V.
To turn it "ON" and "OFF" initialize the pin by writing a logic low to it (and leave it there) then turn the pin "ON" and "OFF" by writing to the TRIS latch which will cause the pin to be either a logic low or high z respectively.
The pin will switch from 0 to 2.7V which should be enough to drive a low gate threshold MOSFET.