Elementary microcontroller examples feature “in rail” voltage measurements. A prime example is the measurement of a variable resistor’s output. One end of the resistor is connected to the microcontroller’s 5.0 voltage rail, the other is grounded, and the wiper is sent to a microcontroller’s Analog to Digital Converter (ADC) pin. The resulting voltage is bounded by ground and the microcontroller voltage rail.
This article provides guidelines for selecting resistors for a microcontroller measuring outofrail voltages. You will learn how accurately measure these higher voltages without damaging your microcontroller. The article is focused on optimizing the resistor selection.
Our discussion is limited to DC voltages generally less than 30 VDC. The AC considerations are beyond the scope of this short article. Also, higher voltage DC measurements may require additional safety considerations which again are outside of this article’s scope.
Model Microcontroller Analog Inputs
Let’s start with a model of the microcontroller analog input. Figure 1 presents a simplified model for the Microchip (Atmel) ATmega4809. This is a popular microcontroller featured on the Arduino Nano Every. The prime component in this model is the sample and hold capacitor. The sampled signal must be present on this capacitor before the conversion begins.
Figure 1: Simplified model of a microcontroller’s analog input pin in relation to the external voltage being measured.
Tech Tip: A small mistake will lead to the microcontroller’s destruction. We must be especially careful near R1 and the higher source voltage. A wiring or probing mistake could inadvertently send the high voltage to the microcontroller’s I/O pin. While the rail clamp diode provides a small measure of protection, it will be destroyed in an instant along with the microcontroller itself. In extreme cases the higher voltage will travel all the way to UPS port on the associated PC. If you are lucky, it will damage the PC’s port. If you are unlucky, you will be in the market for a new PC.
This Sample and Hold (S/H) requirement is a complex interaction between various microcontroller structures including:

Operation of the analog selector mux. From the Arduino perspective, this operates when you differentiate between analog input pins such A5 and A7.

Size of the S/H capacitor. The ATmega 4809 has a Sample Capacitance Selection (SAMPCAP) bit in the Control C special function register. According to the data sheet: “This bit selects the sample capacitance, and hence, the input impedance. The best value is dependent on the reference voltage and the application’s electrical properties.”

