Using Op Amp to read voltage and current with a PIC

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
I would like to measure both voltage and current using a PIC. I have been doing some research and found that the PIC would be most happy if it has a high impedance input into the ADC and this can be accomplished with an OP AMP.

I read a bit about op amps in the wonderful tutorials on this site but these little devices do so many things, I am not sure exactly what I need to accomplish my task.

Can any one point me to a sample circuit that I can measure voltage in the range of 0-30 VDC and current maybe a 1ma or so to maybe 1 or 2 amps? My supply to the PIC will be 5VDC so therefore my reference voltage for the ADC will be 5VDC.

I would like to power the op amp with the same 5 VDC. I don't want to have to add any extra supply voltages.

I know I can use a voltage divider using resistors but that is about as far as I know to read the voltage from the ADC. I would like to be able to do it with an op amp.

Plus points for being able to turn the op amps on and off so that I can read multiple sources with the same ADC input.
 

mik3

Joined Feb 4, 2008
4,843
You have to find a single supply op amp with rail to rail output voltage. Connect it as a voltage follower (output connected directly to the inverting input) and connect the non inverting input to the resistor voltage divider. The job can be done with only a voltage divider if the resistors have not very high resistance. However, it is better to use an op amp as it has low output resistance which is better. If you want to sample many signals you can use many ADC inputs of the PIC.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
You have to find a single supply op amp with rail to rail output voltage. Connect it as a voltage follower (output connected directly to the inverting input) and connect the non inverting input to the resistor voltage divider. The job can be done with only a voltage divider if the resistors have not very high resistance. However, it is better to use an op amp as it has low output resistance which is better. If you want to sample many signals you can use many ADC inputs of the PIC.

Thanks . Do you mean like this one?

http://semicon.njr.co.jp/njr/hp/fileDownloadMedia.do?_mediaId=108

And my circuit might look as follows?


Did I do this correctly?
 

Attachments

MikeML

Joined Oct 2, 2009
5,444
Wouldn't you want a much higher input impedance, so use resistors in the hundreds of KΩ in the input voltage divider?

For the current monitor, use a high-side monitor like the ZXCT-1009 and a suitable resistive shunt.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Wouldn't you want a much higher input impedance, so use resistors in the hundreds of KΩ in the input voltage divider?
Wasn't really sure if I needed it just a noob. Maybe like 500K and 100K?


For the current monitor, use a high-side monitor like the ZXCT-1009 and a suitable resistive shunt.
I was going to use thePIC18F45K20 just because I have a demo board with one and that is what I have been working with. Also iit is optimized for C (not sure if any others are). It only has 2 ADC inputs but I want to monitor both current and voltage for two different sources.

Is there any way to make this work? If it is too complicated, I might just nix the current idea and just monitor voltage for now and add current later.
 

SgtWookie

Joined Jul 17, 2007
22,230
Actually - you want to present a low impedance source to the ADC, and a high impedance load to the source being tested.

One really easy way to do that is to use a resistive divider, with a suitably sized cap on the input to the ADC.

Of course, you will need to wait an appropriate amount of time for the capacitor voltage to settle before/between samplings. Otherwise, you might drain too much from the cap.

One advantage of using a cap is that you won't have to worry about noise or offset voltages from an opamp or other buffer circuit. Another is that it is very simple and very inexpensive.

As far as measuring current - you can simply place a current sense resistor between the load and ground. Ohm's Law will help with the rest.

For example, let's say your sense resistor is 0.1 Ohms. If 1A current passes through the resistor, you will measure E=IR or Voltage = Current x Resistance = 1A x 0.1 Ohms = 0.1 Volts.

Note that the accuracy of your readings will be very dependent upon your PIC's reference voltage. If you use a precision reference other than your PIC's power supply, you will likely get a much more accurate result.

Microchip has several application guides on their website regarding using the ADC; you should read them.

Have a look at AN513 and ADN007 for some ideas.
 
Last edited:

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Actually - you want to present a low impedance source to the ADC, and a high impedance load to the source being tested.

One really easy way to do that is to use a resistive divider, with a suitably sized cap on the input to the ADC.

Not sure I understand 100%. Would it like attached?

Or would C1 parallel R2?


OK but before you answer let me look at AN513 and ADN007 to see if I can figure it out myself. Thanks for the reference.
 

Attachments

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830

Thank you lmartinez this really helps.


Thanks Sarge for the reference to the datasheets. This is exactly what I want to do, getting back into this hobby. I want to not be told but to be pointed in the right direction and maybe just a little help along the way.


I was a little lost on ADN007. About the only thing I got out of it was the importance of component selection and that I should use a filter before the input of the ADC,


I may have done a little better on AN513. I understand AN513 correctly:


  • The actual example circuit is meant to measure current.
  • Reading analog input (voltage) is not as simple as attaching the source to the analog input and reading it (for accurate measurement). Some method must be used to compensate for differences in temperature, reference voltages etc. AN513 gives a method for compensating for these differences.
  • AN513 measures the voltage by repeatedly discharging a capacitor then measuring the amount of time it takes for the capacitor to be charged.

