RESISTANCE METER

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

RESISTANCE METER

Post by siliconchip »

hi im trying to make a simple resistance meter, i know nothing about these so looked on google to get an idea about them,it seems i need a voltage divider on an ADC channel with the bottom resistor as the test resistor to be measured and a fixed 10K resistor on the top of the divider, ive then attempted to make some code using floats as this seems to be the type of variable used ?? but im tying my self in knots trying to get the solution and because im using floats as variables i think i need to convert them to strings to display the result on a lcd ???................. HELP :oops: :oops:
Attachments
MVB.fcfx
(9.91 KiB) Downloaded 272 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

I can't comment on the hardware side of things - but a couple of things software wise:

1) Converting a float to a string is easy:
float.png
(2.92 KiB) Downloaded 4483 times
PrintNumber just takes an integer - so the value would just show the integer part (ie 3.124 will show as 3) - this is C converting from a float to an integer for you...

2) You need to move the calculation macro to after the read = ...GetByte - currently it just sets voltage3 to the initial value of read (probably 0) * 5/1024 etc) I would also specify your constants as floats (so 5.0/1024.0) - depending on the compiler - it might be treated as an int (and thus 0) before being converted to a float for the multiplication)

Hope that helps a bit.

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi martin thanks for the reply,ive moved the calculation box and things are happening but im trying to change variable voltage3 to a string but it comes up as invalid im using floattostring$(voltage3) ??

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

Hi,

What's coming up as invalid - ie is it compiling and something odd is displayed (NAN (Not a number), Odd chars or 'Invalid'), or is it failing to compile?

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

Hi
When im in the calculation box trying to change voltage3 from a float to a string ... floattostring$(voltage3) it comes up as invalid symtex error

Bob

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: RESISTANCE METER

Post by Steve001 »

Hi Siliconchip
siliconchip

hi im trying to make a simple resistance meter, i know nothing about these so looked on google to get an idea about them
I had a dabble with a resistance meter some time ago, I used the kelvin method and a constant current generator.

https://en.wikipedia.org/wiki/Four-terminal_sensing

http://www.ti.com/lit/ds/symlink/xtr110.pdf

For the constant current generator i made the range selectable between 1 Amp and 10 Amps
The current was messured by a LEM transducer

https://uk.farnell.com/lem/lts-6-np/cur ... p/2146860?

Make sure that you're PSU is stable and can supply the current without the voltage dropping under load !

Kelvin Clips are readily available on ebay - some are rubbish though

https://www.ebay.com/bhp/kelvin-clip

the voltage was measured by a programmable amplifier - attched

I hope this give you some idea's

Steve
Attachments
VoltageAdapter.jpg
VoltageAdapter.jpg (49.85 KiB) Viewed 11690 times
Success always occurs in private and failure in full view.

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

change variable voltage3 to a string but it comes up as invalid im using floattostring$(voltage3) ??
Can you post an image / chart for that - sounds like a typo / mismatched types error?

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi martin
ive attached a screen shot when trying to change a float to a string in the calculation box
Attachments
Doc2.docx
(146.98 KiB) Downloaded 257 times

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

You need to assign the return value to a string so:

str = FloatToString$(voltage3)

Where str is a variable defined as a string.

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi martin
i can now convert a float to a string but my calculations are way off when trying to find the test resistance, im using proteus to simulate the circuit :cry:

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

Hmm,

I tried:

R1 = (Vs * R2) / Vout - R2 Where R1 is the resistor I want to find - Vs is input voltage, R2 is 10000ohm and Vout is adc read voltage

Adc voltage = adc_read value * 5.0 / 1024. I've assumed input voltage (and ADC ref voltage) to be 5.0.

So: In a simulation only world - I subbed in 512 for adc read (should be 2.5v half of 1024 samples)
adc.JPG
adc.JPG (24.87 KiB) Viewed 11659 times
Which gives the value of 10000 for r1. I've renamed the values to better reflect what they represent...

There is another problem - you are using getByte to get ADC value - so this is only going to return 0..255 - so change this to GetInt.
I planned to use the UART to display the results - but just used the simulation mode of FC in the end.

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi
with regard my calculations ive finally worked it out but when i simulate with proteus the higher my test resistance the more it starts to become inaccurate only by 1 - 3 ohms though im only looking to measure a max of around 500R, how in my calculations could i make this become more accurate,id appreciate any help

cheers bob
Attachments
MVB.docx
(199.27 KiB) Downloaded 179 times
MVB.fcfx
(11.57 KiB) Downloaded 194 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

HI MARTIN
just seen your reply as i posted i think ive nearly cracked it but im looking for a bit more accuracy i put 2 10K resistors in series as a start point then put a low test value in and did ohms law to confirm what i was seeing was correct and im now not far off the mark

cheers bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi all
ive gone as far as im able with my calculations and would like some advice if its possible to tweak them to make reading a resistance more precise rather than a couple of ohms out,any help or advice would be great fully received thanks in advance

bob

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi all,
ive now made a hardware version which is showing different results in real life rather than those when simulating through proteus, when measuring for example a 270 ohm resistance the lcd shows several different values ranging from 209 to 238 to 270 to 281 ohms repeatedly and not settling on or near 270 ohms thats on test has anyone any suggestions how i can make the lcd more stable thanks in advance

bob
Attachments
MVB.VERSION 2 fcfx.fcfx
(13.82 KiB) Downloaded 180 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RESISTANCE METER

Post by Benj »

Hi Bob,

Your calling the ADC function GetInt but storing the value into a byte, ideally this should be stored into an unsigned Int type variable or you will loose the upper bits of the reading.

