Measuring an unknown resistor with Arduino and Ohm's law
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):
- Resistance (R):
- Voltage (U):
I = U / R
R = U / I
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.
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 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:
- The total voltage is:
- The voltage across the unknown resistor (read on A0) is:
- So the current is:
- When this expression for the current is put into the formula for Rx:
Vcc = I ⋅ (Rp + Rx)
UA0 = I ⋅ Rx ⇒ Rx = UA0 / I
I = (Vcc − UA0) / Rp
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
A0gives 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:
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:
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:
- 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.
- 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.
- 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.
- 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.
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.
- 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.
- Besides the resistance Rx, compute and print the current in the circuit in milliamperes: I = (5 − U) / Rp.
- 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?
Measuring an unknown resistor using Arduino and Ohm's law
Excercise Measuring un unknown resistor on arduino
Objective
In this lesson you will learn to measure an unknown resistor using a voltage divider and Arduino.
Task description:
This task uses an Arduino Uno to measure an unknown resistance (Rx) using a simple voltage divider and applying Ohm's Law. The system consists 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 node between these two resistors using analog pin A0.
Goal:
Measure the value of the unknown resistor using the analog input of the Arduino and calculate the current in the circuit using Ohm's law.
Video lesson: Measuring an Unknown Resistor with Ohm’s Law & Arduino UNO
Understanding Ohm’s Law
Ohm’s Law is one of the most fundamental principles in electronics. It defines the relationship between voltage (U), current (I), and resistance (R) in an electrical circuit.
The Formula
U = R ⋅ I
In simple terms, voltage is equal to the product of current and resistance.
□ What Does This Mean?
When electric current flows through a component (like a resistor), it faces some opposition called resistance. To overcome that resistance and keep the current flowing, a certain amount of voltage is needed — like pushing water through a narrow pipe.
For example, if you have a resistor of 100 ohms and a current of 0.1 A, the voltage across the resistor will be:
U = 100 ⋅ 0.1 = 10 V
Rearranging the Formula
Ohm’s Law can be rearranged to calculate any of the three quantities:
- To find current (I):
- To find resistance (R):
- To find voltage (U):
I = U / R
R = U / I
U = R ⋅ I
Why Is Ohm’s Law Important?
Ohm’s Law helps engineers and makers to:
- Predict how a circuit will behave
- Choose the right resistor for a component like an LED
- Avoid damaging electronics with too much current
Whether you’re designing a simple LED circuit or working on complex electronics, Ohm’s Law is a tool you’ll use all the time.
In the picture, the resistors are connected in series and then the voltage of 5V is distributed to the resistors. For example, if the voltage on a known resistor is 3V, it means that up to 5V, 2V remains, so we conclude that U_A0=2V.
Measuring an Unknown Resistor Using Ohm’s Law and Arduino
In the figure above, you can see a simple voltage divider circuit made of two resistors connected in series:
- Rp – a known resistor (reference resistor)
- Rx – the unknown resistor we want to measure
The Arduino’s analog input pin A0 is connected to the junction point between the two resistors.
This pin reads the voltage drop across the unknown resistor Rx (i.e., the voltage between A0 and GND).
How It Works
The circuit is powered by Vcc (5V) from the Arduino board. Since the two resistors are in series, the same current (I) flows through both of them.
According to Ohm's Law and the rules for resistors in series:
I = const, Rtotal = Rp + Rx
Using Ohm's Law:
- The total voltage is:
- The voltage across the unknown resistor (read on A0) is:
- Solving for the current:
- Substituting that expression for current into the formula for Rx:
Vcc = I ⋅ (Rp + Rx)
UA0 = I ⋅ Rx ⇒ Rx = UA0 / I
I = (Vcc − UA0) / Rp
Rx = UA0 ⋅ Rp / (Vcc − UA0)
This final equation allows us to calculate the value of the unknown resistor Rx by measuring the voltage at A0.
□ Notes
-
The analog input pin
A0reads a value from 0 to 1023, corresponding to 0V to 5V (if powered from USB). -
To convert the analog reading into a voltage:
UA0 = analogRead(A0) ⋅ Vcc / 1023
This method is a practical demonstration of Ohm’s Law and the concept of voltage dividers, which are widely used in electronics to measure or scale voltages.
The wires diagram is:
Arduino IDE-code
// Arduino sketch for measuring an unknown resistor Rx using a voltage divider and Ohm's law
constintpin_A0 = A0; // Analog pin A0 is used to read the voltage between known and unknown resistors
floatU_A0; // Voltage at A0
floatanalogValue; // Raw analog value read from A0 (0–1023)
floatRp = 220; // Known resistor value in ohms (Ω)
floatRx; // Calculated value of the unknown resistor
constfloatVcc = 5.0; // Supply voltage (usually 5V on Arduino Uno)
floatI; // Calculated current in amperes
voidsetup()
{
Serial.begin(115200); // Start serial communication at 115200 baud for monitoring results
voidloop()
{
analogValue = analogRead(pin_A0);
// Skip calculation if analog value is 1023 to avoid division by zero
if (analogValue >= 1023)// Skip calculation if analog value is 1023 to avoid division by zero
{
delay(2000); // Wait before next reading
return; // Skip the rest of the loop
// Convert analog value to actual voltage (0–5V)
U_A0 = analogValue * Vcc / 1023.0;
// Calculate unknown resistance Rx using 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 known resistor
Rx = U_A0 * Rp / (Vcc - U_A0);
// Calculate current using Ohm’s law: I = (Vcc - U_A0) / Rp
// This is the current through the voltage divider
I = (Vcc - U_A0) / Rp;
// Output voltage at A0
Serial.print("U_A0= ");
Serial.print(U_A0);
Serial.println(" V");
// Output current in milliamps
Serial.print("I= ");
Serial.print(1000.0 * I, 2); // Convert to mA and print with 2 decimal places
Serial.println(" mA");
// Output calculated unknown resistance
Serial.print("Rx= ");
Serial.print(Rx);
Serial.println(" ohm");
delay(2000); // Wait 2 seconds before next reading
Note on the Voltage Reading Formula for A0
The original formula
U_A0 = analogRead(A0) × Vcc / 1023
is correct. However, some sources divide by 1024 to account for the fact that readings range from 0 to 1023 (i.e., 1024 distinct levels).
The difference is small but can matter in high-precision measurements.
Precision Limitations
• Known resistor tolerance: Common resistors often have a ±5 % tolerance, which can introduce an error of several ohms.
• Arduino ADC accuracy: The 10-bit ADC typically has an uncertainty of ±2 LSB (least significant bits),
which can further affect the final result by a few ohms.
How to Physically Connect the Components to Arduino
To perform the unknown resistor measurement experiment using Arduino, you need to build a simple voltage divider circuit. Here's how to properly connect the components on a breadboard:
Required Components
- 1 × Arduino UNO board
- 1 × Known resistor (Rp) — e.g., 220Ω or 1kΩ
- 1 × Unknown resistor (Rx) — any resistor you want to measure
- Jumper wires
- 1 × Breadboard
Wiring Instructions
Follow these steps to create the correct electrical connections:
- Connect the Reference Resistor (Rp):
- Connect one leg of the known resistor to the 5V pin on the Arduino.
- Connect the other leg of this resistor to a point on the breadboard where it will be joined with the unknown resistor.
- Connect the Unknown Resistor (Rx):
- One end of the unknown resistor should connect to the same row on the breadboard as the second leg of the reference resistor (they form a series connection).
- The other end of the unknown resistor connects to GND on the Arduino.
- Connect the Analog Input (A0):
- Use a jumper wire to connect the junction point between Rp and Rx (where they meet) to pin A0 on the Arduino.
- This point measures the voltage drop across the unknown resistor.
- Power and Ground:
- Ensure the Arduino is powered through USB or external supply.
- The 5V and GND pins must be used consistently to power the voltage divider circuit.
Troubleshooting & FAQs
Here are some common issues you might encounter and how to resolve them:
- No readings? → Check for a missing or loose ground (GND) connection.
- Always reading maximum value (1023)? → Likely cause: Reference and unknown resistors are wired in reverse, or A0 is directly connected to 5V.
- Unstable or noisy readings? → Possible issues include a floating ground, bad breadboard contacts, or loose jumper wires.
Next Steps & Extensions
Once you’ve successfully measured an unknown resistor, consider trying the following extensions:
- Use the same voltage divider concept to measure analog sensors like the LM35 temperature sensor.
- Improve accuracy by calibrating your setup with a reference resistor of precisely known tolerance (e.g., 1% metal film resistor).
- Visualize your data:
- Send data via serial to Processing and generate live graphs.
- Or display the results directly on a small OLED screen using I2C.
Check your knowledge
Open the quiz
Quiz: unknown resistor
1. What is Ohm's law?
2. How are Rp and Rx connected in this exercise?
3. What does pin A0 measure?
4. Why does the program skip the calculation when the reading is 1023?
| Previous |< Arduino - Temperature sensor |
Next Arduino-Ultrasonic Sensor >| |
