Microcontroller running 3V3 sense 9V battery voltage with ADC - Page 1
Forum section: electronics / projects

author: delmadord
author_rank: Regular Contributor
timestamp: February 28, 2014, 06:28:07 pm
content: Greetings,I wold like to sense a 9V battery voltage on a microcontroller (ATmega328) ADC. I have found a guidehttp://www.w9xt.com/page_microdesign_pt15_battery_ad.htmlhowever I am quite suspicious about it. The idea in the article is that, you set P0 low to start measurement, and after it set it HIGH again so the resistor divider is not draining battery. I am planning on using 10k and 5.4k resistor voltage divider. What concerns me is that when the P0 is high (3V3), ADIN pin is almost on battery potential. Also, some hundreds uA flows into the P0 pin. I was thinking about using P-channel MOSFET (circuit in the attachment) to switch ON the voltage divider for battery voltage measurement only for occasional measurement. Will this approach of putting two more resistors and two more transistors in give me some real advantages like protecting MCU from higher voltage, if it can make some harm in this case or less current drained from battery? Should I just use the circuit shown in that article? Thanks!

author: PA0PBZ
author_rank: Super Contributor
timestamp: February 28, 2014, 06:56:09 pm
content: Are you running the microcontroller from the same battery? If so the negative from the battery will already be connected to ground and your circuit makes no sense.If I read the datasheet correctly the input impedance of the ad converter is somewhere around 100M, so you can make a divider with pretty large resistors, minimizing the current flowing trough them.

author: georges80
author_rank: Frequent Contributor
timestamp: February 28, 2014, 07:16:23 pm
content: I do voltage sensing on AVR's all the time. I use a resistor dividing in the 400k range, low quiescent battery drain. The battery supplies the regulator that provides 2.5V or 3V (depending on the design) to the AVR. You could increase the resistance even more - the AVR A/D inputs are quite high impedance... all depends on what accuracy you are after and what quiescent current consumption you can live with.cheers,george.

author: delmadord
author_rank: Regular Contributor
timestamp: February 28, 2014, 07:26:12 pm
content: ATmega328 datasheet:http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet.pdfPart:24.6.1 Analog Input CircuitryQuoteWhat exactly does this mean then?Also I made correction in a circuit.

author: PA0PBZ
author_rank: Super Contributor
timestamp: February 28, 2014, 07:59:33 pm
content: That works, only change R2 to 100K, that will drop the total current from 1.3 mA to 500 uA.

author: Andreas
author_rank: Super Contributor
timestamp: February 28, 2014, 08:31:36 pm
content: Quote from: PA0PBZ on February 28, 2014, 06:56:09 pmHello,This is only true for room temperature.At higher temperatures the leakage current limits your accuracy significantly.1uA * 10kOhms = 10mV error voltage referred to the ADC-Pin.There is still another possible circuit by using a simple logic level N-FET together with the internal 1.2V voltage reference. (see attachment). For 9V applications you can reduce R2 significantly. R1 is chosen to met the "below 10k" specifications of the A/D-converter.The only restriction is that you can never get more than about 3.3V-1.5V = 1.8V at the ADC-Pin.This is because the BSS123 needs a voltage difference of 1.5V between source + gate to conduct.with best regardsAndreasp.s. the BSS138 is meant for the 1.5V. But with the internal reference the 1.8V of the BSS123 will work too @ 3.3V.

author: georges80
author_rank: Frequent Contributor
timestamp: February 28, 2014, 09:06:01 pm
content: Input leakage current 1uA spec is for a I/O pin. Disable the digital feature of the pin and use is with the A/D and the input leakage will be much lower. As I wrote, I use around 400kohms as my divider impedance and it works FINE."The ADC is optimized for analog signals with an output impedance of approximately 10 k? or less. If such a sourceis used, the sampling time will be negligible. If a source with higher impedance is used, the sampling time willdepend on how long time the source needs to charge the S/H capacitor, with can vary widely."Above from the datasheet, the 10k is due to having to charge the S/H cap. If you are measuring a battery voltage you don't need to sample it quickly. Adding even a 0.01uF cap in parallel with the uC input (to GND) will provide more than enough charge storage to deal with the S/H and still use a high impedance divider.I've shipped 10's of thousands of AVR based boards and 400k ohm is NOT a problem for input impedance of the divider when used for measuring slow changing signals like battery voltage.For Atmel AVR stuff the avrfreaks forum has a LOT of information and folk with specific knowledge - that's where I'd call 'home' if I had AVR questions...cheers,george.

