PIC – 4-20mA (Duty cycle formula)

cpicpwm

I am implementing a 4-20mA current loop to my PIC. I have everything working. I am measuring the distance away from an object. When I hardcoded 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 = \dfrac{4}{20K} = 200uA \times 100 = 20mA \$

(12cm) \$ Current = \dfrac{0.8}{20K} = 40uA \times 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 \times 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

http://cr4.globalspec.com/PostImages/201012/4-20mA_LT1013_XTR115_3EA2C958-E757-2953-041770EF1E1B4760.jpg

Best Answer

Given your one data point, that 818 gives you 4V, I can apply some detective work and surmise that you are using the PWM in 10-bit mode and your supply / PWM reference voltage is 5V.


Edit: That means that the output voltage after low-pass filtering is

Vout = Vref * count/1024

so count must be 1024 * Vout/Vref.

Count for 4V (20mA full scale @ 300cm in) is 819.

Count for 0.8V (4mA minimum scale @ 12cm in) is 164.


Edit: So start by determining the multiplier for the 'gain'- change in count for change in input cm. We call the change in input or output of an instrument from one extreme to the other the 'span'. Since this is linear we can ignore the zero intercept for now and calculate the slope- we'll add or subtract a number later to get the 'zero' right.

The span of your output voltage must be 4-0.8V = 3.2V for an input change of (300-12) = 288cm, which corresponds to a count span of 819 (at 20mA out or 300cm in) - 164 (at 4mA out or 12cm in) = 655 counts.

(You had 818 corresponding to 654 counts which might be because your 5V Vref is a touch higher than 5V).

So the multiplier must be 654 or 655 divided by the input change 288 = 2.274.

Now correct the zero intercept:

At 12cm you desire an output voltage of 0.8V, or a count of 164. If we multiply 12 * 2.274 we get 27 counts, which is short of the desired 164 counts by 137 counts, so add 137 to the result.

So, if x is the number of cm, you must multiply it by 654/288 = 137/144 (or 655/288 with exact 5V Vref) and add 137.

12 cm would thus give you 164 counts for 0.802V and 300cm would give you 818 counts for 3.994V (or 819 for 3.999V).

Note that 16 bit integer multiplication is inadequate to multiply 137 x 300 without overflow, but if you're using floating point, no problem.