You then convert the reading into a Voltage (voltage2), why not use the ADC function GetVoltage instead, this will allow you to read the ADC value directly as a floating point voltage.

I mention this because this line looks wrong.

voltage2 = (voltage1 / 1023) * 5

Probably should look like this instead.

voltage2 = (voltage1 / 1023.0) * 5.0

Any other literal constants used for floating point calculations should also be typecast as floating point by adding the decimal point.



The value will bounce around as the ADC reading bounces around. To avoid the bouncing around you need to perform multiple samples and do an average.

You could for example set a floating point value to 0. Sample the ADC 10 times each time adding the result to your floating point value. Finally divide by 10.0.

Here's a quick example.
MVB.VERSION 2 fcfx-1.fcfx
(14.1 KiB) Downloaded 200 times

On your hardware what tolerance of resistor have you used in your potential divider? 10%? 5%? 1%? 0.1%? The higher the value the less accurate your readings will be.

Also have you measured your supply voltage, is this bang on 5V or is it slightly over or under?

If you measure the voltage and resistance value and plug these into your calculation then this might improve things.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi benj
thanks for the reply, my power supply is stable and resistor tolerances are 5%, but im convinced my problem is in my calculations, ive tried your suggestions to the best i can but i still cant get the screen to settle and im slightly out my depth doing sampling at this point, i justcant get my head round this at the moment aaaaarrrrgggghhhhh lol :oops: :oops:

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

One suggestion - output some debug information to the UART.
This will allow you to see what is happening in a bit more detail than on the LCD - where things keep getting overwritten.

For example in Ben's example above - input voltage1 (voltage returned by ADC)
& voltage2 - current sum of input voltages (over 10 samples)
in the loop - where the samples are taken.

I - (5.0 - voltage2)
R - voltage2/I
in the outer loop - where the calculation occurs.

etc

This (hopefully) will show where the error lies and if the ADC returned voltage is fluctuating wildly or if the calculation is going astray...

Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi martin and benj
thanks for the reply ive tried to debug and understand whats going on step by step watching the variables but i cant grasp the problem, as for UART im not familiar with this as im still a bit wet behind the ears, maybe this project is a bit to advanced for me but i dont want to give up but the more i try the more im tying myself in knots, i think im close with what ive done so far but the light is not quite at the end of the tunnel, flowcode is an addictive drug :P :P time to take a break my head hurts lol :shock: :shock: :shock:

bob

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: RESISTANCE METER

Post by mnf »

Sometimes it's good to take a break and sleep on it!

Computers are good at doing simple tasks - very fast! So in Ben's code - the ADC is read 10 times with a 5ms delay between each reading - so 50ms per 10 times (so still ~20 times per second - okay a little slower as the ADC read takes some time) The output is then displayed with a 500ms (half second display) before repeating - this is still pretty fast!

Try a larger delay - say 5 seconds...

The UART component outputs information to a serial (usually converted to USB) port - on your PC. You can read (and indeed transmit) data to the MCU using a terminal emulator such as 'putty' You'll need the COMMS pack (which is a pity - I'd call this an essential part of FC and probably should be in the base pack) - except for Raspberry Pi - where there is a terminal option under tools... You can output - strings, numbers etc (you'll need "\r\n" - as a newline) - I would recommend trying it - it makes output of debug info a lot easier!

Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RESISTANCE METER

Post by Benj »

You'll need the COMMS pack
Not necessarily, in v8 anyway. You need the Comms pack for the UART component but the UART CAL component under Tools is completely free and will likely do what you need :D

It won't handle strings so you might need to make a loop to cycle through the string characters and output a byte at a time.

Other CAL components are free too to try and encourage users to create their own components without having to pay for a license.

In v7 the CAL components are licensed and you will need the Comms pack to work with a UART.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RESISTANCE METER

Post by Benj »

i still cant get the screen to settle
Are you holding or touching the wires? If so then your body will be introducing noise into the ADC input.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: RESISTANCE METER

Post by medelec35 »

Hi Bob,
Do you want to see if the attached flowchart works any better for you?
Unable to test on hardware.

The nearer the pull-up resistor value is to test resistor then the more accurate the results would be.
best to measure pull-up with an accurate meter first, then change

Code: Select all

PullUpResistance = 1000
to the correct value
Attachments
Resistance Meter 1.fcfx
(12.62 KiB) Downloaded 178 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: RESISTANCE METER

Post by siliconchip »

hi martin
been working on this a fair bit today and changed from bytes to Uint to string etc on the AN0 input and tested on hardware and found the Uint works best rightly or wrongly ?? also my original intention was only to measure a max of around 500 ohm,therefore instead of using a 10K resistor i decided to use a 1K resistor and used a fluke to measure its actual resistance which was 1003 ohm i then changed the calc to reflect this my test resistor was a 270 ohm measured at 266 ohms now when powering up in hardware my display changes between 263.4 to 265.8 ohms and stays on 265 ohms most of the time so it seems that by using a smaller resistor closer to that you want to measure the accuracy improves, both my resistors are as close as can be and im not touching them when testing, i will look at your flowchart martin and try it on hardware and report back and although not yet what i would call 100% not far off, one other question can i use a buck convertor to power this from a battery for a stable power supply thanks in advance

bob

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: RESISTANCE METER

Post by medelec35 »

Don't know how well its going to work for you as I'm using a different formula.
I'm using

Code: Select all

R = PullUpResistor / (1023.0 / read * 1.0 - 1.0)
Resistance = FloatToString$ (R)
As you can see I'm not using any references to voltage.
So in theory you should be able to a supply between 3.6 and 5V and the formula will remain the same.
I don't see why you can use a buck converter so long as caps are used to filter high frequency ripple.
Martin

Post Reply