Using Op Amp to read voltage and current with a PIC

SgtWookie

Joined Jul 17, 2007
22,230
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.
You're limited by the uC's Vcc/Vdd on the high end, and Vss/GND on the low end. The I/O pins have protection diodes; but those have limited current handling capabilities.

OK, if you take the max voltage range and divide it by an even number, you'll wind up with an ADC result that is easily scaled by left-shifting.

For example, 30v/16 = 1.875. If you used 1.875 as a Vref, shifting the ADC result left four bits effects a multiply by 16, and you have your result in volts.

30v/12 = 2.5v. Using a 2.5v reference, save the ADC result, and left shift it 3 bits (effecting x8) and then add it to the ADC result left-shifted 2 bits (effecting x4), so algebraically you've effected x12 multiplication, and the output is scaled to your actual voltage with no multiplication; just shifts and adds.

A disadvantage of using small Vrefs is that noise becomes a much greater factor than at higher Vrefs.

Oh, on resistor calculators - here's a very handy page for calculating pairs of resistors to use in series/parallel to get close to the real value you need:
http://www.qsl.net/in3otd/parallr.html
Bookmark it.
Table of standard resistance values: http://www.logwell.com/tech/components/resistor_values.html
Bookmark that, too.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
It's Schottky - it's a proper name :)
Spelling was never one of my great talents. :)

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.

Like I said I need to study Schottky diodes so not sure if this is a good thing or bad. :)
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Why not get a few each of the 1N5817 thru 1N5819 diodes? They're cheap enough, maybe a dime each.

Mouser has these pretty cheap: http://mouser.com/ProductDetail/Fai...iMZZMuIUjt4yeP9c9obPm%2b%2bm%2bwSlqDatmHznmQ=

Electronic Goldmine has some 1N5820 20V 3A Schottky diodes, $1 for a 5-pack.
(E.G. is not an authorized distributor, but they get in some interesting stuff. $10 min order.)
Mouser has them.

26 cents eac. Not too far off of goldmines price.

Question on the trimmer. I got a 20K 10% . 5% are like $34. I am assuming that since they are trimmers, that this is OK to have 10%?
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
I decieded to go with a 0-5V divider with a max input of 35VDC.

I used your calculator and then simulated the results in LTSpice. I manually set the input voltages (I need to learn how to get spice to vary the voltages for me) to 35VDC input and 17.5 vdc input. At 35VDC I was only getting 4.589 to the ADC.



But even more curious, I am getting 3.22 VDC at 17.5 VDC input. I would expect that to be around 2.29.

What am I doing wrong?


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

But I am not sure I understand. In series that is way too much. But in parallel 18K at most. Is this a typo? Or something I am not understanding?
 

SgtWookie

Joined Jul 17, 2007
22,230
Attach your .asc simulation file. It's in C:\Program Files\LTC\SwCad...

I'm going to have to go back and re-read the thread.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Attach your .asc simulation file. It's in C:\Program Files\LTC\SwCad...

I'm going to have to go back and re-read the thread.

Here ya go.

I should have done this in the first place.

As I wrote in previous post, I manually changed VIN because I am not sure how to have spice change it for me.
 

Attachments

SgtWookie

Joined Jul 17, 2007
22,230
OK.
Right-click on V1. Click the "Advanced" button in the pop-up dialog.
In the next dialog, click the Sine button.
Enter 17.5 for the DC offset and amplitude.
Enter 100 for the frequency.
Click OK.
Change C1 to 10nF
Change your TRAN statement to 10mS
Run the simulation, and you'll see that the TO_ADC output is getting clamped to the positive rail.
 

SgtWookie

Joined Jul 17, 2007
22,230
Gee, I really blew something in that calculator. :rolleyes:

[eta]
Now I'm confused - the same circuit works perfectly in Circuitmaker Student, but not in LTSpice?
[eta]
Hmm - changed the diode to an MBRB2545CT; now it works just fine. Wonder what's up with the 1N5817 model?
It also works fine with an MBR0520L ... but not with any of the 1N581x models.

The calculator is fine - it has something to do with the 1N5817 library model.
 
Last edited:

SgtWookie

Joined Jul 17, 2007
22,230
Models:
.model 1N5817 D(Is=31.7u Rs=.051 N=1.373 Cjo=190p M=.3 Eg=.69 Xti=2 Iave=1 Vpk=20 mfg=Motorola type=Schottky)

.model MBR0520L D(Is=82.5n Rs=.115 N=.7228 Cjo=180p M=.63 Eg=.69 Xti=2 Iave=.5 Vpk=20 mfg=Motorola type=Schottky)

I'm certainly no guru at Spice models (I'm just doing good to find the darn things!)
I can't explain what the differences in these library models are, or why the 1N5817 doesn't work and the MBR0520L does.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Models:
.model 1N5817 D(Is=31.7u Rs=.051 N=1.373 Cjo=190p M=.3 Eg=.69 Xti=2 Iave=1 Vpk=20 mfg=Motorola type=Schottky)

.model MBR0520L D(Is=82.5n Rs=.115 N=.7228 Cjo=180p M=.63 Eg=.69 Xti=2 Iave=.5 Vpk=20 mfg=Motorola type=Schottky)

I'm certainly no guru at Spice models (I'm just doing good to find the darn things!)
I can't explain what the differences in these library models are, or why the 1N5817 doesn't work and the MBR0520L does.
No problem at all. I just appreciate the help. So a 1N5817 should work in real life?


I changed it to a MBR0520L and it does look much better but I am still only getting 4.69 volts at 35VIN.

Maybe I will try Circuit Maker Student.
 

SgtWookie

