As far as I can tell you get 4.84V out with your setup and you might fry the pin as the maximum input voltage is 3.3V. You probably should have the resistors the other way around.
It won’t matter which way you put the resistors as you HAVE to drive the divider from the 3.3V rail as 0 - 1023 bits equates 0 to 3.3v as Geir said.
Keep the circuit as is but change to the 3.3V rail.
Cheers Ian
P.S. also use the scaling function to convert 0 - 1023 directly to 0 to 3.3 Volts. ( Saves the first calculation and is quicker in native code )
This is how I’d do it…
analogInput.SetLinearScale(0, 3300);
var knownVoltage = 3300;
var knownResistance = 10000;
var current;
while(true)
{
var voltage = analogInput.Read();
//var voltage = (knownVoltage / 1023) * reading; //use inbuilt function
var current = voltage / knownResistance;
var resistance = (knownVoltage - voltage) * current;
Debug.Print("Resistance: " + resistance);
Thread.Sleep(1000);
}
First, thank you for your responses. In case you can’t tell, I’m new to micro programming. I write business software for a living, which is far less low level and just completely different. Forgive the noob questions.
So, I’ve altered the code to reflect your suggestion and also moved over to the 3.3v rail. Makes sense why. But it still doesnt seem to work right.
Here’s the latest code:
analogInput.SetLinearScale(0, 3300);
var knownVoltage = 3300;
var knownResistance = 10000;
while (true)
{
var voltage = analogInput.Read();
var current = voltage / knownResistance;
var resistance = (knownVoltage - voltage) * current;
Debug.Print("Resistance: " + resistance);
Thread.Sleep(1000);
}
var reading = analogInput.Read();
var vOut = (3.3 / 3300) * reading;
var temp = (3.3 / vOut) - 1;
var r2 = 10000 / temp;
Also, make sure you have it hooked up like this:
3.3V-----10k-----R2-----Gnd
|
V
Analog Pin
And lastly, to get a more accurate reading you should probably average 5-10 readings on the analog input pin. This helps suppress some of the noise you might see from the power supply or other factors.
Make sure your scaling is correct. Use a meter to make sure you get the right scaling.
With scaling I mean not linear scaling. I mean the scale between the actual current provided and the volts read by FEZ. It’s explained in my blog post I posted above.
C# supports variants as of 3.0. I personally don’t like to use them because I feel it’s poor programming, you should know the types of everything you declare. It was introduced almost exclusively for use in Linq statements because Linq can return so many different types (arrays, integers, etc). Using them for other declarations (to me) is a bit on the lazy side, and makes code difficult to read and debug.
In terms of var for new c#'ers it may be important to add.
Var is neither variant, object, or dynamic. The compiler just infers the type at compile time (it says, ok, I know what type you want). So the type is static just like had you declared the type. It can not change type at runtime.
I agree with Ron, using var for buildin types reduces readability and inspection and does not really save you anything in that case.
I have read your blog post on reading external voltage. I have it bookmarked and it’s my next task after I conquer this one of reading resistance. Great write up and fantastic photos. Thanks for contributing the community the way you do. I’ve already learned a lot from you.
I did change it a bit. I’ve tried several iterations and even swapped out R2 with a few values.
IanR, thanks for your help. This puts me right within the range of tolerance I needed. I think I can safely put this little exercise to rest and move on to reading external voltage.
I’m back for another easy question. This is likely second nature to most of you, but this is all new to me. My next exercise is to measure the resistance, in ohms, that a potentiometer is applying to a circuit. The circuit itself is easy enough to hook up. The potentiometer has three leads and the middle one is connected to one of my analog inputs. I’m able to read the voltage while applying variable resistance. That part is easy enough. But what is the formula to derive the resistance being applied? Going back to basics, ohms law states that R = V/I. But in this case, I don’t have the value of “I”, do I? The voltage is 5V and I know how much that is cut down at various points by the potentiometer, but how do I know the current use in the equation above?
I apologize if this is too fundamental of a question for this forum, but I need to gain an understanding of this so I can move forward with my self-learning process.