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;
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 ?
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.
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.
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 ?
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.
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.