I need to measure battery voltage which supplies my electronics.

Vbat supplies buck-boost DC/DC converter then goes to uC and other ICs (3,3V). I want to use 3x2 goldpins in conjunction with OpAmp to select Vbat voltage range: U1 (3,6V Li-Ion) or U2 (7,2 2x Li-Ion) or U3 (12V Pb) which generate 0V to 3,3V on uC ADC input / OpAmp output.

That is clear.

Now I would like to feedback uC through GPIO some way that I will know which jumper / battery type has been selected (I need to know that in software). I imagine some additional OpAmps connected to R2/R2'/R2'' but I have no idea how to do it.

I'll appreciate your help :)


simulate this circuit – Schematic created using CircuitLab

Edit 1:

The device is used to measure water pH and conductivity. It needs temperature feedback for pH and conductivity ICs and communicates through GSM. It will be powered by batteries so it needs to be energy-saving. Most of time it sleeps and every 5 minutes it makes the measurements. GSM is turned on (GSM sleep or DC/DC sleep mode) only for daily report or when measurements are out of range. When it sleeps sub-ICs takes: 1mA (pH) + 0,4mA (conductivity) + 1mA (GSM sleep mode) or 1uA (GSM DC/DC power down quiescent current) + ??mA (PT1000 - not yet determined) + ??mA (uC - not yet determined but low).

Battery's voltage ADC measurement is need to calculate battery percentage of capacity and time-to-live (based on past statistics), additionaly raw voltage display. For above I need OpAmp or voltage divider. Selecting battery type (input voltage) is mandatory to make calculations more detail. I dont want to do software computing and if-else to figure out if battery is 12V or 3.6V, because it is possible to plugin any battery combination which summed voltage is from 3V to 15V. Of course it is easy to determine from one voltage divider the battery type: 3.2-4.2V for one-pack Li-Ion, 6.4-8.4V for two-pack, and 10.8-12.6V. Just thinking out loud :)

Getting back to merritum: I need to choose between OpAmp and voltage divider based on energy-saving. Voltage divider (mega ohms) will consume uA the same for OpAmp's quiescent current in shutdown mode. Im puzzled because at first I thought OpAmp will be more energy-saving. Whant do you think guys and gals? :)

Edit 2:

Accuracy and resolution. The ADC is from STM32F091 and it is 12bit so cutting off the noise lets assume it is 10bit. The worst case is Li-Ion battery which voltage ranges from 3.4V to 3.7V. That give 300mV delta. To represent the percentage I need 0,5% step so the resolution is 1.5mV per step. That is when using dedicated voltage divider.

When using single voltage divider for all batteries types: The maximum battery voltage might be about 13V (for lead) so I need to scale it to 3.3V => div = 4. When using single Li-Ion I will get with this divider values from 925mV to 850mV so delta is 75mV. The needed resolution is 75mV/200 = 0.375mV. Step for 0V-3.3V 10bit is 3.2mV so 10 times too small. Even if I use all 12bit the step is too small - 0.8mV.

I think I need dedicated voltage divider for battery type or again OpAmp? Whichever would be more energy-saving??

  • \$\begingroup\$ I have a different suggestion, instead of goldpins in the opamp feedback, use a multiplexer like 74HC4051. Then program the 74HC4051's inputs with goldpins (you only need 2 goldpins because with 2 bits you can select 4 states). The same 2 bits you can feed directly to the uC. Beware though that the multiplexer has some series resistance so I would make the values of R1 and R2 in the xx kOhms range to make that series resistance insignificant. \$\endgroup\$ Commented Jan 21, 2016 at 8:33
  • \$\begingroup\$ A mere voltage divider should be able to do the job, I think. \$\endgroup\$ Commented Jan 21, 2016 at 8:34
  • \$\begingroup\$ You do realize that the op-amp drawn is inverting and that a +V input will produce a "below ground" output. \$\endgroup\$
    – Andy aka
    Commented Jan 21, 2016 at 8:35
  • \$\begingroup\$ @NickAlexeev - I think so too, then switch different resistors in the tap-to-ground part of the divider using uC pins as Open drain pull down. Need to watch overvoltage, or use a cheap FET. \$\endgroup\$
    – ChrisR
    Commented Jan 21, 2016 at 8:56
  • \$\begingroup\$ @FakeMoustache: +1 nice one! :) \$\endgroup\$ Commented Jan 21, 2016 at 12:00

