View previous topic :: View next topic |
Author |
Message |
AK
Joined: 20 Apr 2004 Posts: 33
|
4 to 20 mA converted to voltage |
Posted: Fri Dec 02, 2005 7:29 am |
|
|
Hi,
I have a laser distance sensor that outputs 4 to 20 mA. I am interfacing the sensor with a 16 bit A/D converter that has a voltage range of 0 to 1.024 Volts. I'm using a 51 ohm resistor to convert the current to voltage. This means I'll have a minimum voltage of .204 Volts and a max of 1.02 Volts. When I get a .204V reading that should be my 0 inches reading and when I get 1.02V reading that should be my 2 inch reading (maximum distance of the sensor). I'm having a brain freeze in figuring out the equation on how to convert my .204V reading to 0 inches, since I'll never actually get a 0V reading. Please let me know if I'm not giving enough information. Any help would be greatly appreciated.
Thanks,
AK |
|
![](templates/subSilver/images/spacer.gif) |
Eric Minbiole
Joined: 10 Jun 2004 Posts: 16 Location: USA
|
|
Posted: Fri Dec 02, 2005 8:17 am |
|
|
Something like this should do the trick:
Code: |
#define MINIMUM_READING 0.204
#define MAXIMUM_READING 1.02
#define MAXIMUM_DISTANCE 2.0
...
fInches = MAXIMUM_DISTANCE * (fVolts - MINIMUM_READING) / (MAXIMUM_READING - MINIMUM_READING);
|
When fVolts == MINIMUM_READING, fInches will be zero. When fVolts == MAXIMUM_READING, fInches will be 2.0. If speed / code size are important, you could change the equation to use integer counts, rather than floating point numbers. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Dec 02, 2005 8:23 am |
|
|
Think y=mx+b
or modified for your application
inches = (volts - offset) * (inchspan/voltspan)
inches = (V - 0.204V) * ((2" - 0") / (1.02V - 0.204V))
You should get the idea by now... _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri Dec 02, 2005 9:20 am |
|
|
Remember also, that you might as well store the 'minimum reading', and 'span' (number of counts from 4-20mA), as integers. Your resistor will have a tolerance, as will the sensor itself, so ideally have code to set the minimum value, and maximum value, store these in EEPROM, rather than defines, and these can be changed when required.
So:
Code: |
int16 MIN_READING;
int16 SPAN;
float max_distance;]
float factor;
float distance;
//This wants to be the distance corresponding to the max reading
int16 val;
//To 'calibrate', feed in the minimum distance signal, and store this
//as 'MIN_READING', then feed in the maximum range signal, and
//store this minus the 'MIN_READING', as 'span'. Then tell the system
//what range this corresponds to as 'max_distance' (may be fixed).
//Now calculate the scale factor once only
factor=max_distance/SPAN;
//This only needs caculating when the calibration changes.
//Then if you get a 16 bit ADC reading
val=get_your_adc_reading();
distance=(val-MIN_READING)*factor;
|
Now the 'key' here is that the conversion, only needs an integer subtraction (very fast), followed by a float multiplication (which is a lot quicker than a division). The only division comes when calculating the factor applied, which needs only be done once. Also you don't bother to convert the reading to 'volts', but work directly with the ADC reading.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Sat Dec 03, 2005 5:34 am |
|
|
AK,
try the equation V=I*R
V = voltage, I = current (4-20ma) R = resistance (50ohms)
V = 4ma * 50ohms = 1V
V = 20ma * 50ohms = 5V
I would assume the voltage range is 1 to 5V across a 50 ohms resistor
Andrew _________________ What has been learnt if you make the same mistake? ![Wink](images/smiles/icon_wink.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Dec 03, 2005 7:47 am |
|
|
Storic wrote: | AK,
try the equation V=I*R
V = voltage, I = current (4-20ma) R = resistance (50ohms)
V = 4ma * 50ohms = 1V
V = 20ma * 50ohms = 5V
I would assume the voltage range is 1 to 5V across a 50 ohms resistor
Andrew |
Er.
0.004A * 50R = 0.2v
0.02A * 50R = 1v
You have a factor of five wrong here somewhere....
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Dec 03, 2005 9:28 am |
|
|
And, just to point out that AK had already got this part of the question 'right', and has chosen the resistor to give the allowable range for his ADC.
The question he was asking is how, given the ADC reading, to solve for 'distance', not what is "Ohm's law"?...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Storic
Joined: 03 Dec 2005 Posts: 182 Location: Australia SA
|
|
Posted: Sun Dec 04, 2005 5:25 am |
|
|
Sorry, I got it wrong, It is just out in the field I was getting 1 to 5v variation on 4-20ma, I now need to find out why.
Oops It was 250ohm that gave me the 1 to 5V variation
Andrew _________________ What has been learnt if you make the same mistake? ![Wink](images/smiles/icon_wink.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sun Dec 04, 2005 3:13 pm |
|
|
If we ran a contest for 'answering the wrong question', I'd be up there strongly in the runners at times.... :-)
It is amazing how often you 'see' the problem youhave had, rather than what is asked.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
AK
Joined: 20 Apr 2004 Posts: 33
|
|
Posted: Mon Dec 05, 2005 12:17 pm |
|
|
Thanks, the replies are very helpful.
AK |
|
![](templates/subSilver/images/spacer.gif) |
|