Did I understand it correctly?


If so:

I am not seeing exactly where they are converting the time into a meaningful voltage (it has been a long time since I used assembler).


Would this technique for measuring voltages be covered in the forum tutorials?

Do I really need such a complicated technique for measuring voltages for my supply DMM?
 

SgtWookie

Joined Jul 17, 2007
22,230
It's been awhile since I've read those Application Notes and Guides; but I had them handy and thought you could get some benefit from them by reading them.

Basically, there are some really easy ways to get a fairly high impedance presented to the load, a low impedance source to the ADC input, if you don't mind having the uC spend some cycles waiting for the filter cap to charge up.

There are also sophisticated ways to sample and hold the input voltage. It's kind of a question of what trade-offs do you want to make for simplicity vs accuracy vs time required for the sample. If you're creating a hand-held VOM/DMM, your uC has plenty of time to waste. If you were attempting high speed signal processing, you'd need every machine cycle and then some.

As far as this being covered in the tutorials; they are pretty general, there is nothing really PIC specific in there.

The project that lmartinez linked to looks very close to what you're wanting to do.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Thanks! My demo board has a PIC18F45K20 , so I was going to build my bench supply with that. But I am learning much from you! :) I reread the PIC18F45K20 carefully and discovered that the max operating voltage of the PIC18F45K20 is only 3.6V.

I tried coming up with a voltage divider for that range but no matter what I pick, I get odd ball values for the resistors. My guess it would be easier to select a mcu with a 5V operating range.


I doubt my input voltage would ever go over 30VDC. I have a 2 resistor voltage divider that is working out on Spice. While the input should never exceed 30vdc, it would be nice to have some protection. Can I add a zener to limit the max voltage?
I got this to work in spice but I am stuck with a fairly low value for R1 (the resistor connected to the voltage being measured). Plus I understand zeners can be noisy.

Maybe it is best just not to worry about my voltage exceeding 30V?
 

SgtWookie

Joined Jul 17, 2007
22,230
Thanks! My demo board has a PIC18F45K20 , so I was going to build my bench supply with that. But I am learning much from you! :) I reread the PIC18F45K20 carefully and discovered that the max operating voltage of the PIC18F45K20 is only 3.6V.
Well have you done, to read the manual, young Jedi. :D

I tried coming up with a voltage divider for that range but no matter what I pick, I get odd ball values for the resistors. My guess it would be easier to select a mcu with a 5V operating range.
What is your minimum and maximum range of voltage that you wish to measure?

You do know that you can provide an ADC reference voltage externally, right? (I highly recommend doing so)
And the reference voltage will be divided up by the number of bits in your ADC?

I doubt my input voltage would ever go over 30VDC. I have a 2 resistor voltage divider that is working out on Spice. While the input should never exceed 30vdc, it would be nice to have some protection. Can I add a Zener to limit the max voltage?
A Zener does not have a "brick wall" response. It starts to conduct current well below it's rated voltage, but the current ramps up quite a bit as it approaches the "knee". Consult a datasheet for a Zener diode to see what I'm talking about.

I got this to work in spice but I am stuck with a fairly low value for R1 (the resistor connected to the voltage being measured). Plus I understand Zeners can be noisy.

Maybe it is best just not to worry about my voltage exceeding 30V?
Resistors and semiconductors definitely cause electrical noise.

It's like a nozzle at the end of a garden hose set to create a jet of water. Lots of hissing. If you take the nozzle off, it becomes quiet. Or, if you drop the hose end with the nozzle still on it in a bucket of water, it also becomes quiet. The bucket is an analogy to a capacitor.
 

SgtWookie

Joined Jul 17, 2007
22,230
Give this a test run:



Note that R3 can be a 270K resistor with a 10-or-more turn 20k trimpot.

R1 and R2 are standard values. They should be precision metal film for the lowest noise and greatest stability.
[eta]
The combination of resistors I used results in under 0.1mA (100uA) current through the divider with 30v input.
The 1N5817 has a Vf of about 0.32v at low current. R2 was selected so that when the voltage across R1 was near 2.5v, the voltage from R2 to ground would be 3.6v. Since you'll be using a 3.3v supply to power the PIC, and D1 has a Vf of 0.32v, that's a reasonable approach to clamping the ADC input voltage.

C1 was chosen to provide a reasonable compromise between giving the ADC a good low impedance input and RC time. Note that you'll need to give the cap around 20mS between ADC samples to get a reasonably accurate output.

There are plenty of precision 2.5v references available; they are highly stable. MC1403, LT1009, LM336, ADR421 are but a few examples.

You don't have to use a 2.5v reference; you could go with a 2.048v reference. That could make it easier for your conversion between ADC output and actual voltage. You really don't want to be performing multiplication or division in a uC if you can avoid it; unless you're just using shift operations.
 

Attachments

Last edited:

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Well have you done, to read the manual, young Jedi. :D

Actually more like old Jedi that has been away from "the force" for a VERY long time. :)

What is your minimum and maximum range of voltage that you wish to measure?
Around 0-30V. Sorry thought I mentioned that.

