Resistor messaurment: Value fluctuating

Hi,
I am testing this tutorial: Arduino Resistance Measurement | Circuits4you.com
using a 1100 Ohm and 20k Ohm resistor. But the result is a bit disappointing and for me it is not clear why:

What I want to use this method later is to adjust a value (distance in millimeter) using 4 rotary switches without touching the source code every time.

Any suggestions? Thanks in advance

Please show your code (all code) and draw your schematic.
Without it there is no subject for discussion.

The code is:

/*
  Resistance Measurement - www.circuits4you.com
  Reads an analog input on pin 0, converts it to resistance, and prints the result to the serial monitor.
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1024.0);

  float I = voltage / 1000;
  float VRx = 5 - voltage;
  float Rx = VRx / I;
  Rx = (5 - voltage) / I;

  // print out the value you read:
  Serial.print("Resistance:");
  Serial.print(Rx);
  Serial.println(" Ohms");
  delay(3000);
}

Schematic as according to the website:

Which resistor are you measuring - R1 or R2?
What the resistance of the "known" resistor? On the scheme I can see two resistance values - 1.1k and 20k ?
Where you use it in the calculation:

How you powered your arduino? What is power supply and where do you connect it - to the USB, to VIN or to "5v" pin?

Both resistors are known. I just wanted to test this tutorial to understand how its working.
I use USB to power it. The Arduino is connected to a powered USB Hub

I just wanted to say that your formula is not for your resistor values. I hope that you understand it...

Try this simplest code and show the output in IDE monitor

void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1000);
}

@riedochs

Your topic was moved to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

It will help you get the best out of the forum in the future.

Thank you

1 Like

I suspect from the wildly varying values that one or more of your connections is loose. Do the results change if you gently wobble the components and connections around? It might help to post some photos of your setup.

In your formula, 'I' should be the current. You should calculate that current from a voltage over a known resistor in your circuit.

This line:
Rx = (5 - voltage) / I;
Can be removed from your sketch (it is already in the line before). Does not do any harm though...

I tested this code from b707

The Result looks much better:

11:06:14.035 -> 52
11:06:15.040 -> 51
11:06:16.018 -> 51
11:06:17.030 -> 52
11:06:18.039 -> 51
11:06:19.005 -> 50
11:06:20.026 -> 51
11:06:21.021 -> 51
11:06:22.033 -> 51
11:06:23.039 -> 51
11:06:24.042 -> 52
11:06:25.046 -> 50
11:06:26.040 -> 51
11:06:27.050 -> 51
11:06:28.031 -> 51
11:06:29.042 -> 52
11:06:30.023 -> 51

My setup. I also resettled all cables again:
setup

And... 1024*1.1/(20+1.1) = ....

I assume I need to add this. Where?
thanks

It is approx. 50 (exactly what you measure)...

You need to decide which resistor is your reference and which one is your object of measurement.
Then you need to rewrite this equation to solve the resistor you want to know...

1024*R1/(R1+R2) = analogRead(somePin)

Thanks.
For this Project I will stay with the sensor value.
I also noticed as soon I power the Arduino with 9V, i get very stable results.
Thank you all for helping. Next step is learning functions and building this:

Hi,

How have you been powering the UNO.
Where are you applying the 9V?

Thanks... Tom... :smiley: :+1: :coffee: :australia:

The black Power connector:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.