3 Answers 3


Since the jumpers have to be set manually, a simple solution might be to use a second jumper that indicates the selected gain to the microcontroller. So the user have to set two jumpers to identical positions. Or just use a rotary switch with two poles and three positions (e.g.: SS-10-23NPE).

A completely different approach would be to implement a autoranging function, where the microcontroller selects the gain factor automatically. Just use a small analog switch that will be controlled by the microcontroller. Now it is possible to auto gain the input signal: start with the lowest gain factor and work your way up just one step before saturation of the ADC input would occur. If neccessary protect the ADC input from overvoltage: just add a clamping zehner or TVS diode (important: low leakage current!) to the output of the amplifier.

  • \$\begingroup\$ I'll go with 74HC4051 multiplexer with 8 resistors in the divider backed up with zener diode and software autoranging for ADC. Tahnks! :) \$\endgroup\$ Commented Jan 21, 2016 at 14:59

Peak voltage to measure might be 13V and if you have a 10 bit ADC then you can arrange this, with a suitable resistor divider, to give ~13 mV per bit.

How much accuracy are you going to require when measuring the voltage - is not 13 mV enough?

Anyway, this is the simplest way of doing it and you don't need to worry about what link you have selected.

  • \$\begingroup\$ Iv updated my post. I think I will go for single voltage divider because measurement resolution is not the issue. I need to know more or less what is the percentage of battery capacity. Using Li-Ion instead of Pb it will degraded the resolution but it is acceptable. \$\endgroup\$ Commented Jan 21, 2016 at 12:05
  • \$\begingroup\$ Firstly noise helps (it's called dithering). It works by randomly wobbling the signal a little bit and you can get a single bit extra resolution by averaging 4 samples. So, you should use 12 bits and across a range of (say) 15V, one LSb = 3.66 mV. Are you saying that is too clunky for what you are trying to achieve? Gut feeling tells me that 3.66mV resolution should be fine but maybe you can link a document or website that says differently? \$\endgroup\$
    – Andy aka
    Commented Jan 21, 2016 at 12:39
  • \$\begingroup\$ please check my Edit2. 3.66mV would be enough If I would use OpAmp or voltage divider with jumpers to choose appropriate battery type (voltage fit). If I'll use single OpAmp/VD for all batteries types then it is not enough -> my edit 2. Please advise which to use: voltage divider with high resistance or opamp considering energy-saving? Any pitfals? If not Im going with voltage divider + analog multiplexer :) \$\endgroup\$ Commented Jan 21, 2016 at 13:20
  • \$\begingroup\$ You have to think about the wider issues here. You have to pot-down the input and to get anything like 4 mV accuracy you'll need a temperature matched potential divider that has a ratiometric accuracy of 0.025% or better. Anything else degrades the whole idea so, I would advise a single high accuracy divider. If you start using op-amps or other resistor dividers you are going to run into problems for sure. Yes you can find a very temperature stable pot-divider but now you might want to use an analogue switch to modify this and this will cause you significant stability problems..... \$\endgroup\$
    – Andy aka
    Commented Jan 21, 2016 at 13:29
  • \$\begingroup\$ Based on this and not knowing your budget, equipment or skills I can only advise a single accurate potential divider and it will cost ~$10 to get the accuracy you want. You'll need a pot divider anyway so, is your design budgeted to cover this expense? If it can't handle the expense of several quality pot dividers then you must degrade your requirements. \$\endgroup\$
    – Andy aka
    Commented Jan 21, 2016 at 13:32

Replace your mcu with a STM32F2 or F3 devices with builtin opamps.

Some are pin compatible and have internal PGA, you can remove most or all of the external Opamp and associated circuitryfrom the design.

ST have an appnotes where oversampling and noise yield an EXTRA two bits (i.e. effective 14 bits accuracy).



I've tested this and it works, especially in low impedance DC measurement.

The F373 device has 16 bit ADCs as well, but this would really be an overkill.

Also you may replace your opamp with a PGA MCP6S21/2/6/8 or MCP6S91/2/3.

You accuracy, calibration, testing ,PCB and manufacture will all be simpler and cost less.

Power consumption is minimal as you can clock the whole device at a very low speed (32KHz). I would also put the whole device to sleep, or perhaps switch off the analog for long periods between measurements, depending on what else you need to do.

Conserving power this way is a challenge with external opamps.

For one-offs the Nucleo boards cost < $10, Discovery slightly more.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.