Measure Current using PIC Microcontroller

Thread Starter

CVMichael

Joined Aug 3, 2007
419
On a test board, I made a simple circuit that I hope to measure the current used from a 12V 7.2 Amps battery.

I never done this before, so if I am completely off... please be pacient and explain the right way to do this...

I did not connect it to the PIC microcontroller because I was afraid I will damage it in some way, this is why I want to get aproval from you first.

I attached the test circuit, the input is a 12V 7.2 Amps battery, and the Load I put a 12V 50 Watt light bulb. The battery was almost discharged when I tested it ( I'm charing it now :) )

I used an Ammeter and it shoed 3 Amps on the Load, then I tested the voltage on the "To PIC" and it showed ~210 mV

So because of the mesurements, I think it is safe enough to plug the PIC in, and measure through it's analog input.

I have a question:
How to get an accurate input from the PIC ? I mean, when it was 3 Amps, it showed 210 mV, so it's not like there is a 1 to 1 correlation between Amps and Volts...
 

Attachments

SgtWookie

Joined Jul 17, 2007
22,230
See the attached; do it that way instead.

R1 is the current sense resistor. Rload is your 50w lamp.
R2 limits current flow through the Zener in case R1 fails. You could probably omit the Zener if the input pin was clamped to Vdd, but you didn't say which PIC nor pin you're using.

Accuracy of your reading will depend upon the accuracy of the current sense resistor and the number of bits in the PIC's ADC, whether you have the PIC snooze while the ADC is processing the sample, and how well the PIC's Vdd/GND pin is connected to the ground reference of the sense resistor.

After that, it's I = E/R. Since R=0.1, and the ADC will return the E... the rest is up to you.
 

Attachments

Thread Starter

CVMichael

Joined Aug 3, 2007
419
Thanks for your input SgtWookie,

But what if I want to connect the load directly to the negative, and have the 0.1 Ohms resistor on positive ?

I need it like that because the 50w lamp was just for testing, I will be putting stepper motors, microcontrollers, and other devices on the Load, and I wanted to measure the total amperage for all of them combined.
 

SgtWookie

Joined Jul 17, 2007
22,230
Thanks for your input SgtWookie,

But what if I want to connect the load directly to the negative, and have the 0.1 Ohms resistor on positive ?

I need it like that because the 50w lamp was just for testing, I will be putting stepper motors, microcontrollers, and other devices on the Load, and I wanted to measure the total amperage for all of them combined.
The ADC of the PIC uses the Vss/GND terminal as the 0V reference. It will greatly simplify your project if the current sense resistor (Rsense) is on the low side.
It will require fewer components, and is very straightforward.

Keep in mind that what current comes out of one battery terminal must go in the other.

You have already noted that your readings from your circuit are non-linear. With the Rsense on the low side, you will not have this problem.

The PICs' ground/Vss needs to be connected to the battery negative terminal. For everything else that you want to monitor current usage, connect them to the high side of the resistor. Using a few capacitors across Rsense will help a great deal to stabilize/average out the voltage across Rsense, requiring fewer readings per second to obtain a reasonably accurate average. I suggest using 0.1uF and 100uF caps in parallel across Rsense.

Keep in mind that the PIC is electrically noisy. To get a reasonably accurate output from the ADC, you need to start the ADC sample, and then have it sleep for around 20uS. If you are using the internal clock, it will take a while for it to stabilize before the PIC will fully 'wake up'.
 

Thread Starter

CVMichael

Joined Aug 3, 2007
419
OK, thanks, I will do all that....

For the last paragraph:
I am using PIC16F877, and I program it with mikroC.
I assume that the function to get the analog value waits 20uS already ?
This is how I read the analog:
Rich (BB code):
temp = Adc_Read(0)
 

SgtWookie

Joined Jul 17, 2007
22,230
What is meant by clamped to vdd?
Using a semiconductor device (like a diode) to ensure that the voltage on a signal line cannot rise above or fall below a certain value; in this case Vdd, or the 5V supply.

It is very common to use diodes, particularly Shottky diodes, to clamp signal lines that have fast rise times. For an example of an 8-bit barrier Shottky diode bus termination network, look up a datasheet for an SN74S1056. One IC with 16 Shottky diodes in it.

Why is the zener in this circuit?
If the voltage on the signal line were to attempt to rise higher than 5.1v, the Zener would begin conducting, preventing the voltage from rising further.

The PIC microcontrollers do have clamping diodes internally. However, they are limited to 20mA maximum current. It is good practice to not subject microcontrollers to stress if you can avoid it. Replacing a Zener diode is much easier than programming and replacing a microcontroller. Besides, the Zener would be far more robust (able to dissipate far more power) than the PICs' internal clamping diodes. The PIC's internal clamping diode wouldn't start conducting until the signal input was around 5.6V, or 0.6v above Vdd. If a 5.1V Zener is used to ground, the input voltage should never reach 5.6v, thus the microcontroller is not subjected to stress unless the Zener fails due to overcurrent. However, this is highly unlikely due to the limiting resistor in the signal path.
 
Last edited:

jut

Joined Aug 25, 2007
224
Thanks a lot for the detailed explanation. It makes sense now.

I too am trying to measure current with a PIC (18f2520) using a 0.100 Ohm current sense resistor. I want to measure current as small as 10 mA (from a small solar panel), which would give a voltage of (0.1 Ohm * 0.010 A) 1 mV. I think the PIC will have a hard time distinguishing between 1 mV and noise. Do you know of a good circuit to multiply this voltage?
 

SgtWookie

Joined Jul 17, 2007
22,230
An operational amplifier (opamp) could do that for you.

If you use a rail-to-rail opamp designed for single-supply operation, you wouldn't need a negative supply. An LMV2011 from National Semiconductor or LT1006 from Linear Technology are such opamps. There's another reason for choosing one of these amplifiers, and that's their extremely low offset voltage. A typical "standard" opamp like a 741 might have an offset of several mV; this offset gets amplified along with whatever signal you're trying to amplify.

Please read through our E-book on opamps:
http://www.allaboutcircuits.com/vol_3/chpt_8/1.html
You will find many hints and tips on using opamps.
Also, go to National Semiconductor's site, and download Application Note 20. It's quite old, but still a valid application note on how to use opamps in a variety of circuits.
Here is a page that contains several presentations in Adobe format:
http://cktse.eie.polyu.edu.hk/eie304/
 

blocco a spirale

Joined Jun 18, 2008
1,546
It would be useful if you first define the minimum and maximum currents you need to measure. Aiming for an unecessarily high maximum current reading capability will compromise resolution.

A higher resistance I-sense resistor will provide a decent voltage for the PIC A-D but will also waste power. So why not take a reading every second (or more) rather than continuosly and when not taking a reading, bypass the sense resistor altogether with a transistor.

If you need to cover a large range of currents with good resolution you could have a range of I-sense resistors that could be switched in and out by the PIC.

Bear in mind that an op-amp will amplify both noise and signal so should be avoided if possible.
 
Last edited:

jut

Joined Aug 25, 2007
224
I've been experimenting w/an op-amp tonight... to no avail. My Vout is "railing", or saturated to 12V.

Here's my circuit:



Am I committing an obvious foul?
 

SgtWookie

Joined Jul 17, 2007
22,230
Well, it appears that you're using an LM1458N, which is basically two LM741 opamps, which are 40-year-old technology. Unless you have dual supplies connected to it, you have absolutely no hope of getting even a ballpark correct output. An LM741/LM1458 won't be able to properly read input voltages that are within about 1.5v of either supply rail.

Even then, your input offset will be huge, and that error will be amplified by the opamp, which has extremely poor performance compared to the opamps I'd mentioned above.

If you would go with one of the opamps I suggested, you wouldn't have those kinds of problems.

However, you are currently set up to amplify the input signal 11:1, since (R1+R2)/R1 = 11. If you remove RLoad and short the + input to ground, you will get the offset voltage of the LM1458, amplified 11 times. If you are attempting to use a single supply, you will probably read somewhere around 1.5v to 2.1v out. If you are using dual supplies, you may read anywhere from 2mV to 10mV, but multiplied by the gain - so 22mV to 110mV in your case. That's a lot.

It could also be that your load has a very low resistance; like a motor that is near stall. If you're measuring somewhere around 1.1v or more at the top of R-SENSE, then you're actually getting a reasonable output from your opamp.
 
Last edited:

blocco a spirale

Joined Jun 18, 2008
1,546
Then look at the AD8202 in DIP.

There is nothing wrong w/Sgt Wookie's suggestion of using an opamp to amplify a weak signal.
Since the original concern was distinguishing a 1mV signal from noise there is something wrong with amplifying it. Amplifying the signal and noise will not help distinguish one from the other. A higher signal-to-noise ratio is needed which means either less noise and/or more signal but not more of both.
 

jut

Joined Aug 25, 2007
224
Well, it appears that you're using an LM1458N, which is basically two LM741 opamps, which are 40-year-old technology. l.
The LM1458 is the only op-amp that I had on hand. I was planning to just experiment with it to see if I can get a reasonable output, and then eventually upgrade.

But point well taken about the amplifying that offset voltage.

However, you are currently set up to amplify the input signal 11:1, since (R1+R2)/R1 = 11.
Yes I wanted to amplify the input signal about 10 times. I should have mentioned my expected input signal range: 1 mV to 30 mV. Which corresponds to 10 mA and 300 mA through my R-sense.


Thanks a lot for the help. I will do some more experimenting tonight.
 
Top