# Interfacing Current Monitor with PIC

#### jwilk13

Joined Jun 15, 2011
228
Hi all,

I recently tried a current monitoring scheme with a PIC 18F45K20 as shown in the attached schematic. The idea is that the ZXCT1083 monitors the voltage across the 0.1Ω resistor, and the gain resistors make it such that a voltage of 0-3.3V is output by the device when the current ranges from 0-1A.

My problem is this: I failed to notice in the datasheet of the ZXCT1083 that it is a CURRENT output device rather than a voltage output. The voltage at the output varies from 0-3.3V like I initially expected, but a direct connect between the output of the ZXCT1083 and an analog input on my PIC is not possible due to the incredibly high output resistance of the ZXCT1083 (the datasheet says it's 1 GΩ / 5 pF). The maximum recommended input impedance for an analog input on the PIC18F45K20 is 10kΩ (for acquisition time and leakage current requirements).

I'm wondering if there's a way I can work around this without having to get a different current monitoring device. The fact that I can set the gain with the external resistors is really nice and I haven't been able to find another that will perform like this one does. Any suggestions?

#### SgtWookie

Joined Jul 17, 2007
22,220
Use a 10nF metal poly film or ceramic cap to ground on your ADC input pin.

#### Markd77

Joined Sep 7, 2009
2,806
I think it might be a typo in the datasheet or it means something I don't understand.
With the recommended Rg of 125K in the example circuit, if it's output impedance was actually 1G, it seems impossible for the voltage at OUT to ever be anything over a few millivolts.

#### jwilk13

Joined Jun 15, 2011
228
Use a 10nF metal poly film or ceramic cap to ground on your ADC input pin.
This helped. I was actually able to get readings from the ADC with the cap there, but the readings jump around all over the place. I've increased the acquisition time to make sure I'm giving the holding capacitor time to charge like the datasheet states, but still the same result. I'm thinking maybe my register settings are off or something...here's how I have them configured:

osccon = 0b11010111;
t2con = 0b00000111;

The process I'm using for the A/D conversion is as follows:

Rich (BB code):
unsigned int ADC_currentsense(void)
{

// This function reads the analog voltage at pin RE2 and converts it
// using the A/D converter.  The analog voltage at RE2 corresponds to the output
// current of the high-side switch (total current through the load).

unsigned int currentSense;

currentSense = adresl;            			// Get the 8 bit LSB result
return currentSense += (adresh << 8);     	// Get the 2 bit MSB result
}
The only other thing I can think of is that my initial assumption of the output resistance of the ZXCT1083 is far too high to give accurate readings still applies. Thoughts?

#### jwilk13

Joined Jun 15, 2011
228
So I thought for a few minutes about what's going on, and I thought of something that I figured I would run by the experts.

If the 33k resistor is replaced by a 10k, wouldn't the combination of the output resistance and the 10k resistor be considered in parallel? If they are, the equivalent resistance would be really close to 10k because the output resistance is so large, meaning the ADC input of the PIC should be able to read the voltage at the output of the ZXCT1083 with no problem. So I changed the 33k to a 10k and the sense resistors to 330 ohm, but my problem is still there.

It doesn't make sense to me, but I've also never done this before. Any thoughts?

#### SgtWookie

Joined Jul 17, 2007
22,220
You exported your schematic as a .jpg instead of a .png, so it's hard to read as it's fuzzy.

You should include the links to your prior threads on this topic, as I forget what you're doing to get the current readings in the first place. I don't have time to track down everyone's threads.

#### Markd77

Joined Sep 7, 2009
2,806
Don't know if you missed my post below, I don't think the output impedance is 1GΩ, it's probably in the tens to hundreds of ohms range.

#### jwilk13

Joined Jun 15, 2011
228
After clicking the thumbnail, if you click the image that pops up, it shows a full-size image of the schematic that should be readable. As for the other thread, it can be found here.

Mark, I did see your comment, and I've seen other current outputs similar to this with extremely high output impedances. Also, I know it's possible for the output to be above a few millivolts because I've been measuring it from 0-3.3V as the current through the 0.1 ohm resistor goes from 0-1A. It's working like I expected it to, I just can't get my PIC to bring in the right values. I know the code is right because I've used it before for reading A/D values from 0-3.3V. That's what led me to believe that the output impedance of the ZXCT1083 was too high for a PIC analog input. AFAIK, if the "source" impedance of the A/D input is too high, the charging capacitor won't have enough time to charge and you'll end up with messed up readings much like I'm getting right now. Also, you'll notice that the ZXCT1083 is a "current output" device according to the datasheet, whereas some of the other device family members are "voltage output" (such as the ZXCT1085). The "voltage output" devices have an output impedance of 125k according to the datasheet. Does this make sense?

Also of note, when I debug, I'll let the code run for a few seconds, then I pause it and set a breakpoint after the ADC code for the current sensor and I'll run it again, letting it restore the value. If I keep pressing run, letting the debugger go through the program and stop again after the ADC code, the values of the variable currentSense change DRASTICALLY from cycle to cycle. Keep in mind that I don't freeze the peripherals on halt, so when I press halt, I still get current through the 0.1 ohm resistor and therefore still get voltage at the A/D pin.

If you have any other questions that need clarification, please ask. I'll probably be thinking about this all night...normally the case when I can't figure something out. My wife has a hard time with that one. Thanks for the help.

#### colinb

Joined Jun 15, 2011
351
I failed to notice in the datasheet of the ZXCT1083 that it is a CURRENT output device rather than a voltage output. The voltage at the output varies from 0-3.3V like I initially expected, but a direct connect between the output of the ZXCT1083 and an analog input on my PIC is not possible due to the incredibly high output resistance of the ZXCT1083 (the datasheet says it's 1 GΩ / 5 pF).
The output impedance of an ideal voltage source is indeed zero ohms. However, the output impedance of an ideal current source is infinite.

You would certainly have an easier time just switching to a voltage-output device, and would probably get more accurate results since you wouldn't have to convert current to voltage to current to voltage.

But here is an analysis of your initial schematic:

Having a 0.1 ohm current sense resistor in series with the load and a load current of 1 A, you'll have, Vsense = Isense * Rsense = (1 A)*(0.1 ohm) = (0.1 V). The ZXCT1083 has a transconductance of G_T=100 µA/V, so the OUT pin will source a current of G_T * Vsense = (0.1 V) * (100 µA/V) = 0.01 mA. Your 33 kohm sense resistor between OUT and GND will have a voltage drop of V_OUT = I_OUT * R_OUT = (0.01 mA) * (33 kohm) = 0.33 V.

So the voltage on your ADC pin should vary from 0 mV to 330 mV as the load current varies from 0 A to 1 A.

I don't know why you're having trouble getting a good reading.

#### Markd77

Joined Sep 7, 2009
2,806
It seems I didn't know that definition of impedance, I thought it was just the equivalent of that value resistor in series with the output.

The best way to connect to the PIC is with a rail to rail op amp connected as a buffer. Takes some space and costs a little, but should solve the problem.

#### thatoneguy

Joined Feb 19, 2009
6,359
Taking the PIC out of the picture, what can you measure across the R14/33k resistor on the output with a known current through the sensor?

If that works, you are close and the PIC should be able to read it, as the impedance is 33k. Changing it to 10k would give lower resolution, but it would be worth a shot to see if the PIC will read what your known current is.

#### jwilk13

Joined Jun 15, 2011
228
For those who are curious, I disconnected my PIC and just took readings from the current monitor to see what the results were. R12 and R13 were changed to 330 ohm resistors and R14 was changed to 10k, which should give a gain of about 30 (according to the datasheet). Here are the results:

Vsense (V) Vout (V) Isense (A) Gain
0.0024 0.075 0.024 31.25
0.0068 0.213 0.068 31.3
0.011 0.339 0.11 30.8
0.0193 0.598 0.193 30.98
0.0334 1.035 0.334 30.99
0.0522 1.618 0.522 30.99
0.0709 2.199 0.709 31
0.0887 2.752 0.887 31

Vsense and Vout were measured with a voltmeter. Isense was calculated using Vsense and Rsense (0.1 ohm). The gain was calculated as Vout/Vsense.

As you can see from the results, the current monitor seems to be working as I expected it to. The fact that I can't get the right results in my A/D is bothering me. I'm going to try another input pin and I'm going to write some sort of check code because I'm also thinking that my way of debugging and checking the A/D value might not be right. If there was a way to do real-time monitoring with a PICKit 3, that would be awesome. Does anyone know of a way?

#### jwilk13

Joined Jun 15, 2011
228
Update:

I know my ADC acquisition code is good. I applied a variable voltage to the A/D input on the PIC and debugged just like I have been and the results are all good. The only thing left to blame is the current monitor (and my mistake of picking a current output ). I even lengthened the acquisition time to 20*Tad, but the results are still the same.

Can anyone think of something I'm missing?

#### thatoneguy

Joined Feb 19, 2009
6,359
If there was a way to do real-time monitoring with a PICKit 3, that would be awesome. Does anyone know of a way?
Not really, that's why they sell the ICD2 and ICD3 now. I suppose you could connect an LCD or serial port and have the ADC value spit out every second or so.

#### jwilk13

Joined Jun 15, 2011
228
I was about to fall asleep last night and I thought of something interesting. The signal through the 0.1 ohm sense resistor is a 250 Hz PWM signal, meaning the voltage out of the ZXCT1083 should also be a PWM signal. I confirmed it this morning...I was measuring the voltage out of the ZXCT1083 with a voltmeter previously, thus giving the illusion that the voltage was staying steady. When I used my scope this morning, it proved that the signal is in fact PWM with a max voltage of about 3V at ~90% duty cycle.

My solution is to low pass filter the ZXCT1083 output. My questions are these:

1. Would a simple RC filter work in this case?
2. If so, are there any considerations when designing for a PWM frequency as low as 250 Hz?
3. I already tried one configuration with R=33k and C=0.1uF, but the result was more of a sawtooth wave. When I replaced the capacitor with a 0.012uF capacitor, the output smoothed, but that doesn't make sense to me. Then the filter BW would be at about 400 Hz...I thought the filter BW was supposed to be much less than the PWM frequency. Can anyone provide any clues on this?

For the LPF design, I'm using this to help.

#### colinb

Joined Jun 15, 2011
351
Maybe instead of low-pass filtering the output voltage, you want to integrate it?

#### jwilk13

Joined Jun 15, 2011
228
Maybe instead of low-pass filtering the output voltage, you want to integrate it?
Can you elaborate on that? I'm not familiar with integrating a PWM signal to get a DC voltage, only using a low-pass filter.

My main concerns are that the PWM frequency is really low, and the ripple after filtering is too high. I suppose one solution could be to use another LPF in series. Any thoughts?

#### colinb

Joined Jun 15, 2011
351
Can you elaborate on that? I'm not familiar with integrating a PWM signal to get a DC voltage, only using a low-pass filter.

My main concerns are that the PWM frequency is really low, and the ripple after filtering is too high. I suppose one solution could be to use another LPF in series. Any thoughts?
OK, my suggestion of integrating the PWM signal may not be very helpful. The low-pass RC filter really is acting as an integrator, but what you want is an average voltage over some number of PWM periods, in order to measure the average current. Of course the integral of the sense voltage over some time period would indicate the electric charge transferred -- recall that the time-integral of current (amperes) is charge (coulombs).

There's a discussion of an RC low-pass filter as an integrator (see bottom of page, section entitled The RC Integrator Circuit). As you say, the ripple may be much too high for accurate measurement of average current.