Joined Jul 17, 2007
22,230
Do this:
Get rid of D1, V2, and R2.
Connect the bottom of R3 with the top of R1.
Change R3 to 162k.
Now you'll see you get the full range.

The built-in ESD protection diodes will take care of any over/under voltage.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Do this:
Get rid of D1, V2, and R2.
Connect the bottom of R3 with the top of R1.
Change R3 to 162k.
Now you'll see you get the full range.

The built-in ESD protection diodes will take care of any over/under voltage.

Well that makes it simple! :)

So all this worry about over voltgage was for nothing? The PIC will protect itself?


Almost every tutorial I read warned of over voltage on the ADC input.


Here is what the datasheet says about ESD (now that I know what it is :)).

A simplified circuit for an analog input is shown
Figure 20-6. Since the analog input pins share
connection with a digital input, they have
biased ESD protection diodes to VDD and VSS.
analog input, therefore, must be between VSS and
If the input voltage deviates from this range by
than 0.6V in either direction, one of the diodes
forward biased and a latch-up may occur.
A maximum source impedance of 10 kΩ is recommended
for the analog sources. Also, any external component
connected to an analog input pin, such as a capacitor
a Zener diode, should have very little leakage current
minimize inaccuracies introduced.

What do they mean by "latch up"? Does this need to be cleared in some way?

The datasheet just mentions that it will happen in over and under voltage. It does not say what to do about it.

Also what is the best way to wire my trimmer? I have a 20K trimmer right now. Would I want to place that in series or parallel with another 1% resistor or does it not matter?
 

SgtWookie

Joined Jul 17, 2007
22,230
What was happening with the LTSpice simulation is that the 1N5817 model has this parameter:
Is=31.7u
where the other diode has this:
Is=82.5n

This is the saturation current of the diode. For some reason, the simulation decided that the diode should ALWAYS have a current flow of Is when the diode is reversed-biased. In the case of the 1N5817, Is=31.7uA; when you put 31.7uA current across a 27k resistor, you wind up with an offset of 837mV.

Your quote from the datasheet got a bunch of words cut out of it, so it's difficult to understand what it's saying. What datasheet are you referring to?

In opamps, "latch-up" is a sudden reversal of phase when the input goes too high or too low.

What values of resistors do you have on hand now?
Let's just worry about the trimmer if you absolutely have to use it.
 

SgtWookie

Joined Jul 17, 2007
22,230
What was happening with the LTSpice simulation is that the 1N5817 model has this parameter:
Is=31.7u
where the other diode has this:
Is=82.5n

This is the saturation current of the diode. For some reason, the simulation decided that the diode should ALWAYS have a current flow of Is when the diode is reversed-biased. In the case of the 1N5817, Is=31.7uA; when you put 31.7uA current across a 27k resistor, you wind up with an offset of 837mV.

Your quote from the datasheet got a bunch of words cut out of it, so it's difficult to understand what it's saying. What datasheet are you referring to?

In opamps, "latch-up" is a sudden reversal of phase when the input goes too high or too low.

What values of resistors do you have on hand now?
Let's just worry about the trimmer if you absolutely have to use it.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Your quote from the datasheet got a bunch of words cut out of it, so it's difficult to understand what it's saying. What datasheet are you referring to?

In opamps, "latch-up" is a sudden reversal of phase when the input goes too high or too low.

This is what I plan to use for both my bench power supply and my solar panel project. I picked it mainly for the low power consumption for the solar panel. It would have plenty of memory and i/o for both projects. It has a wide range of operation voltages so I can experiment there too. I figured no reason I should have to learn another PIC. That is why I picked the same for both.

http://ww1.microchip.com/downloads/en/DeviceDoc/41303F.pdf

Search on latch up.

What values of resistors do you have on hand now?
Let's just worry about the trimmer if you absolutely have to use it.
In the 1% range I have

200 ohm
270 ohm
27Kohm
100K ohm

I have a various values of 5%. I can list if you think it is worth it.

I have a couple of 20K 20 turn trim pots. I have 1 1K but it is only 1 turn.


In diodes I have

1n5817
1n5818
1n5820

No big deal if I need to order more. I need to order a bunch of new parts anyway. But I need your signoff before placing the order. :) Details in this post.


No big deal if I need to make a order. I need to make another order anyway.
 
Last edited:

SgtWookie

Joined Jul 17, 2007
22,230
OK.
Go back to post #34, where I said:
Do this:
Get rid of D1, V2, and R2.
Connect the bottom of R3 with the top of R1.
Change R3 to 162k.
Now you'll see you get the full range.
Add in a Schottky diode, like a MBR0520L or BAT54 - and you'll still get the full range, but you'll likely avoid the latch-up problem.

In the spreadsheet, just change the Vf of the diode to 0v and you'll see that R2 goes to zero Ohms. The problem here is that the current flow through the resistor network is so low that any reverse leakage current through the diode contributes significantly to errors.

I simply do not have the time to digest the datasheet for the PIC that you have chosen. I am uncertain why you decided on that model, but I was thinking that a PIC12F683 or PIC12F675 or another in that ilk might've been sufficient for the battery charger/LED night light project.

Being overly ambitious is quite common for n00bs. Beware that there is a steep learning curve at the outset, and wild enthusiasm can rapidly turn to despair and disillusionment if you try to go too far, too fast.

This board is a great resource, but it it not interactive. There are quite a few members who need input for their projects; and I have a few of my own going on right now - some are actually related to your project.
 

Thread Starter

spinnaker

Joined Oct 29, 2009
7,830
Thanks!

No problem. I did not expect you to digest the datasheet. Actually it says nothing much about latch up. :)



Maybe I will just keep things simple as you suggested with a simple 2 resistor voltage divider. It is easier and something that I can understand right now. I could always add the over voltage protection later.
 
Top