**We talked a lot about voltage dividers and resistance, but how can we actually measure resistance?**

Till now, we always measured resistance using a DMM. For using resistance based sensors like NTCs (temperature) or LDRs (light), we need a way to measure resistance with microcontrollers like our Arduino Uno. There is more than one possible method to do this, but in this tutorial we will focus on resistance measurement using a voltage divider. By replacing one of the resistors in the voltage divider with the sensor we get an output voltage that is dependent on the resistance of sensor. We can then measure that voltage using an ADC (analog-to-digital converter). This method can be also used for non microcontroller circuits, e.g. in conjunction with analog comparators or any other circuit that reacts to the voltage change.

For our measurements we use a classic voltage divider circuit. One resistor value is known and does not change. The second resistor is our sensor. In the example circuit below I used a potentiometer as a variable resistor.

Note that, there is no advantage in this circuit over directly using the potentiometer as voltage divider as it was shown in the last tutorial. This circuit is not exactly smart as we will see at the end of this tutorial. If you use a potentiometer in your projects use it as it is supposed to be used.

Think of the potentiometer as a placeholder for other resistance based sensor. As the resistance of these sensors, the resistance of our potentiometer is variable. In the case of a potentiometer it changes according to the wiper position. In case of the sensors it changes with temperature or light intensity.

Independent of the concrete application, our goal is to get the unknown resistance based on the voltage reading.
We can calculate it using the voltage divider formula:

\({U_{out} \over U_{in}} = {R_2 \over {R_1+R_2}}\)

As \(R_1\) is the unknown resistor in our the example circuit, we need to solve the equation for \(R_1\):

\(R_1+R_2 = R_2 \cdot {U_{in} \over U_{out}}\)

\(R_1 = R_2 \cdot {U_{in} \over U_{out}} - R_2\)

\(R_1 = R_2 \cdot ({U_{in} \over U_{out}} - 1)\)

If R2 is the unknown resistor we would solve the equation for \(R_2\) instead:

\(R_2 = {R_1 \over {{U_{in} \over U_{out}} - 1}}\)

Let's assume we measure \(V_{out} = 2 V\) and our input voltage \(V_{in}\) is 5 V, which is the operating voltage of the Arduino Uno. \(R_2\) is 5.6 kΩ as shown in the circuit diagram. Let's calculate \(R_1\): \(R_1 = R_2 \cdot ({U_{in} \over U_{out}} - 1) = 5.6 kΩ \cdot ({5 V \over 2 V} - 1) = 8.4 kΩ\)

In case of a resistance based sensors, we can then use the resistance to calculate the actual measurement value, e.g. in degree Celsius in case of a temperature sensor. A conversion formula, table or graph is usually given in the datasheet of the sensor. In the next tutorials, we will take a look at temperature dependent resistors, so-called thermistors.

There are still some open questions: How to choose the value of the second resistor and why did I say that it is not wise to use a potentiometer like in the example circuit? In a way, both questions belong together. The short answer to the first question: Pick a resistance equal to the expected sensor resistance. In our case, we used a 10 kΩ potentiometer as variable resistor, which means we get resistances from 0 kΩ to 10 kΩ. The mean resistance value is 5 kΩ. For this reason I used a 5.6 kΩ resistor for \(R_2\). But why this resistance and not a totally different one?

When we use a potentiometer as voltage divider we get a nice linear correlation between voltage and resistance. This is also true for potentiometers with logarithmic taper. The difference between linear and logarithmic potentiometers is, that the resistive material is unevenly distributed on logarithmic potentiometers and thus the correlation between the wiper position and resistance is non-linear. The correlation between resistance and voltage is still linear. If we look at the voltage divider formula and think about how potentiometers are constructed, we can see why.
The reason lays within the fact that the overall resistance of the resistive track is unchangeable. \(R_1+R_2\), the sum of the resistance of the resistive material on the left and on the right side of the wiper, is a constant. For a constant input voltage \(U_{in}\) we can simplify the voltage divider formula to \(R_2 \cdot c = U_{out}\). We can see that output voltage is proportional to the resistance \(R_2\). It's purely linear correlation. The constant \(c\) can be calculated like this:

\(c = {U_{in} \over {R_1+R_2}}\)

In our resistance measurement circuit this is not the case. The sum \({R_1+R_2}\) changes as only \(R_1\) changes. The consequence is that we now get a non-linear correlation between the output voltage and resistance. The exact curve depends on the resistance range of our sensor and the value we chose for \(R_2\). Below you can see the graph for our example circuit. Play around with the parameters to see how the choice of \(R_2\) affects the voltage values.

In areas where the curve is flat we will run into problems, if we want to differentiate between resistance values close to each other as they all create roughly the same output voltage. In areas where the curve is steep, on the other hand, this is an easy task. Depending on the resistor configuration we are also not able to use the whole voltage range.

You may have noticed that the curve becomes more or less a straight line, if you pick a huge value for \(R_2\) . We can increase the linearity by doing so. A higher linearity is desirable because it allows us to measure all resistance values in our range with roughly the same accuracy. There is a problem with this, however. While we do achieve a high linearity, we are only able use small voltage range. The consequence is that we have a low sensitivity (voltage change per resistance change) and it thus becomes a lot harder to detect small resistance changes.

The ADCs in microcontrollers only have a limited accuracy. If we do not use the full voltage range of the ADC for our measurements, we get more inaccurate measurements throughout the whole measurement range. To keep it short: you get the maximum sensitivity if \(R_2\) is equal to the resistance of the sensor.

Usually, we decide for maximum sensitivity and not for linearity. On microcontrollers, we have the ability to calculate the correct resistance value in software, even if the curve is non-linear. Oftentimes the resistance curve of the sensor isn't linear either. We have to accept some non-linearity. If our overall sensitivity is not too bad we will still get decent results. Sometimes we also get lucky: in case of NTCs the non-linearity of the sensor and the voltage divider will compensate each other to a certain degree.

With that being said, you now, know the answer to the question of why we used a 5.6 kΩ resistor. We also know the answer to the question of why it is not smart to use a potentiometer in the way we did in this circuit. With the normal potentiometer circuit we get a nice linear curve, while in this circuit we have to worry about linearity and sensitivity.