Exercise: measuring an unknown resistor with Arduino

Goal


In this lesson you will learn to measure an unknown resistor with a voltage divider and an Arduino.


Task description:


In this task, an Arduino Uno measures an unknown resistance (Rx) using a simple voltage divider and Ohm's law. The circuit is made of a known resistor (Rp) and an unknown resistor (Rx), connected in series between the power supply (5V) and ground (GND). The Arduino measures the voltage at the point between these two resistors through the analog pin A0.


Goal:

Measure the value of an unknown resistor using the Arduino's analog input, and compute the current in the circuit using Ohm's law.

Video lesson: Measuring an unknown resistor with Ohm's law and an Arduino UNO board

Ohm's law


Ohm's law is one of the most important rules in electronics. It connects voltage (U), current (I) and resistance (R) in an electric circuit.

Formula

U = R ⋅ I

Simply put, the voltage equals the product of the current and the resistance.


□ What does this mean?

When electric current flows through a component (for example a resistor), it meets an opposition called resistance. For the current to keep flowing, a certain voltage is needed – similar to pushing water through a narrow pipe.

For example, if you have a 100-ohm resistor and a current of 0.1 A, the voltage across the resistor is:

U = 100 ⋅ 0,1 = 10 V

Rearranging the formula

From Ohm's law, any of the three quantities can be computed:

  • Current (I):
  • I = U / R
  • Resistance (R):
  • R = U / I
  • Voltage (U):
  • U = R ⋅ I

Why is Ohm's law important?

Ohm's law helps engineers and designers to:

  • predict how a circuit will behave
  • choose the right resistor for a component such as an LED
  • avoid damaging electronics with too much current

Whether you are building a simple LED circuit or working on complex electronics, you will use Ohm's law all the time.

The next picture shows an electric circuit with two resistors – one is known (Rp), and the other is unknown. If we know the supply voltage, we can measure the unknown resistance with Ohm's law. This can be done with an Arduino. The voltage from the Arduino board is 5V.
In the picture the resistors are connected in series, so the 5V is shared between them. For example, if the voltage across the known resistor is 3V, 2V remain up to 5V, so we conclude that U_A0 = 2V.
Measuring an unknown resistance with Ohm's law
Figure 1: Measuring an unknown resistance with Ohm's law

Measuring an unknown resistor with Ohm's law and Arduino


The picture above shows a simple voltage divider made of two resistors connected in series:

  • Rp – the known resistor (the reference resistor)
  • Rx – the unknown resistor we want to measure

The Arduino's analog input pin A0 is connected to the point where the two resistors meet. This pin reads the voltage drop across the unknown resistor Rx (the voltage between A0 and GND).

How it works


The circuit is powered from Vcc (5V) on the Arduino board. Since the resistors are connected in series, the same current (I) flows through both.

By Ohm's law and the rules for resistors in series:

I = const,    Rtotal = Rp + Rx

By Ohm's law:

  1. The total voltage is:
  2. Vcc = I ⋅ (Rp + Rx)
  3. The voltage across the unknown resistor (read on A0) is:
  4. UA0 = I ⋅ Rx  ⇒  Rx = UA0 / I
  5. So the current is:
  6. I = (Vcc − UA0) / Rp
  7. When this expression for the current is put into the formula for Rx:
  8. Rx = UA0 ⋅ Rp / (Vcc − UA0)

This final equation lets us compute the value of the unknown resistor Rx by measuring the voltage on the pin A0.

□ Notes

  • The analog input A0 gives a value from 0 to 1023, which corresponds to a voltage from 0V to 5V (when the board is powered over USB).
  • The analog reading is converted to a voltage like this:
    UA0 = analogRead(A0) ⋅ Vcc / 1023

This method is a practical example of Ohm's law and the voltage divider, which are often used in electronics to measure or reduce voltage.

Wiring diagram:

Measuring an unknown resistance – wiring diagram
Figure 2: Measuring an unknown resistance with Ohm's law – wiring diagram

