Language: EN

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

Reading the value of a resistor with Arduino

In this entry, we are going to use Arduino to measure the electrical resistance of a device. Why is this interesting? Why not just use a multimeter and be done with it?

Well, the reason we want to measure resistances is that many sensors provide their measurement through a variation in their resistance. To use these sensors (light, temperature), we need to be able to measure their resistance from Arduino.

Unfortunately, in Arduino (in general, practically in no automaton), we do not have an input to directly measure resistances. The only thing we can measure are digital or analog voltage signals.

But we can use these, along with a small setup that we are going to create, to easily measure the value of an unknown resistance by comparing it with a known one.

Electrical schematic and setup

As we said, our processor cannot measure resistances or electric currents. The only thing we can measure are discretized voltages 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. We will call the known resistance calibration.

The setup we need is a simple voltage divider between the resistance of unknown value and our calibration resistance. The electrical schematic we need is as follows.

medir-resistencia-arduino-esquema

While the assembly on a breadboard would be as follows.

medir-resistencia-arduino-montaje

Sometimes you will hear this pull-down resistor incorrectly referred to because they occupy similar places in the electrical setup, as we saw in the entry Read a button with Arduino. However, the functionality of these resistors is different, so make me happy and call it calibration resistance 😙

Example code

The code needed to perform the reading is simple. We simply read the voltage value using 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 resistance

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 wanted with Rsensor

  delay(1000);
}

Try it online

You will notice that we have performed all the calculations using integer arithmetic, deliberately 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 resistance

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

In general, the value to be chosen will depend on:

  • Sufficiently large to limit the current that passes through the sensor when it reaches its minimum value. (For example, if the sensor can register 0 Ohms, the only resistance that will limit the current will be the calibration resistance).

  • Sufficiently small compared to that of the sensor to limit the loss of measurement precision. (For example, if the sensor’s resistance reaches the same value as the calibration, we will be losing half of the available precision).

As a guideline, common values for this resistance usually range from 1k to 4.7k, although as we said, it depends on the resistance range of the sensor.

You can use our voltage divider calculator to determine the value of the calibration resistance, the current it will support, and the loss of precision.

Download the code

All the code from this entry is available for download on Github. github-full