Main Site Documentation

Problems with creating an ohm meter circuit


#1

OK, I’m trying to create a circuit to read resistance utilizing a voltage divider.

This is what the circuit looks like.
http://i285.photobucket.com/albums/ll42/akilhoffer/Resistive_divider.png

Vin = 5v
R1 = 330 ohm (Resistance value we’re trying to read)
R2 = 10k ohm (Known constant resistance)

The code to achieve this looks like this:



var knownVoltage = 5;
var knownResistance = 10000;

while(true)
{
   var reading = analogInput.Read();

   var voltage = (knownVoltage / 1023) * reading;
   var resistance = knownResistance * ((knownVoltage / voltage) - 1);

   Debug.Print("Resistance: " + resistance);

   Thread.Sleep(1000);
}


My reading is way off and I’m sure it’s because of my logic above. Math is flawed somehow. Does anyone see what I did wrong?

Thanks in advance for any input or advice!
-T


#2

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.


#3

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);
}


#4

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. :wink:

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);
}

The values when executed are:

voltage = 141
current = 0
resistance = 0

Any idea why its so far off?


#5

Try:



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.


#6

If your circuit is right, the voltage generated will be 3195 not 141

3.3V / 10330R = .3194 uA ( current ).
.3194mA * 330R = 0.105V.

3.3V - .105V = 3.195V this should be the A to D reading!!

if you get a voltage 141 then the 10k resistor isn’t a 10k resistor or your ladder is upside down to your diagram.

if its upside down then .141 *.3194 = 441 ohms (the current is DEFINITELY .3194mA )

You will need a double to get precision as the current as you can see is milli amps (tiny)

And I apologise. but the resistance will need to be *10

Cheers Ian

@ Ron your equation should be

var r2 = 10000 * temp ( not divide, This works as well!!)

I know its probably due to tabs but your circuit will show 3.3v on the A to D all the time.

Lastly… I know the code shows Variants! I didn’t think c# supports variants! I use “double” or “single” for floating point.


#7

And this will get you going also:
http://robertjacobs.eu/2011/02/03/voltage-dividing-reading-battery-voltage/

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.


#8

Ian-

Thanks for the correction.

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.


#9

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.


#10

But!!! it does state that once declared it will not change so if declared into an int type, you won’t have floating point availability.

As I’m not sure on this automation… I’ll not use them…

Cheers Ian


#11

Hmmmm…somehow still not right. I must be missing something.

I have it wired like this:

3.3v-----10K-----|-----2.2K----GND
____________A0

I’ve updated the code as such:



            const double knownVoltage = 3300;
            const double knownResistance = 10000;

            var analogInput = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An0);
            analogInput.SetLinearScale(0, 3300);

            while (true)
            {
                double voltage = analogInput.Read();

                double current = voltage / knownResistance;
                double resistance = ((knownVoltage - voltage) * current) * 10;

                Debug.Print("Resistance: " + resistance);

                Thread.Sleep(1000);
            }


For some reason, I’m still getting inaccurate values:

Voltage (from A0) = 576.0
Current = .0576
Resistance = 1569.024

Anyone see what I’ve done wrong?


#12

@ Robert (Foekie):

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.

-T


#13

You have changed your circuit…

Right as the current is the same as the voltage (across the 10k) to the power of 10^-3

3.3V - 0.576V = 2.724V the current WILL be 0.0002724mA. So


double resistance = (voltage / (knownVoltage - voltage)) * knownResistance;

I was trying to keep it simple. You may need to change the scaling a tad as this yields 2.114k not 2.2k

Cheers Ian


#14

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.

Again, thanks for everyone’s help!


#15

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.

Thank you…


#16

If you have a set up like the image attached.

The current is constant 3.3v / 1k so its always 3.3mA

If you need to ascertain the current first you will need a known resistor.

Draw a picture of what’s required.

Cheers Ian