Hey Newsletter Readers: Looking for Best Resistor Sorting Method? My Bad!
- Measure Battery Voltage
- Signal Level Shifter
- Reference Voltage
- R-2R Ladder
- One Analog Input with Many Buttons
#1 Measure Battery Voltage
There are a couple of different cases where you might need to “step down” your battery voltage. In this case, this is NOT a substitute for a buck regulator. Instead, you might need to step down the battery voltage to measure it. Let’s say you are using a 3v3 microprocessor (Raspberry Pi) or ESP8266 (Microcontroller). Your board is being powered by two LiPos in series. Together these batteries create a supply of 7.4 volts.Problem using Simple Voltage Divider Circuits
Two 100kohm resistors divide the voltage from 7.4 to 3.7 volts. While easy, that is still too high for a most 3.3 volt systems. (At least the ones you want to last.) When splitting the voltage in half does not work, I turn to this voltage divider calculator. With R1 at 100kOhm and R2 at 68kOhm, the divider outputs about 3.0 volts. Simple enough, right? Well, there are two problems.Leakage Current
First, connecting these two resistors in series to the battery will create a leakage current. No matter what else is going on in the circuit, 44 µA will flow through the divider. Sounds small, but that means 325 µWatts of wasted power. With USB there is little concern about that much leakage. When powered by batteries, however, that leakage means less battery life.Back powering
If your board stays on all of the time the battery is connected, then “back feed” or “back powering” is an issue for you. Here, let’s say you take the simple divider from above and leave it connected to the analog pin. At the microcontroller, you add a switch to turn it off. Now, this switch turns off VCC but not the battery voltage. This circuit will cause the voltage divider to “power” the microcontroller through the I/O pin. This path can damage the microprocessor and means your battery-powered circuit stays powered! So you need a way to avoid back powering your circuit.Add transistors to voltage divider circuits
Most applications do not need continuous battery voltage monitoring. What if you could just turn on the voltage divider when you make the measurement? Ah-ha! Here’s a solution. Add a high-side PNP transistor to the simple voltage divider. A digital I/O pin controls a NPN, which turns the PNP on and off. With this configruation, no current can sneak through the analog pin’s protection diodes. And you have full control over how long the divider is activated.Update: Thank you to James Whong from Mooshimeter for helping correct this circuit.
Note: Using voltage divider circuits to measure the voltage of Lithium-Polymer batteries isn’t a reliable method to determine how much energy they have left. But I’m asked about it often, so I wanted to mention it here. (For those that are going to do it anyway.)Their voltage will drop below 3.7 volts when they are about to die. So you can tell when they are low, but voltage alone cannot tell you how long they will operate.
Raspberry Pi Battery Measurement (without Analog)
Wait a minute; I mentioned the Pi. To read a battery voltage, don’t you need an analog-to-digital converter? Yes, you do, sometimes. An ADC is necessary to measure precise voltages. If you’re just looking for a low battery indicator, treat it as a digital signal! You could set the divider to 2.35 volts based on your battery’s “low voltage.” Any battery voltage above that point will give a larger value than 2.35 volts, which is the logic-high threshold of the Pi’s GPIO pins.#2 Voltage Level Shifter
Modern microcontrollers are based on 3v3 logic with 1v8 logic right behind it. Using older 5.0-volt technology means you need to voltage-shift I/O signals. For example connecting an Arduino Uno’s output directly to an ESP8266 input can cause damage. (I don’t care what you read on that random forum. Don’t connect 5.0-volt logic to 3.3-volt boards!) Like I said this is the most common of the voltage divider circuits that I build. One option is to use an application-specific chip. TI’s TXB0108 Bi-Direction shifter is one example. If you are interested in that chip, Adafruit offers a breakout board with it. This chip is designed for bi-directional operation. I2C signals are an example of a bi-directional bus. Serial, or UART, is a case of a unidirectional signal pair. So to connect serial between a 5.0-volt and 3.3-volt system, you need logic shifting. I bet when I called this article “voltage divider circuits,” you didn’t expect two voltage divider examples in a row, right? For uni-directional signals just put two resistors in series and tap it’s Vout to go to the I/O pin. Good values to use here are R1 at 10kohm and R2 at 15kohm. Keep in mind signals going from the 3v3 system to the 5v0 system, don’t need shifting. The Raspberry Pi’s output voltage is high enough for an Arduino Uno to see it as a logic high. It is only when you go from 5.0 to 3.3 volts you need the voltage divider.#3 Reference
Not all voltage divider circuits use just resistors. One example contains a resistor and diode. The diode’s reverse voltage creates a precision reference voltage. The resistor creates a series circuit, just like when used with an LED. Zener diodes are often used to create these reference voltages. When a Zener diode is reverse biased, it produces a relatively small voltage drop. The remaining voltage drops across the series resistor. Knowing your source voltage, the Zener’s reverse voltage, and the current needed through the Zener, you can calculate the series resistor’s value. An example is this 3.3V Zener from Diodes Inc (at Digi-Key). Looking at the data sheet, it’s nominal Zener drop is 3.3 volts when 5.0 mA is applied. Using a stable 5.0 volt supply, a 340 ohm resistor will give you a stable Vout of 3.3 volts. Keep in mind this isn’t a voltage regulator. Well, at least, not one that can provide much current.Use it for the analog reference
Let’s say you have an 5 volt Arduino measuring the 7.4 volt lithium polymer battery pack from the first example. Attaching this circuit to the AREF pin will increase the resolution of the A/D converter. The full-scale range of the A/D is no longer VCC’s 5.0 volts. Instead, it will be the lower 3.3 volts. Why 3.3 volts? Well, I picked it as an easy to buy Zener. And to measure the battery with an Arduino, you’ll need to divide down anyway. So here you’ll do something like a 2.3:1 divider. For slightly better resolution you could use a 3.7 volt Zener, if you can find one.#4 R-2R Ladder
An R-2R ladder is a bunch of repeating resistors, or a network of resistors. The idea is that as you turn on more inputs, it affects the output voltage. This circuit is one way to make a digital-to-analog converter (DAC). You apply digital inputs and get an analog voltage out. Since this is a form of a current divider, the performance depends on how well the resistors are matched to each other. So you should use precision parts or measure each to match them manually. Add more resistors to the string for more resolution. Use better matched resistors for more precision. In this example with 1K and 2K resistors, each bit is worth 313 mV. The max output is 4.68 volts. I might cover these in more detail in the future. For now, check out this great Tektronix blog post on R-2R DACs.#5 One Analog Input Button for many Buttons
Using a network of buttons and resistors you can use a single analog input for multiple buttons. Between each button is a resistor value. In this example, I used 470 ohms resistors. When you press a button, R1 (1k) creates a divider with the rest of the network. Here I am pressing button two (2) which means the voltage out is a divider between 1 Kohm and 1.4 Kohm. You don’t have to use equal resistor values. My suggestion is that you pick values that give you a wide range between the buttons. That way your code reading the analog input can have a wide input range. By the way, to check a range in C/C++, you need to use a logical-AND statement. So for example, reading A0 on an Uno might look like this:
int analogValue = analogRead(A0);
If ( (analogValue > 128) && (analogValue < 512) )
// Do something… it’s between 128 and 512.
Conclusion
Voltage dividers have a variety of uses. As you might notice, nowhere on this list is there a “voltage regulator.” The Zener reference comes close, but that circuit isn’t designed to drive much current. Or at least, not current to a load that might change.Did I miss any other voltage divider circuits? If so, leave a comment with them below.
28 Comments
Did you miss any?
Well, there’s the low-pass and high-pass filters using RC and RL components. Sure, they’re mostly for AC (audio) but 1st order designs which are commonplace are just voltage dividers. You can even do it with two capacitors or two inductors although that’s usually not done.
Great ideas, came across your site when looking for ideas on voltage dividers. Would you have a recommendation for a high-side PnP when turning on/off a voltage divider with V in around 28v (down to 23 vDC)? I did some looking and finding several in the voltage range, I think. How much of a buffer on the emitter/collector break down should I have?
Thanks in advance.
Hello James,
thank you for the article. I can imagine most of this to be considered as being “basics” for your audience but, despite the risk of outing myself as someone who did not do his homework – I still find it hard to apply the provided examples to the problem I’m facng.
The idea is to make … wait for it … an MQTT garage door opener / helper. For that I bought a Wemos D1 Mini incl. some shields. While I’m able to control a relay that then triggers an existing switch, I would also like to implement a rudimentary sensor to check whether the garage door is open or closed.
The system does already have two reed switches (guessing) to limit the movement of the door. Checking with a multimeter, I got a “solid” 5V reading when the switch / circuit is closed (the door open or in between). This led me to believe that I could fed this “signal” into the D1 to then have some form of a reference point.
Allow me to throw in a few keywords that I picked up along my research, which I think underline the actual question(s) I’m prolonging to ask.
Common Ground
The garage door system and the D1 are / will be powered via a separate power supply. This means, that for a voltage divider circuit, I must have a common ground, right?
Back powering
I suppose it would be wise to implement some form of switch(es) for the -> two <- "signals" that I want to feed to the one and only analog input pin of the D1? So "a high-side PNP transistor" is needed, correct? The example circuit you've provided, at least to me, shows two transistors being used?! Do I have to duplicate the circuit shown for each "input line"?
Analog Input
Given that I'm not really interested in the voltage level but only the state of the reed switches and only have one analog input, is there a better solution to detect their state with the D1?
Thank you for your time
Sven
I have a problem where a 1.0V standard needs to be nulled with a 10.0V standard with 90k/10k divider. I’m using a Vishay precision divider network unit. But I can’t figure out where to attach the divider. It works with the divider on the meter with the 10.0V (732B) alone, albeit with ~700ppm discrepancy.
Any help would be great.
I don’t understand what this means.
Part number?
Not enough information to suggest.
Since you used a fake email address, there isn’t much point in replying now, is there?
This was a great find for me, since I’m now using some voltage dividers to tell me when two devices are powered up by the voltage they put out on their USB ports. But I’ve also found out that I’ve got a (potential) backpower problem. So I really liked your example of curing that with a transistor, but since the controller board I’m using only has three free pins, and I’ve got two inputs and an IR LED output to drive, I was wondering if you think it could be setup so that I use the Transistor method, but then also the multiple buttons on one Analog Pin to read my voltages? I don’t care about the values, just whether or not voltage 1 is on/off, and voltage 2 is on/off.
Off hand, use the power supply to bias/enable the measurement transistor.
That was one of my thoughts… actually having the TV power the unit, since I think it will boot quickly enough to not be painful. It’s been quite nice having it up and running too! The family rolls their eyes a bit, but they’re enjoying not having to hunt for a second remote at times too.
It’s interesting how working in a small restricted hardware set makes you think more. Fun!
James, I ended up designing a board in KiCAD for this, but now I’ve run into a problem where I screwed up somewhere and the voltage on the input pin is always around 1.3V, with a 3.3V used to turn on the transistor (2N3904, what I had in my parts bin…) Base. So on a breadboard, it all works fine. But in the PCB… it’s broke. Gah!
But from what I’ve been reading, it might be actually better to use a dual optoisolator as my sense instead, since I’ll be guaranteed to not have back powering of my board.
In any case, thanks for your blog, it’s really informative and a great thing to read!
John
There has to be an issue with how you designed the PCB then. Make sure you didn’t turn the 2N3904 around when you soldered it to your PCB.
“Add transistors to voltage divider circuits”
The circuit that you have depicted, what limits the current, IB from the PNP’s emitter to base? It looks like the limit is the 100k series resistor into the NPN and the NPN’s maximum hfe. Reconsider this circuit by adding say a 10k ohm resistor inseries between the PNP base and NPN collector node.
Talking about transistors to voltage divider circuits.
As mentioned before, a low side switch would be better choice. I would prefer a “logic level” NMOS over a bipolar transistor. Advantages are virtually no voltage drop due to very small RDSon and simple control of transistor’s Gate as no resistor needed to control it. That way you could even increase reasonably the current through divider to get a better result during short measurement. Remember internal resistance (and Caps in S&H) of ADC input is a virtual resistance parallel to divider and seen as a load.
You missed a favorite of mine, the reverse of #3. Putting the zener in the high side before a voltage divider allows you to expand the scale by subtracting a fixed voltage. This can give a few more bits of data with an A/D.
In 1978, ON4OVmade already a power sypply with a 7805, it was working perfectly, I toke it as base for all my power supply’s wiht LM317. I changed the 7805 to LM317 only because the tension was limited to 5V, with the LM317 1.2V is possible.
I send the schematic to your Email adress.
The attachment didn’t come across. Can you send it again?
Thanks, BE, for some great ideas!
Re #5, reading multiple buttons with one Analog input pin:
If you choose your resistors to produce evenly spaced codes, instead of using multiple if … statements the software an use a switch .. case structure, which in my opion gives simpler and more transparent code.
The resistor values can be worked out most easily using a spreadsheet.
For example, with 4 buttons, the resistors should be 1500 (SW1), 680 (SW2), 330 (SW3) and 100 (SW4) using your circuit with a 1000 ohm top resistor. Using preferred values for the resistors introduces some deviations from evenly spaced codes, but still OK.
The voltages with 1 button pressed are: SW1 3.62V (code 740); SW2 2.63V (code 538); SW3 1.5V (code 308); and SW4 0.45V (code 93). Divide by the average gap between codes (a bit over 200) and the result is 3,2,1,0.
The software can then be:
[arduino]
int analogValue = analogRead(A0);
switch (analogValue/200) {
case 3:
// do something.. its between 600 and 799
break;
case 2:
// do something.. its between 400 and 599
break;
case 1:
// do something.. its between 200 and 399
break;
case 0:
// do something.. its between 0 and 199
break;
default:
// no key pressed
}
[/arduino]
Clever. I like the idea. Thanks.
Hi James! In the figure under “Add transistors to voltage divider circuits”, the diagram has a high side NPN transistor. To turn this transistor on, you will need a base voltage that is at least Vbat+0.7, which is probably impractical with 3.3V logic. I suggest using a high side PNP with the base current pulled down by a low side NPN, with the NPN switched by the 3.3V digital IO.
Thanks for another great blog post!
Excellent point. High side switch. I’ll fix it soon. Thanks. (If only I had built a real circuit and tested it with my Mooshimeter first!)
another, simpler, yet working idea is to connect the low side of the voltage divider directly to the GPIO-pin – if you can put the GPIO-pin into a high-impedance state (input). Alternatively you can just switch the low side of the divider by an NPN transistor.
In the high-impedance state or with the NPN off there is essentially no current flowing (in the case of the GPIO pin alone there might be a leakage current through the protection diodes!).
When you switch the GPIO-pin to 0 or alternatively turn on the NPN with a 1, then current will flow. Yes, there is a drop over the output transistor or the collector-emitter of the NPN, but that is in the same order as your drop over the PNP (or even less), and for these low currents it should be negligible.
I’m not a fan of the pin-sinking idea. When in the High-Z state, (nearly) the full battery voltage is going to be applied to the IO pin. (Plus the leakage issue you mentioned.)
Very nice writeup.
What IDE/theme are you using, because I love the dark background.
Thanks.
For code, it is a WP plugin called Syntax Highlighter. The theme is RDark.
Hello sir,
I want to measure the energy consumed in our house accurately by using clamp sensors,
Im requesting to give me your time to make a blog about measuring the sinusoidal waveform by using microcontrollers like arduino.or esp chip.
I have a problem in measuring the energy(kwh) of inductive loads, i think this is due to powerfactor..
I dont know how to measure the phaseshift beetween voltage ang current.
If you have some time it will be a big help to me. Cause i love the way you explain circuits from simple to complex.
Mathematicaly it is easy to calculate the energy by just using integral to get the average voltage.
But making it in actual is a different thing.
Thanks in advance
I’ll have to give this some thought. I’m generally don’t like to discuss AC circuits. I’m not a licensed electrician and I don’t have enough experience to comfortably give safe advice. Using clamp sensors, however, does reduce the risk. So, as you say it, it becomes more a discussion on handling sine waves and some math. I haven’t done work with PFC circuits for years, so it’ll require me to do some homework before I could help you. Which is to say, I’ll add it to my list of ideas, but it will be some time before I come back to it.
Ok thanks
More power