Language: EN

lectura-de-una-resistencia-variable-con-arduino

Reading the value of a resistor with Arduino

In this post we are going to use Arduino to measure the electrical resistance of a device. Why is this interesting? Why not just use a multimeter? Well, the reason it is important is because many sensors provide their measurement by varying their resistance. In order to use these sensors (light, temperature), we have to be able to measure their resistance from Arduino.

Unfortunately in Arduino (in general, practically in no automatons) we do not have an input where we can directly measure resistances or electrical currents. The only thing we can measure is digital or analog voltage signals. But we can use these and a small setup to easily measure the value of an unknown resistance, by comparison with a known one.

In this post we are going to use the analog inputs intensively, so it is assumed that you are familiar with their use. If not, it is advisable to visit this post ”Analog inputs in Arduino”, where we saw how to use the analog inputs of Arduino.

Electrical diagram and setup

As we said, our processor cannot measure resistances or electrical currents. The only thing we can measure is voltages discretized through its inputs. However, we can take advantage of the analog inputs to easily measure the value of an unknown resistance, by comparison with another one that we will call calibration.

The setup we need is a simple voltage divider between the unknown value resistor and our calibration resistor. The electrical diagram we need is the following.

medir-resistencia-arduino-esquema

While the setup on a breadboard would be as follows.

medir-resistencia-arduino-montaje

Sometimes you will hear this resistor incorrectly called a Pull-Down resistor because they occupy similar places in the electrical setup, as we saw in the post ”Reading a button with Arduino”. However, the functionality of these resistors is different, so make me happy and call it the calibration resistor.

Code example

The code needed to perform the reading is simple. We simply read the voltage value through the analog input, and use the voltage divider equations to obtain the value of the measured resistance.

const int sensorPin = A0;
const int Rc = 1500;  // value of the calibration resistor

int V;          // stores the measured value
long Rsensor;      // stores the calculated resistance

void setup() {
}

void loop() {
  V = analogRead(sensorPin);    //perform the reading
  Rsensor = 1024L * Rc / V - Rc;   //calculate the value of the resistance

  //...we would do whatever we want by Rsensor

  delay(1000);
}

Try it online

You will notice that we have done all the calculations using integer arithmetic, intentionally avoiding using float type variables. The reason is that floating point operations take up a lot of memory and are significantly slower.

Whenever possible, avoid using floating point operations. There are multiple techniques to perform similar operations with integer arithmetic, without losing much precision.

The value of the calibration resistor

The only thing left is to choose is the value of the calibration resistor. The answer is not unique, and its value will depend entirely on the range of resistances that the sensor can adopt during its operation. In general, the value to choose will depend on:

  • It must be large enough to limit the current that passes through the sensor when it acquires its minimum value. For example, if the sensor can reach 0 ohms, the only resistor that will limit the current will be the calibration resistor.
  • It must be small enough compared to that of the sensor to limit the loss of precision of the measurement. For example, if the sensor resistance acquires the same value as that of calibration, we will be losing half of the available precision.

For your guidance, typical values of this resistor are usually 1k to 4.7k, although as we said it depends on the range of resistances of the sensor. You can use our voltage divider calculator to determine the value of the calibration resistor, the current it will withstand, and the loss of precision.

Download the code

All the code in this post is available for download on Github. github-full