> For the complete documentation index, see [llms.txt](https://monasheng.gitbook.io/zerotohero/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monasheng.gitbook.io/zerotohero/arduino-zero-to-hero/core-skills/input-output-pins/voltage-dividers.md).

# Voltage Dividers

{% embed url="<https://youtu.be/Vf2-vD-Vnd4>" %}

It is often the case that you will want to `digitalRead()` or `analogRead()` a voltage using your MCU that is outside of its allowable input range (i.e. $$5\text{ V}$$ for an Arduino). In these circumstances, you can use an arrangement of resistors called a *Voltage Divider* to decrease the voltage to an acceptable range.

## Theory

{% hint style="info" %}
:man\_mage: **Important:** Voltage dividers are a block of two resistors, arranged to scale an input voltage $$V\_\text{in}$$ to a (lower) output voltage $$V\_\text{out}$$ according to the formula:

$$
V\_\text{out} = V\_\text{in} \times \frac{R\_2}{R\_1+R\_2}{}
$$
{% endhint %}

Where $$V\_\text{in}$$ and $$V\_\text{out}$$ are both relative to `GND`, and $$R\_1$$ and $$R\_2$$ are arranged as follows:

<figure><img src="/files/3V4TL7h64z9KkjMn7YT6" alt=""><figcaption><p>Voltage Divider schematic, created in KiCAD.</p></figcaption></figure>

By choosing $$R\_1$$ and $$R\_2$$ carefully, you can alter the divide ratio. You need not calculate this by hand - many online voltage divider calculators exist.

{% embed url="<https://www.digikey.com.au/en/resources/conversion-calculators/conversion-calculator-voltage-divider>" %}

{% hint style="danger" %}
:zap: **Warning:** Voltage dividers are only used for signals, where there is little-no current transfer. They cannot be used to regulate power or in other high-current applications.&#x20;

When *loaded*, [the parameters of the divider can change substantially](https://spinningnumbers.org/a/voltage-divider-design.html#case-1-90-of-v_in).  Consider a [Voltage Regulator](https://www.monolithicpower.com/en/voltage-regulator-types#:~:text=A%20voltage%20regulator%20is%20a,with%20the%20other%20electrical%20components.) for such applications.
{% endhint %}

<details>

<summary>Mathematical Derivation of the Voltage Divider Formula</summary>

Assume a Voltage Divider with values $$R\_1$$ and $$R\_2$$ as shown previously is set up between an input voltage of $$V\_\text{out}$$ and `GND`.&#x20;

Ohms Law applies to each resistor in the divider:

As voltage dividers are only used for signals, where there is essentially zero current flow from input-to-output, the only current flow runs through the divider to `GND`. In other words, the current through $$R\_1$$ is the same as that through $$R\_2$$:

$$i\_1 = i\_2$$

Re-arranging Ohms Law:

$$V = iR \implies i = \frac{V}{R}$$

Identifying the voltage across $$R\_1$$ is $$V\_\text{in}-V\_\text{out}$$, and the voltage across $$R\_2$$ is $$V\_\text{out}$$, we get:

$$\frac{V\_\text{in}-V\_\text{out}}{R\_1} = \frac{V\_\text{out}}{R\_2}$$

Which gives:

$$R\_2 V\_\text{in} - R\_2 V\_\text{out} = R\_1 V\_\text{out}$$

Re-arranging:

$$R\_2 V\_\text{in} = R\_1 V\_\text{out} + R\_2 V\_\text{out}$$

$$\implies R\_2 V\_\text{in} = (R\_1+R\_2) V\_\text{out}$$

$$\implies \frac{R\_2}{R\_1+R\_2}V\_\text{in} = V\_\text{out}$$

$$\implies V\_\text{out}=V\_\text{in}\times\frac{R\_2}{R\_1+R\_2}$$

</details>

## Practical Considerations

### Sensitivity to Input and Safety-Resolution Tradeoff

Voltage dividers only scale inputs, they do not regulate them. In other words, an unexpected rise in input voltage (say when a car turns on and the alternator raises the electronics system voltage from $$\approx 12 \text{ V}$$ to $$\approx 14 \text{ V}$$) will be translated through a divider to a rise in output voltage.

This could lead to damage of your MCU if you have specified your divider ratio to map the full-range of the input pin to, say, $$\approx 12 \text{ V}$$.

{% hint style="info" %}
:man\_mage: **Important:** Be sure to specify your divider ratios to map the highest expected voltage the divider will see on input **in any operating condition** to the safe range for your MCU pins.

You may wish to sacrifice some resolution on your measurement (say $$\approx 10 %$$) to ensure sufficient overhead for unexpected voltage rises.&#x20;
{% endhint %}

For instance, if measuring the battery voltage of a car, you might expect a maximum $$V\_\text{in}\approx 14 \text{ V}$$, but specify resistances to limit your maximum Arduino measurement at $$V\_\text{in}\approx 15 \text{ V}$$ to $$V\_\text{out}\approx 5 \text{ V}$$ to leave some overhead for unexpected voltage rise.

### Common Values

Resistors can be purchased in a wide range of values, though not every value is common/inexpensive. By using readily available values, you can:

1. Reduce the cost of your circuit,
2. Increase availability of spares, and,
3. Reduce lead times in buying replacement parts,

as they may be in stock at a more local supplier (e.g. Jaycar).

Engineering Toolbox provides some guidance on standard/common values for through-hole resistors at various tolerances:

{% embed url="<https://www.engineeringtoolbox.com/resistors-standard-values-d_1659.html>" %}

### Tolerances on Resistors

Off-the-shelf resistors are not perfectly accurate, and will vary in their actual resistance compared to what's written on the packet. Mercifully, electronics manufacturers often provide a *tolerance* on the resistors that they supply.

Tolerances are normally given as a percentage, and should be interpreted as a $$\pm$$, rather than absolute measure. In other words, a $$150;\Omega$$ resistor with a 10% tolerance should be considered within spec provided the actual, measured value is $$150 \pm 15;\Omega$$ - i.e. anywhere from $$135;\Omega$$ to $$165;\Omega$$.

Tolerances stack when building voltage dividers. You should carefully consider the the effect on your divider ratio in a worst case scenario (say when $$R\_1$$ is at its highest point in the tolerance range and $$R\_2$$ it's lowest, and vice-versa).

<figure><img src="/files/rckmXEft2DLc0DXvXICX" alt=""><figcaption><p>Most though-hole resistors use 4 bands to communicate their value. The isolated band provides information on it's tolerance. Image from Wikimedia Commons (Public Domain) <a href="https://en.wikipedia.org/wiki/File:4-Band_Resistor.svg">https://en.wikipedia.org/wiki/File:4-Band_Resistor.svg</a> </p></figcaption></figure>

| Colour |   Tolerance  |
| :----: | :----------: |
|  None  |  $$\pm20%$$  |
| Silver |  $$\pm10%$$  |
|  Gold  |   $$\pm5%$$  |
|   Red  |   $$\pm2%$$  |
|  Brown |   $$\pm1%$$  |
| Orange | $$\pm0.05%$$ |

### Interaction with other components

Voltage dividers are by-design sensitive to the resistances $$R\_1$$ and $$R\_2$$, and some care should be taken to ensure that adjacent components/circuits cannot act in series/parallel with these resistances and change their effective value. This commonly happens by accident with [Floating Voltages and Pull-Up/Pull-Down Resistors](/zerotohero/arduino-zero-to-hero/core-skills/input-output-pins/floating-voltages-and-pull-up-pull-down-resistors.md#pull-up-and-pull-down-resistors).

### Leakage Current

Voltage dividers provide a path from $$V\_\text{in}$$ to `GND`, even when the MCU is not operating. In battery-powered circuits without a global on/off switch, this can lead to flattening of the battery. The leakage current, $$i\_\text{leak}$$, can be calculated according to the total resistance seen by the input voltage:

$$
V\_\text{in} = i\_\text{leak}\times (R\_1+R\_2) \implies i\_\text{leak} = \frac{V\_\text{in}}{R\_1+R\_2}
$$

Be sure to specify your resistances with this is mind. For large resistances and 'normal' voltages (i.e. $$R\_1+R\_2 \approx \text{k}\Omega$$, and $$V\_\text{in}<24\text{ V}$$), the leakage current will typically be low enough that it can be largely disregarded.