Yes I knew this but I did not think it would matter. Wouldn't I be referencing with the same source I am powering the chip? What other source would I use? If I am right on my first question, what is the difference of using an external source? More reliable than internal?

I will take a closer look at the project submitted above to see if they are using an external source.

A Zener does not have a "brick wall" response. It starts to conduct current well below it's rated voltage, but the current ramps up quite a bit as it approaches the "knee". Consult a datasheet for a Zener diode to see what I'm talking about.
Probably what I discovered when I ran my tests. I think I have to look at taking a Jedi refresher course, perhaps at the local community college. :)



Resistors and semiconductors definitely cause electrical noise.

It's like a nozzle at the end of a garden hose set to create a jet of water. Lots of hissing. If you take the nozzle off, it becomes quiet. Or, if you drop the hose end with the nozzle still on it in a bucket of water, it also becomes quiet. The bucket is an analogy to a capacitor.
Great analogy! :)
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Give this a test run:



Note that R3 can be a 270K resistor with a 10-or-more turn 20k trimpot.

R1 and R2 are standard values. They should be precision metal film for the lowest noise and greatest stability.
[eta]
The combination of resistors I used results in under 0.1mA (100uA) current through the divider with 30v input.
The 1N5817 has a Vf of about 0.32v at low current. R2 was selected so that when the voltage across R1 was near 2.5v, the voltage from R2 to ground would be 3.6v. Since you'll be using a 3.3v supply to power the PIC, and D1 has a Vf of 0.32v, that's a reasonable approach to clamping the ADC input voltage.

C1 was chosen to provide a reasonable compromise between giving the ADC a good low impedance input and RC time. Note that you'll need to give the cap around 20mS between ADC samples to get a reasonably accurate output.

There are plenty of precision 2.5v references available; they are highly stable. MC1403, LT1009, LM336, ADR421 are but a few examples.

You don't have to use a 2.5v reference; you could go with a 2.048v reference. That could make it easier for your conversion between ADC output and actual voltage. You really don't want to be performing multiplication or division in a uC if you can avoid it; unless you're just using shift operations.
Thanks for posting this.

I will try running it myself.

And I see you already answered my question above about the voltage references.

I assume I could also make this all work with a + 5vdc ( Just need to rework the numbers) too or should I still stick with your recommended 2.048? Would 2.048, give me enough resolution to measure 0-30V?
 

SgtWookie

Joined Jul 17, 2007
22,230
You CAN use the uC's Vcc/Vdd as a voltage reference. Lots of folks do. However, you have to keep in mind that most typical voltage regulators really aren't terribly precise, and if you're running a bunch of other stuff from Vcc/Vdd, you may have a lot of noise on it - even with plenty of bypass caps sprinkled around the board.

The circuit I posted is for if you're going to use 2.5v as a reference.
If you want to use 2.048v for a reference, the divider resistors will have to be re-calculated.

The reference voltage is the highest possible voltage your ADC can measure. If your ADC is outputting all 1's (however many bits it is), you should flag that as an overflow or out of range condition - unless the ADC itself has overflow/underflow bits that you can test.

If you have a spreadsheet that is compatible with Microsoft Excel, you can use the attached to figure out different resistor values quickly. Inside the .zip file is an .xls spreadsheet.
 

Attachments

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
You CAN use the uC's Vcc/Vdd as a voltage reference. Lots of folks do. However, you have to keep in mind that most typical voltage regulators really aren't terribly precise, and if you're running a bunch of other stuff from Vcc/Vdd, you may have a lot of noise on it - even with plenty of bypass caps sprinkled around the board.

The circuit I posted is for if you're going to use 2.5v as a reference.
If you want to use 2.048v for a reference, the divider resistors will have to be re-calculated.

The reference voltage is the highest possible voltage your ADC can measure. If your ADC is outputting all 1's (however many bits it is), you should flag that as an overflow or out of range condition - unless the ADC itself has overflow/underflow bits that you can test.

If you have a spreadsheet that is compatible with Microsoft Excel, you can use the attached to figure out different resistor values quickly. Inside the .zip file is an .xls spreadsheet.

Thanks. Found plenty of divider calculators with 2 resistors but I have not seen any with 3.


Yes I understand that the reference is the highest the mcu can measure. But it can be pretty much anything I want as long as it does exceed the specifications of the inputs to the reference input and the ADC input? If so are there advantages to using a higher reference? If I would take a guess, it does not really matter since you have only so many bits anyway.
 

SgtWookie

Joined Jul 17, 2007
22,230
Cool! Got it working in LTSpice!!

Now I need to read up on schotkeys! :)
It's Schottky - it's a proper name :)

Here's a link to OnSemi's datasheet on the 1N5817-1N5819 1A Schottky rectifiers:
http://www.onsemi.com/pub_link/Collateral/1N5817-D.PDF

Looks like I boogered the Vf on the diode; have a look at the left plot (figure 7) on page 5. Looks like Vf @ 25°C with 20mA current flow is about 0.22v, but where we are is an inch or so off the bottom of the plot!

At the current levels you'll be working with, the Vf of the 1N5817 will be next to nothing.
 
Top