Ohm meter - readadc

tjetson

Senior Member
Ok, so I wanted to make an ohm meter with the readadc command. I have some code, but it returns values that aren't expected. Here is my code:
symbol adcpin = 4
symbol adcval = b1
symbol voltoadc = b2
symbol voltognd = b3
symbol diff = b4
symbol ohms = b5


'********** Voltoadc calculations not yet working


ReadLoop:
readadc adcpin, adcval 'Read incoming voltage
voltoadc = adcval / 5666 / 100 'Convert it from a value between 0 and 255 to a value between 0 and 5
voltognd = 46/10 - voltoadc 'Figure out the voltage going to ground
diff = 10000 / voltognd 'Figure out the ratios
ohms = voltoadc * diff 'Ohms

'sertxd (#ohms," ohms",cr,lf) 'Transmit ohm value
sertxd ("ADC Value ",#adcval,cr,lf)
sertxd ("Volts to ADC pin ",#voltoadc,cr,lf)
sertxd ("Volts to ground ", #voltognd,cr,lf)
sertxd ("Ratio difference ",#diff,cr,lf)
sertxd ("Ohm value ",#ohms,cr,lf)
sertxd (cr,lf)

pause 4000

goto ReadLoop

It's entirely possible that my calculations are completely wrong; I probably need another person to look at my code. Thanks in advance.

PS my circuit goes like so:

+4.5v
|
|
[] Unknown resistor
|
--------------ADC Pin
|
[] 10k resistor
|
|
GND
 
Last edited:

MartinM57

Moderator
Your calculations may be correct but your implementation is faulty - remember PICAXE can only do integer maths, so

Code:
voltoadc = adcval / 5666 / 100
is guaranteed to result in 0 for any ADC value you read - any number between 0 and 255 divided by 56660 is less than 1 (in perfect maths) and so will be calculated as 0 by the PICAXE

You can prove this in the simulator or by sprinkling debug/sertxd commands in your code to print out the values of the intermediate variables EDIT - oh, you do that...I'm predicting voltoadc is always 0?

I'm sure others will jump in with a how-to for high precision maths on a PICAXE - it is quite a challenge...
 
Last edited:

hippy

Ex-Staff (retired)
I'm not sure an ohm-meter using READADC is going to be that accurate and useful but, on the principle of try it and see, I would suggest implementing a volt-meter first. That will be easier to test and debug and is the foundation for an ohm-meter.

Once you've seen the problems and issues with that and find how to work round them you will be able to apply that knowledge learned to an ohm-meter.
 

tjetson

Senior Member
Code:
voltoadc = adcval / 5666 / 100
is guaranteed to result in 0 for any ADC value you read - any number between 0 and 255 divided by 56660 is less than 1 (in perfect maths) and so will be calculated as 0 by the PICAXE

If for example we have the number 125:
5666/100 = 56.66
125 / 56.66 = ~2.2

That is the type of answer I want, but not the one I get.


EDIT: If you got the 56660 from the line where I multiplied by 10, that was just a test to see what values I got back; I forgot to delete that line. It is now deleted.
 
Last edited:

westaust55

Moderator
Have have to remember that the PICAXE firstly only has integer maths and secondly the maths are perofrmed in the order given (there is no precedence)

so with the line:
voltoadc = adcval / 5666 / 100

lets firstly consider that you are using READADC12 so the max/highest value you can receive is 1023

voltoadc = 1023 / 5666 / 100

first part is 1023 / 5666 which with the PICAXE gives the result = 0

imaging what happens with any value of READADC less that 1023 . . . .

that is what MartinM57 was trying to highlight



I just did a few maths and the results would be all very non linear and very low on accuracy.
 

hippy

Ex-Staff (retired)
Due to the way the PICAXE does maths - left to right, no precedence, integer only ( as noted above ) you sometimes have to 'get creative' with maths.

To achieve "N / 56.66" you can implement that as "N * 100 / 5666". You have to watch for intermediate values ( eg, N*100 ) exceeding 65535 or overflow occurs and the result will not be as expected.
 

MartinM57

Moderator
If for example we have the number 125:
5666/100 = 56.66
125 / 56.66 = ~2.2

That is the type of answer I want, but not the one I get.

EDIT: If you got the 56660 from the line where I multiplied by 10, that was just a test to see what values I got back; I forgot to delete that line. It is now deleted.
No - I got the 56660 by making a mistake myself - I meant 566600...

5666/100 is indeed 56.66
125/56.66 is indeed ~2.2

...but 125 / 56660 / 100 = 0.000022 using conventional maths ;) You would have to put 125 / (5666 / 100) to achieve what you've described, but you can't do that in PICAXE Basic as brackets aren't allowed
 

premelec

Senior Member
constant current

tjetson - if you use a constant current source and R unknown to V- the whole thing gets much easier - however you'll always have accuracy problems with large R values with the max 10K input to ADC for full accuracy...
 

boriz

Senior Member
Yeh. Constant current source. Automatically switchable for different ranges to maintain accuracy. Thought about building one myself. Auto power on/off. No buttons. Simple pair of contacts on the front. Instant resistor value identification/confirmation.
 

tjetson

Senior Member
tjetson - if you use a constant current source and R unknown to V- the whole thing gets much easier - however you'll always have accuracy problems with large R values with the max 10K input to ADC for full accuracy...
So do you mean this:

+5v
|
|
10k
|
|
----------------ADC
|
Unknown
|
|
GND


Also, how then would I modify my calculations?
 

MartinM57

Moderator
'fraid not - you have a constant voltage source there, so the current through the 2 resistors (ignoring fantastically small current into the ADC input) depends on the sum of the 2 resistances.

In simple terms, a constant current source is a slightly weird device that will force the same current around the circuit whatever resistance is connected to it. This means that the voltage across the resistance will vary.

So all you would need is a constant current source, your resistor under test and the means to measure the voltage across it

Constant current source (say 1mA)
|
|
|
|
|
----------------ADC
|
Unknown
|
|
GND

Say the constant current source was 1mA:
- if your resistor was 1000R then the voltage across it would be 1/1000*1000 = 1 volts - nice and easy to measure with the ADC input
- if your resistor was 2000R then the voltage across it would be 1/1000*2000 = 2 volts - nice and easy to measure with the ADC input
- if your resistor was 10000R then the voltage across it would be 1/1000*10000 = 10 volts - oops - not so easy to measure with the ADC input as it is over 5 volts.

If you try the sums yourself (it's only V=I*R - ohms law) then you will see difficulties at low and high resistances with a fixed constant current source - and you will see that you need to have different constant currents for different resistaces to make a nice PICAXE ADC friendly input to measure (more than a few 10's of mV, less than 5v).

...hence boriz's idea for an autoranging constant current source that adjusted itself to put 0-5v across the resistor under test - which is easy to write down in a sentence on a forum, but a bit harder to build :)

Also you would need to design/find a constant current circuit to build.

Sorry - we have turned you ohm-meter into what might look like an impossible project...but actually it is quite a hard project. To do a constant current solution you need a good understanding of voltage, current and resistance first, then some circuit finding/designing skills, and then some PICAXE skills as well.
 
Last edited:

hippy

Ex-Staff (retired)
I still stand by my suggestion to build a volt-meter first. A simple pot between +V and 0V and a meter on ADC input and 0V will veirfy the code is indicating what the meter does.

That will show the limitations, complexities of coding to determine an accurate conversion, the maths involved and means to solve that. Once the maths and technique is understood further development of the idea should be much easier.

An alternative to constant current generation, and likely cheaper and easier, is using a resistor-capacitor charge circuit; timing how long it takes to charge / discharge a known C and determining what the R is from that. Developing a volt-meter first won't help there though, other than in getting used to handling PICAXE maths.

One would get an ohm-meter and a capacitance meter though.
 

MartinM57

Moderator
...also a 10GBP multimeter would have a "ohm-meter" on it (as well as all the other useful things).

What do you actually need it for - unknown R's go into my bin or onto my multimeter. At 0.01-0.02GBP each for new ones they're not really worth worrying about too much.

Might be worth mentioning why you are wanting to do this and some idea of the range of R's you want to measure and to what accuracy - 0.01R to 500M at 0.1% accuracy might be a bit of a challenge ;)
 

tjetson

Senior Member
Well, the whole point of this was (I assumed) a nice simple project, just for the heck of it I guess. Now I can see it won't be all that simple, and I am better off just reading the resistors (I have the numbers/colours commited to memory).

Thank you all for your help
 

hippy

Ex-Staff (retired)
It's often the case that 'simple' projects turn out to be much more complex and challenging than first realised. It gives an appreciation as to why some products seem expensive for what they do and conversely make one marvel at how some are so cheap - usually achieved by economy of scale.

It's hard to compete against cheaper and easier solutions ( reading resistor bands, a resistor colour wheel, online resources or a commerical meter ), but there is the fun and knowledge gained in trying for oneself.

On finding relatively easy but worthwhile projects for entertainment, fun or learning that can often be a challenge. When one finds such a thing, that it could be done cheaper, easier or better, often isn't a factor.
 

boriz

Senior Member
“...which is easy to write down in a sentence on a forum, but a bit harder to build...”

Of course. But not much. Here’s how I was planning it...



For a detailed explanation, Google ‘current mirror’. But basically any component placed between J3 and J4 can only draw, as a maximum, the current provided by Q1, and Q1’s current is ‘programmed’ by the current through Q2, and Q2’s current can be set using a simple resistor to ground. Or in this case, a resistor to a Picaxe output.

Here I’m using two resistors to allow autoranging. With R1=4300R and R2=478R, you can get a 10mA range and a 1mA range. With both J1 and J2 connected to Picaxe outputs, and both set high, no current is provided by the mirror.

With J2 low the current mirror provides 1mA, and when your measurement requires it, also switch J1 low. Then the current mirror is providing 10mA.

Using the same basic method and mutiple parallel/series combinations, any currents can be programmed.

The resistor values need to be exact and will require parallel combinations, or presets to get them right. Also note, I made a small error in the resistor calculations, can you spot it?
 

tjetson

Senior Member
“...which is easy to write down in a sentence on a forum, but a bit harder to build...”

Of course. But not much. Here’s how I was planning it...



For a detailed explanation, Google ‘current mirror’. But basically any component placed between J3 and J4 can only draw, as a maximum, the current provided by Q1, and Q1’s current is ‘programmed’ by the current through Q2, and Q2’s current can be set using a simple resistor to ground. Or in this case, a resistor to a Picaxe output.

Here I’m using two resistors to allow autoranging. With R1=4300R and R2=478R, you can get a 10mA range and a 1mA range. With both J1 and J2 connected to Picaxe outputs, and both set high, no current is provided by the mirror.

With J2 low the current mirror provides 1mA, and when your measurement requires it, also switch J1 low. Then the current mirror is providing 10mA.

Using the same basic method and mutiple parallel/series combinations, any currents can be programmed.

The resistor values need to be exact and will require parallel combinations, or presets to get them right. Also note, I made a small error in the resistor calculations, can you spot it?
This is a very interesting concept I shall have to peruse further; thank you for sharing it.
 

boriz

Senior Member
You’re welcome. But it’s an old idea. Used commonly in analogue electronics. In fact most analogue ICs will contain several current mirrors.

Take a look at the internal circuitry for the humble 741 op-amp. About three quarters of the way down this Wikipedia page. You can see the current mirrors highlighted with red dotted lines.

Current mirrors are dead handy for lots of stuff and should definitely be in your ‘stable’ of circuit parts. EG: Using 4 Picaxe outputs and a four bit current mirror, you can have a simple DAC with 16 levels. 32 levels with 5 outputs, 64 levels with 6 outputs etc..
 

BeanieBots

Moderator
Current mirrors are a fairly standard circuit but be aware that they require very close matching of the transistors which is why they are more commonly seen within IC circuits rather than as descretes. You can get matched pairs (often in a single package) or you could hand select the devices yourself from a batch.
 
Top