CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

4 to 20 mA converted to voltage

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
AK



Joined: 20 Apr 2004
Posts: 33

View user's profile Send private message

4 to 20 mA converted to voltage
PostPosted: Fri Dec 02, 2005 7:29 am     Reply with quote

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
Eric Minbiole



Joined: 10 Jun 2004
Posts: 16
Location: USA

View user's profile Send private message Visit poster's website

PostPosted: Fri Dec 02, 2005 8:17 am     Reply with quote

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.
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri Dec 02, 2005 8:23 am     Reply with quote

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.
Ttelmah
Guest







PostPosted: Fri Dec 02, 2005 9:20 am     Reply with quote

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
Storic



Joined: 03 Dec 2005
Posts: 182
Location: Australia SA

View user's profile Send private message Send e-mail

PostPosted: Sat Dec 03, 2005 5:34 am     Reply with quote

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
Ttelmah
Guest







PostPosted: Sat Dec 03, 2005 7:47 am     Reply with quote

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
Ttelmah
Guest







PostPosted: Sat Dec 03, 2005 9:28 am     Reply with quote

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
Storic



Joined: 03 Dec 2005
Posts: 182
Location: Australia SA

View user's profile Send private message Send e-mail

PostPosted: Sun Dec 04, 2005 5:25 am     Reply with quote

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 Embarassed It was 250ohm that gave me the 1 to 5V variation

Andrew
_________________
What has been learnt if you make the same mistake? Wink
Ttelmah
Guest







PostPosted: Sun Dec 04, 2005 3:13 pm     Reply with quote

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
AK



Joined: 20 Apr 2004
Posts: 33

View user's profile Send private message

PostPosted: Mon Dec 05, 2005 12:17 pm     Reply with quote

Thanks, the replies are very helpful.
AK
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group