Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Problems with creating an ohm meter circuit


  • Please log in to reply
5 replies to this topic

#1 Kilhoffer

Kilhoffer

    New Member

  • Members
  • Pip
  • 3 posts

Posted 12 March 2011 - 07:50 PM

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

This is what the circuit looks like:
Posted Image

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 = (5 / 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 demonGeek

demonGeek

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationCanada

Posted 13 March 2011 - 01:31 AM

I believe that 3.3V is the maximum on the analog pins so 5V is going to max out the reading at 1023 all the time and skew the results.

When I tested at 3.3V I was getting much better data but there was still a wide variance so I hooked up an external AREF (connect the 3.3V line to the AREF pin) and switched on the external AREF in the code. I also averaged out the result and found that after a dozen or so samples it stabilized at close to the right value. Here's the code:

// Rev B boards use internal AREF by default: switch to external AREF
OutputPort arefSelect = new OutputPort((Cpu.Pin)56, false);

AnalogInput A0 = new AnalogInput(Pins.GPIO_PIN_A0);

const float vIn = 3.3F;
const float rKnown = 10000;
float rTotal = 0;
int samples = 0;

while (true)
{
	float vRead = ((vIn / 1024) * (float)A0.Read());
	rTotal += (rKnown * ((vIn / vRead) - 1));
	samples++;
	Debug.Print("Resistance: " + System.Math.Round(rTotal / samples));
	Thread.Sleep(1000);
}

[N+ firmware v4.1.1.0 ALPHA7]

#3 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 13 March 2011 - 07:06 AM

The circuit you have chosen is correct, but it is much more scholastic/theoretical than practical.
However, what demonGeek wrote is correct.

The circuit is not a good choice, at least for three reasons:
1) it is not linear (that is easy to see);
2) it is preferable having one of the R_test wire to the common ground (for several reasons);
3) the overall error can be high.

The most common alternative is to measure the R_test by a constant-current generator.
Let's say that generator make flowing 1mA through R_test, then the voltage across is V_test=I*R_test. In your example should be V_test=330mV and, since the ADC gives N=1024 for V_in=3.3V, the reading would be (around) 102 (=1024*330mV/3.3V).
The problem is that you must build the current generator that is not straightforward.

Another easier way you may try to implement is the Wheatstone bridge.
It needs two ADC inputs, but also gives a precise value of the resistance.

We may also add that for R-test values below 1 Ohm or above 10 Meg (indicative), there are other methods for measurement.

Cheers
Mario
Biggest fault of Netduino? It runs by electricity.

#4 Kilhoffer

Kilhoffer

    New Member

  • Members
  • Pip
  • 3 posts

Posted 20 March 2011 - 06:25 AM

Thanks for your responses, guys. I have to say, this circuit is not as reliable as I had hoped (for reasons pointed out by Mario), but I'm no electrical engineer so taking on the Wheatstone bridge may be out of my range of skills with this stuff right now. The truth is, I don't need anything all that accurate. I can deal with these fluctuations by collecting several and averaging them. @demonGeek: You mention using an external AREF in your reply. I'm not actually using a netduino, but a close cousin, the FEZ Panda. I posted on this forum because I thought my problem was with my logic and not the platform. I'll have to find out if the FEZ Panda works the same way in regards to hooking up an external AREF. Thanks to you both of you for your replies. They helped me out very much. -T

#5 Tecchie

Tecchie

    Member

  • Members
  • PipPip
  • 23 posts
  • LocationGermany

Posted 20 March 2011 - 10:11 AM

If your posted code is really the code you're using, i'm astonished that you get any reading printed at all.
Because

var voltage = (5 / 1023) * reading;

contains an integer division which always results in zero. And in the next line

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

there's an exception thrown because of the division by zero.

If you want to use floating point division, at least one operand as to be a float.

#6 demonGeek

demonGeek

    Advanced Member

  • Members
  • PipPipPip
  • 42 posts
  • LocationCanada

Posted 20 March 2011 - 07:44 PM

If you want to use floating point division, at least one operand as to be a float.


Tecchie is absolutely correct. I should have mentioned that I modified your original code to explicitly type the variables.

Personally I dislike using var unless there's no other choice. I prefer to make my code as explicit as possible because it leaves less room for mistakes.

As far as the AREF is concerned, I don't know how the Fez Panda works but the Netduino has an internal AREF (on by default) and an external AREF pin. It seemed to me that I got better results using the external AREF but I didn't really test that much so it might not be the case. Either way, you need to understand how the Fez Panda's AREF works otherwise the A/D conversion will be off.

- Adam




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.