# PIC - 4-20mA (Duty cycle formula)

Discussion in 'Embedded Systems and Microcontrollers' started by kevin306, Nov 6, 2014.

1. ### kevin306 Thread Starter New Member

Apr 18, 2013
7
0
I am implementing a 4-20mA current loop to my PIC. I have everything working. I am measuring the distamce away from an object. When I hardcode the distance as 300 the output voltage is 4V and when the distance is 12cm the output voltage is 0.8V. These values are got because the current needs to be 200uA before it enters the current loop. The current loop has a gain of 100, therefore giving 20mA on the output. The resistor I use is 20K.
(300cm) Current = 4/20K = 200uA * 100 = 20mA
(12cm) Current =0.8/20K = 40uA * 100 = 4mA
The problem is I have to work out a formula for the duty cycle to get these values.
The formula I am using is D.C = 300 *2.2 +158 = 818 which gives 4V but if i substitute 12 for 300 I get 184 which does not correspond to 0.8V which is what I need.
Can anyone help on this, if I am going about the wrong way or what way I need to approach this. Thank you

2. ### Kermit2 AAC Fanatic!

Feb 5, 2010
3,845
963
where does the 2.2 come from?
where does the addition of 158 fit in?
You have voltage values which by selecting proper gain and resistance values give 4-20 ma. what is source of those voltages?
need more details here.

3. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,435
1,625
You have a linear relationship, that means a straight line fits the data: y = m*x + b

To compute m (the slope) and b (the y intercept) just use the well worn technique from high school algebra:

Based on the output voltage 4V for 300cm and 0.8V for 12cm you get:

y1=4V y2=0.8V x1=300cm x2=12cm

y1-y2=3.8V x1-x2=288cm

slope m=(y1-y2)/(x1-x2)=0.01111V/cm

plug into either point:

4v=0.0111V/cm*300cm + b

4v=3.3333V + b

b=0.667

Thus the equation is:

Volts = 0.0111V/cm * (distance) + 0.667V

The next trick is to choose appropriate units such that floating point math is not required.

Last edited: Nov 6, 2014