Ohm's Law.

int analogPin = 0;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int raw = 0;           // variable to store the raw input value
int Vin = 5;           // variable to store the input voltage
float Vout = 0;        // variable to store the output voltage
float R1 = 10;         // variable to store the R1 value
float R2 = 0;          // variable to store the R2 value
float buffer = 0;      // buffer variable for calculation

void setup()
{
  Serial.begin(9600);             // Setup serial
  digitalWrite(13, HIGH);         // Indicates that the program has intialized
}

void loop()
{
  raw = analogRead(analogPin);    // Reads the Input PIN
  Vout = (5.0 / 1023.0) * raw;    // Calculates the Voltage on th Input PIN
  buffer = (Vin / Vout) - 1;
  R2 = R1 / buffer;
  Serial.print("Voltage: ");      //
  Serial.println(Vout);           // Outputs the information
  Serial.print("R2: ");           //
  Serial.println(R2);             //
  delay(1000);
}

While not strictly a programming question hence why i'm posting it here..

Now, i'm a little confused here...

  Vout = (5.0 / 1023.0) * raw;    // Calculates the Voltage on th Input PIN
  buffer = (Vin / Vout) - 1;
  R2 = R1 / buffer;

I simplified it down to this...

5v --------~100ohms~----------(2.5v)-----------(Unknown Resistor)------------GND

Vout = (5/2.5)-1 = 1
Unknown Resistor = 100/1 = 100

am i missing something or is that correct?

If some fixed and known resistor is 100 Ohms, the power supply is a stable 5 volts and a measured value at the joint of the two resistors is 2.5 volts, then the unknown resistor is 100 Ohms indeed.

What voltage will you see when the unknown resistor happens to be 47K Ohms ?

am i missing something or is that correct?

Ohm's law is properly used

  Vout = (5.0 / 1023.0) * raw;    // Calculates the Voltage on th Input PIN
  buffer = (Vin / Vout) - 1;
  R2 = R1 / buffer;

is correct. What's the problem?

Regards

No no all is good... ohms law was taught to me via the triangle method

V
IR

Cover one to get the other, I've never used Ohms/Volts only Volts/Ohms so that triangle was lying....

{ sad, resigned sigh}Vout = (5.0 / 1024.0) * raw;

I have my doubts about whether that denominator should be 1023 or 1024, I have seen the arguments for both and neither are convincing. It really comes down to a question about exactly how the a/d converter works.

As for the OP's question, I am assuming that Vin is the 5V and Vout is the voltage measured by the a/d converter attached at the point between the two resistors. If R is the unknown resistor, then

Vout = 5V * ( R / ( R+100) )

therefore

Vout/5V = R / (R+100) so 5V/Vout = (R+100)/R = 1 + (100/R)

so (5V/Vout)-1 = 100/R

so R = 100/((5V/Vout)-1) which is the same formula as the one in the code which you posted.

Make sure you consider the cases which could result in division by zero.

I have seen the arguments for both and neither are convincing.

Ask yourself "why do semiconductor manufacturers go to the expense of creating a precision 1.024V reference?"

Does that help?

I'll go with the 1.024V reference.
Assuming that 00 0000 0000 is 0V then the divisor must be 1023.
(Vref / 1023 = volts per step)

If it was a 2-bit ADC then you'd divide the reference by three.

I'm showing my work (see attached.)

IMG_1723.jpg

Your assumption is wrong. 00 0000 0000 is not zero volts. It is a voltage range which includes zero. That is the way a successive approximation converter works.

OK.
It should not be seen as a DAC, where "0" would yield 0V.
I understand the error of my ways.

The ADC resulting "0" implies everything in the range of 0 to 1mV (1.024V/1024)
0x3ff is anything between 1.023 and 1.024V

With 5V then "0" is the range of 0 to 4.88mV and 0x3ff is the range of 4.9951 to 5V.

Exactly.

With 5V then "0" is the range of 0 to 4.88mV and 0x3ff is the range of 4.9951 to 5V.

Well, no, not "exactly".
1023 is 4.9951V or more.

To make things interesting, what if this sketch is also using a bunch of LEDs to show some status.
So the "5 volts"is actually more like 4.6 volts..
Where's the reference then ?

AWOL:
{ sad, resigned sigh}Vout = (5.0 / 1024.0) * raw;

Not my code, copy and paste job.

cjdelphi:
No no all is good... ohms law was taught to me via the triangle method

V
IR

Cover one to get the other, I've never used Ohms/Volts only Volts/Ohms so that triangle was lying....

Noone's using ohms/volts! buffer is a pure number ratio, so we have ohms/number.
In other words the resistance ratio is the same as the voltage ratio.

MarkT:

cjdelphi:
No no all is good... ohms law was taught to me via the triangle method

V
IR

Cover one to get the other, I've never used Ohms/Volts only Volts/Ohms so that triangle was lying....

Noone's using ohms/volts! buffer is a pure number ratio, so we have ohms/number.
In other words the resistance ratio is the same as the voltage ratio.

.

Good! I thought I was going mad ..... thank you clearing that up.

Quote
I have seen the arguments for both and neither are convincing.
Ask yourself "why do semiconductor manufacturers go to the expense of creating a precision 1.024V reference?"

Does that help?

Not really. No. It's a hardware issue. We know that over the range of possible input voltage, the 1024 possible fixed output levels looks like a staircase if drawn on a graph. There are several different ways that the graph can be drawn, sliding it the right and left a little.

Furthermore, with 1024 output levels, there are only 1023 vertical steps between them. If 0 == 0V and 1023 == 5V, then the height of each step is ( 5V - 0V ) / 1023.

If 0 == 0V and 1 == 5 mV, then at what input voltage does the transition from an output count of 0 to an output count of 1 occur ?

at 0.0000001 V ? at 2.5 mV ? at 4.9999 mV ?

and at what input voltage does the transition from 1022 to 1023 ( the highest possible value ) occur ?

If I draw a diagonal line on my graph which touches each step at the outer edge of each tread, or the inner edge of each tread, which of those lines intersects the origin ?

The answers to these questions would show whether a denominator of 1023. or 1024. is appropriate. I don't see how your precision voltage reference is relevant, at all.

Furthermore, if you have a D to A converter which creates an output voltage which increases by x volts for each count on the input, then your range of output voltages is

0 , 0+x, 0+2x, 0+3x, 0+4x ..... 0+1022x, 0+1023x

That's it. 1024 different possible output values.

If you adjust your device so that 0+1023x is equal to 5V, then x, which is the step voltage change for each count, must be 5V/1023

On the other hand, you could choose to adjust your device so that the maximum possible output voltage is 4.948 V. If you wanted to. And then, you could say that x = 5V/1024

But then, you could never actually achieve a 5V output, because to do so would require an input to the D/A converter of 1024, which is a number than you cannot provide if it is an unsigned 10 bit number.

We could do the reductio ad absurdum argument again for the nth time, but I really can't be bothered.
Believe what you want.

michinyon:
But then, you could never actually achieve a 5V output, because to do so would require an input to the D/A converter of 1024, which is a number than you cannot provide if it is an unsigned 10 bit number.

Stick and wrong end.
First you never achieve an output I assume you mean input.

Second an A/D does not convert a voltage into a number. It simply gives you an idea of between what two steps in the input range your voltage lie. The number of these voltage steps is 2n where n is the number of bits. The space between these steps is 2n / Vref

You are getting totally hung up on thinking this constitutes a measure in the analogue sense.