Code in the Arduino IDE

// Arduino program for measuring an unknown resistor Rx with a voltage divider and Ohm's law

const int pin_A0 = A0;   // the analog pin A0 measures the voltage between the known and the unknown resistor
float U_A0;              // voltage on A0
float analogValue;       // raw value from A0 (0–1023)
float Rp = 220;          // value of the known resistor in ohms (Ω)
float Rx;                // computed value of the unknown resistor
const float Vcc = 5.0;   // supply voltage (usually 5V on the Arduino Uno)
float I;                 // computed current in amperes

void setup() {
  pinMode(pin_A0, INPUT);   // A0 is the input for measuring voltage
  Serial.begin(115200);     // serial connection at 115200 baud for following the results
}

void loop() {
  // reading the analog value from pin A0 (range 0–1023)
  analogValue = analogRead(pin_A0);

  // if the value is 1023, the calculation is skipped to avoid dividing by zero
  if (analogValue >= 1023) {
    Serial.println("Error: A0 has the highest value (5V). Rx cannot be computed.");
    delay(2000);   // pause before the next measurement
    return;        // the rest of the loop is skipped
  }

  // converting the analog value to a voltage (0–5V)
  U_A0 = analogValue * Vcc / 1023.0;

  // the unknown resistance by the voltage divider formula:
  // Rx = U_A0 * Rp / (Vcc - U_A0)
  // where:
  // - U_A0 is the voltage across Rx
  // - Vcc - U_A0 is the voltage across Rp
  // - Rp is the known resistor
  Rx = U_A0 * Rp / (Vcc - U_A0);

  // current by Ohm's law: I = (Vcc - U_A0) / Rp
  // this is the current through the voltage divider
  I = (Vcc - U_A0) / Rp;

  // printing the voltage on A0
  Serial.print("U_A0= ");
  Serial.print(U_A0);
  Serial.println(" V");

  // printing the current in milliamperes
  Serial.print("I= ");
  Serial.print(1000.0 * I, 2);   // converting to mA, printing with 2 decimal places
  Serial.println(" mA");

  // printing the computed unknown resistance
  Serial.print("Rx= ");
  Serial.print(Rx);
  Serial.println(" ohm");

  delay(2000);   // 2-second pause before the next measurement
}
Result:
Measuring an unknown resistance – the result of running the program
Figure 3: Measuring an unknown resistance with Ohm's law – the result of running the program

A note about the formula for the voltage on A0


The formula U_A0 = analogRead(A0) × Vcc / 1023 is correct. Some sources divide by 1024 instead, because the readings go from 0 to 1023 (that is 1024 different levels). The difference is small, but it can matter for very precise measurements.


Limits of accuracy


• Tolerance of the known resistor: ordinary resistors often have a tolerance of ±5 %, which can add an error of a few ohms.
• Accuracy of the Arduino's AD converter: a 10-bit AD converter usually has an uncertainty of ±2 LSB (least significant bits), which can change the result by a few more ohms.

How to physically connect the components to the Arduino


For this experiment you need to build a simple voltage divider. Here is how to connect the components correctly on a breadboard:

Required components


  • 1 × Arduino UNO board
  • 1 × known resistor (Rp) – e.g. 220 Ω or 1 kΩ
  • 1 × unknown resistor (Rx) – any resistor you want to measure
  • jumper wires
  • 1 × breadboard

Wiring instructions


Follow these steps to make the correct electrical connections:

  1. Connect the reference resistor (Rp):
    • Connect one leg of the known resistor to the Arduino's 5V pin.
    • Connect the other leg of this resistor to a row of the breadboard where it will meet the unknown resistor.
  2. Connect the unknown resistor (Rx):
    • Put one end of the unknown resistor in the same row of the breadboard as the other leg of the reference resistor (that way they are connected in series).
    • Connect the other end of the unknown resistor to the Arduino's GND.
  3. Connect the analog input (A0):
    • With a jumper wire, connect the point where Rp and Rx meet to the pin A0 on the Arduino.
    • This is the point where the voltage drop across the unknown resistor is measured.
  4. Power and ground:
    • The Arduino must be powered over USB or from an external source.
    • The 5V and GND pins are used to power the voltage divider.