author: Andreas
author_rank: Super Contributor
timestamp: February 28, 2014, 09:29:07 pm
content: Quote from: georges80 on February 28, 2014, 09:06:01 pmBelieve what you want.The datasheet tells somewhat different:"24.6.1 Analog Input CircuitryThe analog input circuitry for single ended channels is illustrated in Figure 24-8. An analog source applied to ADCnis subjected to the pin capacitance and input leakage of that pin, regardless of whether that channel is selected as input for the ADC. When the channel is selected, the source must drive the S/H capacitor through the series resistance (combined resistance in the input path)."The only pins that are specified different from 1uA leakage are the comparator pins.with best regardsAndreas

author: georges80
author_rank: Frequent Contributor
timestamp: February 28, 2014, 09:46:46 pm
content: I don't need to believe, I have tested. The 10K specifically states that it is a sampling speed issue. This means, slow the sampling speed or add some external capacitance and it will take care of the S/H issue.Anyhow, I'm sure you've built/shipped many products using the AVR family...cheers,george.

author: dannyf
author_rank: Super Contributor
timestamp: February 28, 2014, 11:16:45 pm
content: QuoteWhy would that concern you?QuoteSo?

author: delmadord
author_rank: Regular Contributor
timestamp: March 01, 2014, 12:03:38 am
content: Quote from: dannyf on February 28, 2014, 11:16:45 pmIsn't absolute maximum voltage on IO pin the VCC + 0.3V ?Quote from: dannyf on February 28, 2014, 11:16:45 pmThere is a diode for ESD spike forward biased from pin to VCC. If there is a high enough voltage on the pin, the diode starts to conduct. I am not sure if even some uA are healthy.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: March 01, 2014, 12:21:52 am
content: http://electricstuff.co.uk/picavrstuff.html#Measuring%20battery%20voltage

author: dannyf
author_rank: Super Contributor
timestamp: March 01, 2014, 01:50:40 am
content: QuoteWhy the +0.3v? If you look at the pin structure, you will notice those clamping diodes.QuoteThe datasheet can help you answer that definitively.

author: Andreas
author_rank: Super Contributor
timestamp: March 01, 2014, 06:06:15 am
content: Quote from: delmadord on March 01, 2014, 12:03:38 amHello,AVR182 specifies a allowed clamping current up to 1mA (on some other MCUs are only 0.5mA are allowed).But in some cases it is better not to rely on the internal clamping diodes.- If you use a low power design with sleep mode using only some uA during sleep together with a voltage regulator which only delivers output current and cannot sink any current (like most regulators). In this case during sleep the supply voltage will go up to nearly the 9V through the clamping diodes.- In one design I had a bus powered FTDI module with TTL-Levels connected via clamping resistors to a AVR with a LC-Display.The problem was that after power cycling of the AVR circuit the AVR did not start up correctly because it got current over the FTDI module via clamping diodes. The display was "dark" in this case, except immediately after programming. (In this case there was a correct reset).- Another design had ADC issues on a neighbour pin of a ADC-pin which was connected via 100k to a RS232 connector. Every time with a negative edge (-12V = -120uA) the input filtering capacitor of the ADC pin was discharged by several 10-100mV disturbing the next ADC conversion. A schottky clamping diode external to the RS232 pin helped in this case.So you can use the internal clamping diodes as long as you do not violate the absolute maximum ratings. But you have to examine carefully the risks and side effects.with best regardsAndreas

author: delmadord
author_rank: Regular Contributor
timestamp: March 01, 2014, 10:04:52 am
content: Quote from: mikeselectricstuff on March 01, 2014, 12:21:52 amThanks Mike! Looks nice. In the text on your page, it says I can use a digital transistor (with integrated resistors too), so I can avoid the 0.7V Vce drop, right? What is the value of that Cap? 10n ?EDIT:When I try it in the simulator, it only works with BJT transistor, not with MOSFET. However, there seems to be no error caused by the transistor. Also, I have found that, the voltage on divider stays longer with greater capacitor value, and then gets to the ground. I have observed from simultor that with 10uF it is about 100ms, seems enough. It stops the current drainage through divider automatically, if the control pin on the MCU somehow "forgets" to do it. I really like this solution!

author: papo
author_rank: Regular Contributor
timestamp: March 02, 2014, 08:54:27 pm
content: HiQuote from: delmadord on March 01, 2014, 10:04:52 amI think that the transistor with internal resistors is just a size thing. The Vce drop is much lower than 0.7 V, you probably confused it with Vbe? The cap has to be sufficiently large to charge the sampling cap inside the micro (this was discussed above). Other than that, I don't think that its value is too cricical.This is really a neat trick!RegardsMatt