Internal contributors. This includes leakage associated with components such as the pin’s clamp diodes, resistance, and capacitive interaction between adjacent pins.
We can visualize this capacitor voltage / stability requirement in terms of time constants with an emphasis on input impedance. The result is the simplified model presented in Figure 2. The internal microcontroller structure has been replaced by a momentary switch and the S/H capacitor. The external voltage source and voltage divider have been replaced with their Thévenin equivalent circuit.
Figure 2: High level model of a microcontroller’s input pin as related to a source.
When an ADC conversion is initiated, the momentary switch will close for a brief period of time. This is the “sample phase” where the S/H capacitor charges to the desired voltage. A brief time later, the switch opens. This is the “hold phase” where the capacitor presents the held (sampled) voltage to the ADC.
There is a relationship between the S/H time and the Thévenin impedance of the source. For an accurate measurement R_{Th} must provide a small time constant when we consider the \tau = RC relationship. Here, the term “small” implies that the S/H capacitor will be fully charged in the short duration when the momentary switch is closed.
Tech Tip: The Thévenin equivalent circuit is a powerful tool akin to Ohm’s Law in its importance. It allows a complex circuit to be characterized as an ideal voltage source (V_{Th}) and a series resistance (R_{Th}. Simplified calculations can then be performed using the model. This includes the time constant mentioned in this article.
At this point, we return our attention to the ATmega4809 datasheet. We are given a few guidelines:

The ADC is optimized for a source R_{Th} less than 10 kΩ.

The previously mentioned SAMPCAP bit may be activated to reduce the size of the S/H capacitor from 10 pF to 5 pF.

The sample length (SAMPLEN) control register may be used to increase the hold time for sources with higher impedance.
Taken together, these suggestions provide an excellent starting point for our resistor selection.
Voltage Divider For ADC Scaling
For proper resistor selection, we must consider the voltage to be measured along with the Thévenin resistance.
A voltage divider is defined as:
V_{Out} = V_{In(Max)}\dfrac{R_2}{R_1 + R_2}
where:

V_{Out} is the fullscale microcontroller ADC input voltage.

V_{In(Max)} is the highest voltage to be measured including any surge or charging voltage. The ADC will saturate if the voltage is unexpectedly higher. For example, if 10bit ADC was set up to measure a 16 VDC(max) signal, it would saturate and read 1023 for all voltages higher than 16 VDC.
The Thévenin resistance for a voltage divider is:
R_{Th} = \dfrac{1}{1/R_1 + 1/R_2}
We have simplified the calculation by assuming that the voltage divider does not appreciably load the source. Perhaps we can explore the loading on the source’s Thévenin equivalent circuit in another post.
Tech Tip: The default ADC reference for the Arduino is the positive voltage rail itself which is about 5 VDC for the Arduino Nano Every. For improved performance the internal 1.1 VDC reference may be used by calling analogReference(Internal);. External reference standards such as the LM4040AIZ4.1/NOPB may also be used. These devices offer good tolerance with small temperature coefficients. The “full scale” ADC input voltage would be 5.0, 1.1, and 4.096 respectively.
The resistor selection is not constrained; in that, we can choose either R1 or R2 and then calculate the corresponding resistor. Unfortunately, there is no perfect solution. The best we can do is balance the competing demands of source loading, energy dissipation in the voltage divider for battery powered circuits, the need to stay below the microcontrollers 10 kΩ input impedance, or the need to extend ADC conversion time to accommodate a high R_{Th}.
Design Example
To illustrate the resistor selection process, let’s design a circuit to measure a nominal 12 VDC system associated with a flooded leadacid battery charger. We start by identifying the highest voltage likely to be encountered. While the system is called “12 VDC” the trickle charge is about 13.8 VDC and an equalization charge may be as high as 14.7 VDC (consult the battery datasheet for exact values). If we allow a 10% overhead, we will then design for a 16 VDC(Max) voltage.
Configure the microcontroller for a 4.096 VDC external reference.
4.096 = 16\dfrac{R_2}{R_1 + R_2}
By collecting the variables, we can determine the ratio of R1 to R2:
\dfrac{R_1}{R_2} = 2.906
Our objective is to find a resistor pair that is as close as possible to this ratio such that the resulting R_{Th} is less than 10 kΩ.
Selecting resistor pairs is a fascinating topic onto itself. Using the techniques presented in a previous article, from E24 (5%) series, the closest matching pair is 4.7 k and 1.6 kΩ for R1 and R2 respectively for a ratio of 2.937. Using E96 (1%) we can select 8.98 kΩ and 3.09 kΩ for an improved ratio of 2.906.
In an ideal situation, the full scale voltage into to the ADC is now:
V_{ADC} = 16.0\dfrac{3.09 k}{3.09 k + 8.98 k} = 4.0961 \ VDC
In practice, there is still an error. To be sure, the 1% resistors have improved the situation. However, they are not perfect. Consider the range of acceptable values for each resistor:
8.8902 kΩ < R_1 < 9.0698 kΩ
3.0591 kΩ < R_2 < 3.1209 kΩ
When applied to the voltage divider, the full scale (16 VDC input) will vary from 4.0355 to 4.1574 VDC. This +/ 61 mV error is attributed to the chosen resistor tolerance. There are several solutions to this predicament including the use of lower tolerance (higher cost) resistors, hand selection of resistors, installation of trimmer resistors, or application of a calibration correction in the microcontroller. Each solution is associated with its own cost and time penalty. Yet, that is the unavoidable price of increased precision.
System Performance
A fair critique is that we have been overly fussy with the resistor selection. The reason for this fastidious selection becomes apparent when we consider the performance of the entire system.
Recall that our original 10bit ADC with its external voltage reference is limited to a fullscale binary reading of 1023. If we have selected our voltage divider correctly, this will occur when the measured voltage reaches 16 VDC. A consequence is that the system now has a resolution (smallest detectible voltage change) of approximately 16 mV. This is the best we can do as we have faithfully taken our maximum anticipated voltage and stepped it down to match the fullscale output of the ADC.
Let’s consider a glaring counter example. Suppose we instead selected R1 and R2 using convenient 10 kΩ to 1 kΩ values respectively. The measured voltage would need to reach 45.1 VDC for the ADC to reach a fullscale count of 1023. Since there are 1023 steps to 45.1 VDCX, the resolution is worsened to 44 mV. This is highly undesirable as we are no longer able to discern small changes in the measured voltage.
Tech Tip: Voltage references with values such as 1.024, 2.048, and 4.096 may seem familiar as the voltages are related to Power Of Two (PO2) as in 1, 2, 4, … , 512, 1024, 2048, and 4096. These are convenient values especially when integer math is used. In our example, the 10bit ADC equipped with a 4.096 VDC reference is used to measure a 16 VDC (max) signal. Assuming we selected ideal resistors for the voltage divider, each bit change aligns with a PO2 voltage change. In this example each ADC bit represent 1/64 of a volt. This may or may not be useful to your application as multiplication can be performed using a series of left shift operators. Note that the speed advantage is lost when we use type float values to scale these unique voltage references.
Review
A microcontroller may be used to measure voltages higher than its 5.0 or 3.3 VDC supply rail, provided a proper voltage divider is used to reduce the measured voltage. Proper resistor selection ensures that the microcontroller will have the best available resolution considering both the voltage under measurement and the ADC’s voltage reference. Selection of the proper resistors is aided by this previous article which includes a crude C program to assist in finding the best available resistor pair from the standard values.
Best Wishes,
APDahlen