Measuring an unknown resistance – how to connect the wires
Figure 4: Measuring an unknown resistance with Ohm's law – how to connect the wires

Troubleshooting and common questions


Here are some common problems and how to solve them:

  • No readings? → Check whether the connection to ground (GND) is missing or loose.
  • Always reading the highest value (1023)? → Likely cause: the reference and the unknown resistor are swapped or A0 is connected directly to 5V.
  • Readings jump or are unstable? → Possible causes are an unconnected ground, a bad contact on the breadboard or loose jumper wires.

Next steps and extensions


When you have successfully measured an unknown resistor, try one of the following extensions:

  • Use the same voltage divider principle to measure analog sensors, for example the LM35 temperature sensor.
  • Increase the accuracy by calibrating the measurement with a reference resistor with an exactly known tolerance (e.g. a 1 % metal film resistor).
  • Show the data:
    • Send the data over the serial connection to Processing and draw live charts.
    • Or show the results directly on a small OLED screen over I2C.

Practice tasks

Try to solve the task yourself first, and only then open the solution.

  1. For the known resistor Rp = 220 Ω, the Arduino measures U = 2.5 V on A0. What is Rx? And if U = 1 V? Compute it on paper with the formula Rx = Rp · U / (5 − U), and then check it with a program that uses a given voltage instead of a measurement.
  2. Besides the resistance Rx, compute and print the current in the circuit in milliamperes: I = (5 − U) / Rp.
  3. The measured value “jumps” a little. Compute Rx from the average of 20 measurements of the analog input and compare how much the result changes now.
Solution 1: checking the formula without measuring
// Rx = Rp * U / (5 - U); we give the voltage by hand to check the calculation
float Rp = 220;

void ispisi(float U) {
  float Rx = Rp * U / (5.0 - U);
  Serial.print("U = ");
  Serial.print(U);
  Serial.print(" V  ->  Rx = ");
  Serial.print(Rx, 1);
  Serial.println(" ohms");
}

void setup() {
  Serial.begin(115200);
  ispisi(2.5);    // we expect 220 ohms (equal resistors)
  ispisi(1.0);    // we expect 55 ohms
}

void loop() {
}
Solution 2: resistance and current
const int pin_A0 = A0;
float Rp = 220;

void setup() {
  Serial.begin(115200);
}

void loop() {
  float U = analogRead(pin_A0) * 5.0 / 1023.0;   // voltage across Rx
  if (U > 0.01 && U < 4.99) {                   // we avoid dividing by zero
    float Rx = Rp * U / (5.0 - U);
    float I = (5.0 - U) / Rp * 1000;            // current in mA (the same through both resistors)
    Serial.print("Rx = ");
    Serial.print(Rx, 0);
    Serial.print(" ohms, I = ");
    Serial.print(I, 1);
    Serial.println(" mA");
  }
  delay(1000);
}
Solution 3: the average of 20 measurements
const int pin_A0 = A0;
float Rp = 220;

void setup() {
  Serial.begin(115200);
}

void loop() {
  long zbir = 0;
  for (int i = 0; i < 20; i++) {
    zbir += analogRead(pin_A0);
    delay(5);
  }
  float U = (zbir / 20.0) * 5.0 / 1023.0;
  if (U > 0.01 && U < 4.99) {
    Serial.print("Rx = ");
    Serial.print(Rp * U / (5.0 - U), 1);
    Serial.println(" ohms");
  }
  delay(500);
}

Check your knowledge

Open the quiz

Quiz: the unknown resistor

1. What does Ohm's law say?




2. How are Rp and Rx connected in this exercise?




3. What does the pin A0 measure?




4. Why does the program skip the calculation when the reading is 1023?