Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Measuring DC motor current with PIC ADC.

Status
Not open for further replies.

Exo

Active Member
I was trying this in order to measure a DC motor's current using a PIC - the motor in question can draw 3A max.
**broken link removed**

I had to add the 100µF capacitor because the measurement was wildely fluctuating, can anyone explain why this is and how to caculate the optimal capacitance required - the current value was just a bit trial and error.

Also, should i add a circuit to prevent the analog input from going over the pic's allowed maximum for protection, or do the internal clamp diodes also function in analog input mode ?

Thx.
 
Last edited:
Have you looked at the waveform at the top end of the shunt resistor with an o'scope? If you do, it will not be a mystery why the 100uF improved your readings...

You can increase the 2.7K to 10K, and still meet the PIC spec. That means you can use a smaller capacitor, or get better filtering with the existing 100uF. (Longer RC). A better solution would be a two-pole to four-pole active low-pass Butterworth or Tchebychef filter using opamps.

The internal clamp diodes with conduct if the analog input pin swings < 0.6V below ground, or > Vdd+0.6V. The 2.7K or 10K will limit the current.
 
Last edited:
Haven't scoped yet, will do so when i have more time.
Using a 150µF cap and the 2.7K resistor makes for a stable and accurate reading (only a few mA's off compared to my fluke's reading). It's a bit slow to respond but the intention is to detect an overload situation (mechanically stuck and the motor having to work to hard) for wich this will work i think.

Good to know the clamp diodes also function in analog mode.
Thanks.
 
There is no upper limit to the resistance if you have more than about 0.1µF between the analog input and ground. You might want a ceramic capacitor. The input capacitance, a few pF, has to charge up quite quickly. You need either a low resistance supply or a large enough capacitance to charge the input capacitance without significantly affecting the voltage.

Depending on the PIC, 2.5 V might not be large enough for specified accuracy. I would also suggest a 0.1 µF or more ceramic capacitor there as well because the reference input takes a spike of current as well during conversion.

You could try software filtering. It could be easier to adjust. The way that I do it is to have an accumulator, usually 16 bits. Read the ADC on a regular basis, maybe 100 times a second. Each time, add the ADC reading to the accumulator. Also, subtract a fraction of the accumulator value from the accumulator. I use a power of 2 fraction so you can shift which is quicker than dividing. The accumulator will contain a filtered value of the reading.

For instance, if you read 100 times a second, and you subtract 1/16th of the accumulator each time, then the accumulator will end up at 100/16 = 6.25 times the ADC reading, and the time constant will be about 0.16 second.
 
There is no upper limit to the resistance ...


There most certainly is! You obviously haven't read, or dont understand, the leakage specs on the analog input pins on a PIC.

btw- the upper limit is 10K.
 
Last edited:
Actually the upper limit to guarantee you're not off by more than one code while under the max specified leakage is 2.5K on most PICs.
 
When you buffer a voltage with a capacitor like the above circuit you are measuring the capacitor voltage and so the input resistance is extremely low.

Mike.
 
The maximum input leakage is specified as +/- 1 uA, so to stay at less than 1 count, with a Vref of 2.5 V, then the input resistance should be less than 2.5 kohms.

However, Microchip quote that same figure for lots of different inputs, and on their analog input model diagrams, they show +/-100 nA or +/-500 nA. I think that the 1 uA figure is a lot larger than they achieve in practice. I've found the input leakage to be negligible.

If there is a simple resistance, the glitch as the input capacitance charges is far more important. The holding capacitance seems to vary from PIC to PIC, but the figures quoted are typical values.
 
Some change of plans.

Hey again...

During testing it came to light that the shunt resistor was getting way too hot (should've seen this coming). Since the PCB has to be rather small and the components close together, the heat from the shunt causes the H-bridge IC to go into thermal protection. I've tried smaller shunt resistors but even a few watts equals lots of heat :(

But i've found another version of the H-bridge IC, the MC33887. This version outputs 1/375th of the motor's current trough a feedback pin - this way the shunt resistor can be larger (in value) and not get hot.

I've made a little sketch:
**broken link removed**

The 1µF cap came straight from the MC33887's datasheet. The question is, will this small capacitor produce enough of a smoothing effect to have a stable current reading ? Or should it be larger ?
Or is a capacitor between the PIC's AD pin and the 2.7K resistor to ground a better idea?

Sorry for asking all this but the H-bridge parts are only available in SMD and i'd like not wasting too much off them on prototyping boards.
 
Last edited:
When you buffer a voltage with a capacitor like the above circuit you are measuring the capacitor voltage and so the input resistance is extremely low.

Mike.
Sorry, that's not correct at all. You're confusing DC and AC resistance. The AC resistance is low so the internal ADC sampling cap's charging time is low. But that's not "leakage", which is a DC loss. It applies even if the ADC is off and the pin is just configured as input. 1uA through a 10K resistor and 100uF capacitor is still a 10K DC input resistance and is a 10uV error. With 2.5v Vref and 10-bit ADC, that's 2.44uV/code, so the ADC accurate is degraded by the high input resistance.

IIRC the PIC came up with the 2.5K input rating based on a 1uA leakage assumption, 5v Vref, and a criteria of less the HALF a code of error on a 10-bit ADC. (5v/(1024*2))/1uA=2.441Kohms. This is the possible DC error. It won't matter if you have 100uF capacitance and an hourlong sampling interval, the DC leakage error is still present. With a 2.5v Vref, you'd need <1.2K input resistance to meet a less-than-half-a-code criteria.

In practice, your PIC will probably have <1uA leakage, but it's not guaranteed. The leakage is also not going to be independent of analog input. You will get the most + leakage when the analog voltage is most negative.

Be aware that the PIC pin is NOT the only form of leakage. Traces always have leakage to nearby pins- this is especially true if you didn't clean up the flux. And capacitors have a nonzero leakage. Electrolytics can be especially bad in this regard, and highly variable. As much as 5uA from a low voltage (15v), low capacitance( <6.8uF) electrolytic alone is plausible (higher voltage caps can have even more leakage). Tantalums are about 5x less, but it's still present. Ceramics are pretty good though.

So PIC leakage ain't the big issue if you were to include an electrolytic or even tantalum cap. Bottom line is that if you use a capacitor, the leakage is HIGHER, and the maximum acceptable DC input resistance will need to be LOWER, the opposite of what Pommie's saying.


**broken link removed**

**broken link removed**
 
Last edited:
Sorry, that's not correct at all. You're confusing DC and AC resistance. The AC resistance is low so the internal ADC sampling cap's charging time is low. But that's not "leakage", which is a DC loss. It applies even if the ADC is off and the pin is just configured as input. 1uA through a 10K resistor and 100uF capacitor is still a 10K DC input resistance and is a 10uV error. With 2.5v Vref and 10-bit ADC, that's 2.44uV/code, so the ADC accurate is degraded by the high input resistance.

Entirely correct. I was thinking more about the acquisition time calculations where the effective input impedance is zero. Weirdly, I just checked the 16F88 data sheet and they specify max input impedance of 10k for ½LSB accuracy!!:eek:

BTW, I just realised that there are two circuits in this thread. I was referring to the first circuit where there is no resistor between the cap and the pin.
 
You guys seem to know a lot about the ADC in the pics.
I'm using the TL431 to create a 2.5 reference voltage as per Nigel's tutorials. Would it be ok to put a resistor, say 1K, between the output of the TL431 and the PIC's VREF+ pin to protect against accidentally setting the pin as an output? Or would this resistance create too much of a voltage drop ? I don't seem to find the input impedance of the VREF+ pin.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top