Arduino Forum

Using Arduino => General Electronics => Topic started by: Karsten_87 on Apr 06, 2016, 05:48 pm

Title: Battery Voltage Measurement with OP Amp
Post by: Karsten_87 on Apr 06, 2016, 05:48 pm
Hello Everybody,


In the past I often found a solution here so now I try with something I could not solve with google.

I build a little current controlled battery simulator to charge and discharge cells with different current profiles. For this I have a cell, the charging/discharging circuit and the cell voltage measurement. I use an OP Amp with the output voltage directly connected to an ADC port of an Arduino Uno. The OP Amp has two functions: 1. multiplying the cell voltage times two (better resolution) 2. giving me just the cell voltage since for the charging the negative pole has not the same potential as ground. (see attached circuit figure)

The Arduino gets its reference voltage from an external precision reference (5V.) As you can see in the "test" figure, the measurements works well (precise enough, not noisy) except for one point:

When hitting about 2.44V the measurement starts to jump weirdly as if the resolution has decreased a lot. Does anybody has an idea why this could happen?

I already tried to measure precisely 2.5V with the result of an output voltage of 5V at the OP AMP and 2.49something as Arduino output. I have a bit of the feeling that it could be the Arduino, but not a slightly hint where to start looking.

In the past I, let's say, mistreated the analogue input at least once with overvoltage but couldn't determine any changes in the measurement quality afterwards. Could the route for my error could be somewhere there?


Thank you very much in advance! I hope somebody had something like this before.

Best regards
Karsten





Title: Re: Battery Voltage Measurement with OP Amp
Post by: jremington on Apr 06, 2016, 06:07 pm
What op amp are  you using? With many, there are limits on the allowable input voltage range.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 06, 2016, 06:13 pm
The answer might be more obvious if you look up the charged voltage for an NiMh and devide your measured voltage by 2 (assuming you calibrated your op amp circuit and actually KNOW the EXACT GAIN to be EXACTLY 2.000 ) and compare your measured voltage (after dividing by GAIN) to the charged voltage for an NiMh.
I mean if the battery is already charged, what do you expect it to do ? (other than go into some kind of "trickle charge " mode, if that's even possible)

Quote
a freshly charged AA NiMH cell in good condition is about 1.4 volts.[20]
NiMh battery (https://en.wikipedia.org/wiki/Nickel%E2%80%93metal_hydride_battery)

Judging from your above and your plot, I would estimate your EXACT gain to be about 1.75.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: MarkT on Apr 06, 2016, 06:24 pm
What op amp are  you using? With many, there are limits on the allowable input voltage range.
Not an issue with this circuit I think, the inputs are at about 1.2V in the differential amplifer
configuration.

It could be genuine signal - check with a multimeter across the cell.  However I don't see any
decoupling in that circuit - decouple your opamp for stable operation.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: jremington on Apr 06, 2016, 06:39 pm
Post a complete circuit diagram. The battery appears to be floating, so the reference for V1 and V2 is not clear.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 06, 2016, 09:10 pm
As mentioned, the voltage plotted that looks wierd is actually the battery reaching the charged state at 1.4 V (OP's output voltage is about 1.75 times the battery voltage.)
The battery is alternating between charging and discharging at the charged voltage. (probably a few hundred mV)
Title: Re: Battery Voltage Measurement with OP Amp
Post by: MarkT on Apr 07, 2016, 12:29 am
Post a complete circuit diagram. The battery appears to be floating, so the reference for V1 and V2 is not clear.
That's not an issue, the differential amplifier circuit will bias it OK - if it wasn't floating there could be
a risk of it departing outside the safe operating envelope of the opamp, but as it is it is sitting balanced
around 1.2V which should be OK for a modern opamp.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: MarkT on Apr 07, 2016, 12:31 am
As mentioned, the voltage plotted that looks wierd is actually the battery reaching the charged state at 1.4 V (OP's output voltage is about 1.75 times the battery voltage.)
The battery is alternating between charging and discharging at the charged voltage. (probably a few hundred mV)
Interesting theory, but the voltage steps go the wrong way.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 07, 2016, 01:00 am
When the battery is fully charged , it no longer presents a load and the voltage reverts to an op amp output with no load .

Try adding a bleeder resistor across the battery. (10 k  to 22 k ohm)

Measure tge battery voltage with a meter and post the value
Title: Re: Battery Voltage Measurement with OP Amp
Post by: jremington on Apr 07, 2016, 01:46 am
Quote
That's not an issue
Yes, it is an issue. We have no way of knowing what the rest of the charging circuitry looks like, and how it is connected to the existing circuitry.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 07, 2016, 01:54 am
Quote
We have no way of knowing what the rest of the charging circuitry looks like, and how it is connected to the existing circuitry.
Not to mention we have no idea what the actual resistor values are, what the actual gain is or what the actual battery voltage is when it is charged.  I don't see how posting  a plot from an amplifier with an unknown (actual) gain  will tell us that.  We need the plot to understand the symptoms but we need the (actual)voltage measured with a meter to calculate the actual gain.

The circuit is an Op Amp Subtractor (https://hal9k.ifsc.usp.br/~smaira/Gradua%C3%A7%C3%A3o/7%C2%BA%20Semestre/Microprocessadores%20II/Trabalho/circuitos.pdf)

Try adding Voltage Followers (https://www.google.com/search?q=buffered+op+amp+subtractor&espv=2&biw=865&bih=594&tbm=isch&imgil=zp_zITKOgDqOCM%253A%253B--bID70zpOsrMM%253Bhttp%25253A%25252F%25252Fwww.electronics-tutorials.ws%25252Fopamp%25252Fopamp_5.html&source=iu&pf=m&fir=zp_zITKOgDqOCM%253A%252C--bID70zpOsrMM%252C_&usg=__6OMIvqHD_Z_FZwWvJ64V7lkJQwI%3D&ved=0ahUKEwibjMalsvvLAhVSwWMKHfHdAwMQyjcILA&ei=N7oFV9uUH9KCjwPxu48Y#imgrc=zp_zITKOgDqOCM%3A) in front of R1 & R2


Connect TWO 10 k resistors in  series from +5V to GND you should measure 1.66 V and 3.33 V respectively at the two points where the resistors connect. If the Gain of your subtractor is really 2,  it should output 4.00 V when you connect V2 to 3.33V and V1 to 1.33V.  You  use whatever it outputs to "CALIBRATE"  it by deviding the output by 4.00 to obtain the "CORRECTION" Factor.

Vo = 2* (3.33V- 1.66V)
   = 2*2.00 V
   = 4.00 V

Suppose the output is 3.50V ( instead of 4.00V)

3.50 V /4.00 V = 0.875
1/0.875 = 1.14285714

Correction factor = 1.14285714
1.14285714* 3.50 V = 4.00. V
  For this case, a battery with a voltage of 1.4 V would (after dividing by gain) read 1.225 V
1.225 * 1.14285714= 1.40 V
Title: Re: Battery Voltage Measurement with OP Amp
Post by: Karsten_87 on Apr 07, 2016, 05:01 pm
Hi everybody,

thank you for all the kind and very valuable comments!


First, I used two NiMH batteries in series for testing purposes and the shown voltage is the actual one.

 So the charging starts close to the nominal voltage of 1.2V. Since the cell voltage is rising slowly at this point I thought I take this example to show the effect. Sorry for not making this clear in the first place!

@ jremington: The OP AMP I am using is a LM 358 with a maximal supply voltage and difference Voltage of 32V - so I don't think it that,

I also attached a complete circuit diagram of the current controlled battery simulator. Yes, the battery is floating. But since the OP Amp gives me only the differential voltage between the battery poles it should be ok.

@raschemmel: I compared the battery voltage with the Arduino outputs before with a reasonable precision. Like I said I charged the cells close from nominal voltage and expect a slow rise. Also it cannot be the cell because the "jump" also occurs with other cell chemistries, following the same pattern. The start of the "jump" varies a bit (2.43V - 2.443V) but the upper value is always 2.46.

I don't understand how a bleed resistor could solve that. Furthermore I want to get reasonable precise capacity values by integrating the current over time. For this any unmeasured current will increase my error quite a bit.

Also thank you for that suggestion with the correction factor. But I did this already (not as accurate as you) and got reasonable results for the hole voltage range, except the little "jump" always at this position.

I used 56k Ohm for R1 and R2 and 112k Ohm for R3 and R4

@MarkT: Decoupling sounds interesting. However I have no idea how to do this. Would you mind giving me an example or an online link for that? Also suggestions how to make the circuit more robust would be nice.

I tried today to use different ports of the Arduino, no success. But I narrowed down the error a bit: Next to the the figure with the whole circuit I attached my latest measurement: I connected the Vout of the OP AMP directly with the Multimeter and the Arduino (both measured against ground). The result is shown in test3.jpg. The OP AMP output is not governing the "jump".

So it must have something to do with the Arduino or how it is connected? Should I upload these connections as well?

Thank you again for your help!

Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 07, 2016, 05:49 pm
Your schematic isn't 100% clear to me.  For one thing, you have two nodes named Vout but I know they're not connected to each other.

From the red arrows, however, it seems that you have a mode where you charge the battery through a FET/shunt which is controlled to provide a constant current?

Since your cells get ground through this FET/shunt you are putting a common mode signal at the inputs to your op amp.  It sounds like this has the potential to cause the results you're seeing, although I don't have a good reason why it'd happen at the end of charge (when you'd expect the FET to be fully on).

Edited to Add:  wait...no...if your FET was fully on at the end of charge, then you'd have 12V across your battery.  I don't understand how your FET is controlled...
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 07, 2016, 06:10 pm
Quote
I don't understand how a bleed resistor could solve that. Furthermore I want to get reasonable precise capacity values by integrating the current over time. For this any unmeasured current will increase my error quite a bit. 
i

Quote
The OP AMP output is not governing the "jump". 
There is no "jump" . That's the battery charging current reducing to "0".

Whether you understand it or not, as long as you refuse to add the bleed resistor your results will remain the same, regardless of anything else you do. As I already stated, the battery or batteries only constitute a "LOAD" in so much as they need charging. Once they are charged, they no longer present a load to the op amp and the  op amp see's "no load" and the output oscillates at the last 10 - 15 mV. Adding a bleed resistor ensures that the op amp sees a load and the output will be whatever the charged voltage is because the bleed resistor current with respect to battery current is negligible and it should be obvious that once the battery is charged the charging current approaches 0 mA. Attempting to measure the charging current during the last 0.4 % of the charging cycle is a futile endeavor and you are wasting your time. Your circuit is complete. Just add the bleeder resistors and you should be done. (you may need to experiment a little with the value)
Also , you can't charge batteries in series because when one is charged it will stop conducting current. (that should be obvious) Thus the remaining battery will not get any charging current. (which is no doubt linked to your "problem") ( the op amp output is "floating" because  there is no load after one of the batteries is fully charged) Remove one battery and replace it with a load resistor. The current is the same through the resistor and the remaining battery.
The load resistor needs to drop 1.4 V @ 80% of charging current
R = 1.4 V/ Icharging

It's the most basic law (Ohm's Law) of electronics.
Current is a function of Difference of Potential and resistance. If the sum of the two batteries is the same as the charger output no current will flow.
At some point the current and difference of potential are so small there is nothing to pull down the op amp output and starts to "float" . What you are calling a "jump" (as if it shouldn't occur) is the battery's natural way of telling you one or both batteries are charged and no current is flowing.  If you simply accept that fact you can stop collecting data at that point and call it "charging cycle complete" indication. Otherwise, if you refuse to accept the obvious you can add the "bleeder " resistor, starting at the higher value and adjusting value lower until the "jump" disappears.


Quote
Also it cannot be the cell because the "jump" also occurs with other cell chemistries, following the same pattern. The start of the "jump" varies a bit (2.43V - 2.443V) but the upper value is always 2.46. 
That should tell you something, like , basically, any rechargable battery will no longer present a load once it's charged and your op amp will float and oscillate because it sees no load. (how can it with no difference of potential ? How can the output be pulled low if there is no current ? The only way to pull the output down when the battery is fully charged is to apply a lower voltage and start discharging the battery by sinking current instead of sourcing it. There are no other "options" You are either sinking current (discharging the battery), or sourcing it (charging the battery). You could alternate between the two to keep the voltage at some setpoint but if you simply command the op amp to an output voltage that is exactly the same as the sum of the two batteries, there will be no difference of potential and therefore no current. It's as simple as that. I can't believe how much you are making of a 15 mV fluctuation when the battery is charged. It's not showing up on your meter because it is averaging.  Try changing your code to take 10 to 20 readings and average them and print only the average.


"Jump" = (2.47V-2.45V) = 0.020 V
(@0.00488 V/ per analog count, this is 3 counts)

Based on the step size of the voltage steps, the "jump" appears to be 3 "steps" or analog counts (3*0.00488 = 0.01464 V) or just under 15 mV.

To call a 15 mV voltage change in a 0 to 2.47V range a "jump" is just ludicrous ( because it represents a 0.4 % voltage change)

Total Charged Battery Voltage = 2.47 V

"Jump"(%) = 100* (0.015 V/2.47V) = 0.59 %

I'm curious. How did you plot your readings ?
Did you use a Terminal Capture software and copy and paste into Excel ?

Quote
@MarkT: Decoupling sounds interesting. However I have no idea how to do this. Would you mind giving me an example or an online link for that? Also suggestions how to make the circuit more robust would be nice. 
Google "decoupling caps"
It's just a fancy name for a cap that shunts high frequency noise go ground based on the capacitive reactance (Google "capacitive reactance " and do the math for "Let f = 100000, f = 10000,  then f = 1000 and compare the three. What does that tell you about what the cap is doing ?. )
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 04:52 pm
Your schematic isn't 100% clear to me.  For one thing, you have two nodes named Vout but I know they're not connected to each other.

From the red arrows, however, it seems that you have a mode where you charge the battery through a FET/shunt which is controlled to provide a constant current?

Since your cells get ground through this FET/shunt you are putting a common mode signal at the inputs to your op amp.  It sounds like this has the potential to cause the results you're seeing, although I don't have a good reason why it'd happen at the end of charge (when you'd expect the FET to be fully on).

Edited to Add:  wait...no...if your FET was fully on at the end of charge, then you'd have 12V across your battery.  I don't understand how your FET is controlled...
OK, I put some time into it and I understand now.  If you don't mind, I added some labels to your schematic and reattached here.  I'd post it as a picture, but my employer seems to block the free picture hosting sites (BTW - how little sense does it make that the forums will host the pictures as attachments, but not as pictures?).

Edited to add:

So if I understand your circuit correctly, T1 is a voltage controlled resistance that results in a constant current through your cells.  When S1 is closed and S2 is in position 1, the cells are pulled up to 12V and T1 limits the charge current through the cells.  When S3 is closed and S2 is in position 2, the cells are pulled down to GND and T1 limits the cells discharge current.  Nice circuit.

It appears in your data you are always charging, which explains why your schematic shows the charging configuration.  If your cells are balanced, they should be at ~1.23V.  Are you sure they're both 1.23V?  If they are, then they should be far away from end-of-charge where their impedance might do weird things.

It seems like your control for T1 must be unstable.  As a test, when you see the A/D reading fluctuating like that, could you ground the + input to the amplifier controlling it to see if the A/D reading goes back to normal?

It also seems that the instability must be affecting your Arduino beyond just putting a high frequency on your cells.  If your multimeter was just averaging the signal clean, it would be in the middle of the Arduino's samples where your Arduino samples are clearly higher.  Possibly T1 being unstable could result in common mode signals getting through the op-amp, or maybe its ground currents aren't separated from the grounds of your op-amp and avr.

Seeing if the A/D readings go back to normal when T1 is forced off, however, would be the first step before figuring out how T1 is affecting your reading.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 05:23 pm
Quote
BTW - how little sense does it make that the forums will host the pictures as attachments, but not as pictures?
The answer is simple. After you post the attachments , click SAVE then put the mouse cursor over the attachment and right click "Copy Link Address", then click Modify, select the image toolbutton (computer monitor icon) and paste the link address into that, then click save.
done.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 05:30 pm
The answer is simple. After you post the attachments , click SAVE then put the mouse cursor over the attachment and right click "Copy Link Address", then click Modify, select the image toolbutton (computer monitor icon) and paste the link address into that, then click save.
done.
Well, simple would be the img button having an option to enter a URL or a local file.

But thank you for telling me the workaround.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 06:07 pm
Quote
Well, simple would be the img button having an option to enter a URL or a local file.
 
The link address of the attachment would be a "local file" but if I'm not mistaken , I think what you were asking was why isn't the forum software smart enough to know an image file extension of an attachment means it should be displayed as an image instead of us having to copy the link address URL and paste it into the image toolbutton prompt window ?

Quote
I also attached a complete circuit diagram of the current controlled battery simulator. Yes, the battery is floating. But since the OP Amp gives me only the differential voltage between the battery poles it should be ok.
If you are referring to the schematic posted in Reply#14, it is not complete. The comparator "+" input does not have any input voltage associated with it.
There is no part number for the fet.
There is no component type or value or label for the cylindrical object , (is that the battery ?)

There is also no circuit description for what appears to be a fet switched by a comparator and nothing more. Is that a modification or addition to the original op amp subtractor ?
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 07:27 pm
The link address of the attachment would be a "local file" but if I'm not mistaken , I think what you were asking was why isn't the forum software smart enough to know an image file extension of an attachment means it should be displayed as an image instead of us having to copy the link address URL and paste it into the image toolbutton prompt window ?
What I'm thinking of is something like adding pics on ebay, craigslist, gmail, etc:  when you push the "add picture" button you get a dialog in which you can identify a picture on your local PC or a URL on the web. 

If you are referring to the schematic posted in Reply#14, it is not complete. The comparator "+" input does not have any input voltage associated with it.
There is no part number for the fet.
There is no component type or value or label for the cylindrical object , (is that the battery ?)

There is also no circuit description for what appears to be a fet switched by a comparator and nothing more. Is that a modification or addition to the original op amp subtractor ?
The schematic certainly isn't complete, but it isn't that bad compared to other things posted to the forum.

Yes, the cylindrical objects are the two NiMH cells he mentioned in his post.  The FET being controlled by the op amp measuring the current through the FET is almost certainly a constant current control.  I was a bit confused by this circuit as I didn't see how he could achieve CC-CV charging, but then I remembered CC-CV is not how NiMH are charged.

He doesn't show the input to the current control op-amp.  I assume it's a steady reference, but if it's not then possibly that's the cause of his problems.

Hopefully he comes back and answers, as it is an interesting problem.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 08:39 pm
Quote
He doesn't show the input to the current control op-amp.  I assume it's a steady reference, but if it's not then possibly that's the cause of his problems.
There's no current control. That's a simple comparator. What is the cylinder under the comparator ? Is that a battery ? (or is it supposed to be a current limiting resistor ?)

Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 08:51 pm
There's no current control. That's a simple comparator. What is the cylinder under the comparator ? Is that a battery ?
That is a resistor.   It's drawn the same as his other resistors.

The current through the battery forms a voltage through that resistor.  The voltage is then fed back into that op amp.

Its an op amp because its drawn the same as his other op amp.  Also, in this application, it can not just turn the FET on and off.  It needs to operate in its linear region to form just the right resistance necessary for the desired current to flow.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 09:11 pm
If that's a current limiting resistor, then the value should be chosen to drop the difference after subtracting 1.4V +1.4V + VDSFET .

If we assume:


VDSFET = 1.0 V,

                              Battery 1 = 1.4 V (charged)
                              Battery 2 = 1.4 V (charged)
Then the battery needs to drop 12V -1.4V-1.4V-1.00V = 8.2V

If we assume a Charging current = 300 mA ,

Then RCL   = 12.0v/0.300 A = 27.3 ohms
PR =  0.300A * 8.2V = 2.46 W (round up to 3W)

RCL = 27 ohm/3W

Quote
he current through the battery forms a voltage through that resistor.  The voltage is then fed back into that op amp.
Yes, thats not feedback though, the comparator operates in open loop mode.
Thats the input voltage and the Vref for the comparator (http://www.electronics-tutorials.ws/opamp/op-amp-comparator.html) is the +V pin.


If +V > -V, Vout = +Vcc
 +V < -V, Vout = 0V

Vref = 8.2 V (in above example)

The "feedback " line , as you call it is connected to the wrong pin on the comparator.
The operation should be :
( 0.30 A * 27 ohms = 8.2 V)

If Icharging < 0.3A +V < 8.2V
If Icharging > 0.3A +V > 8.2V


If Icharging < 0.300 A (-V < 8.2 V)
Vout = +Vcc (fet ON)

If  Icharging > 0.300 A (-V > 8.2 V)
Vout = 0 V (fet OFF)

Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 09:27 pm
Yes, thats not feedback though, the comparator operates in open loop mode.
Thats the input voltage and the Vref for the comparator (http://www.electronics-tutorials.ws/opamp/op-amp-comparator.html) is the +V pin.


If +V > -V, Vout = +Vcc
   +V < -V, Vout = 0V

Vref = 8.2 V (in above example)

The "feedback " line , as you call it is connected to the wrong pin on the comparator.
The operation should be :

If Icharging < 0.300 A
Vout = +Vcc (fet ON)

If  Icharging > 0.300 A,
Vout = 0 V (fet OFF)
That is not a current limiting resistor.  It is a shunt to get a current signal.  The current in the resistor has the same direction whether charging or discharging due to S1-S3 changing state.  The current is limited by the FET operated in its linear region.

Honestly, if you don't know this stuff then it might blow your mind to discover this is the same principle behind how an LDO works.  Check out Fig. 2 -> Understanding LDO dropout.pdf (http://focus.ti.com/download/trng/docs/seminar/Topic%209%20-%20Understanding%20LDO%20dropout.pdf)

It's a very common circuit, actually.  I have one in my hand right now that uses an ATmega32U's DAC to control the positive input to the op-amp so that the current level is variable.  I verified its operation from 1uA to 2A with an 8.5 digit HP3458A.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 09:44 pm
Quote
That is not a current limiting resistor.  It is a shunt to get a current signal
Ok. I thought you said it was a resistor.

Quote
It's a very common circuit, actually.  I have one in my hand right now that uses an ATmega32U's DAC to control the positive input to the op-amp so that the current level is variable.  I verified its operation from 1uA to 2A with an 8.5 digit HP3458A. 
Thanks for the link. Do you have a schematic for the one you have ?
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 09:49 pm
Ok. I thought you said it was a resistor.
A shunt is a resistor.  It's just a resistor intended to convert a current into a voltage signal.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 10:20 pm
Can you post the Reply# of the post where the OP describes the current shunt you were talking about . I couldn't find it.

Quote
A shunt is a resistor.
I know what a shunt is. We use them here at work all the time but they are not ordinary resistors. The are made out laminated copper with a plastic mounting base. (and expensive)
We never call them resistors. We call them shunts because if you know what that is you know it is a special form of resistor.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 10:38 pm
Can you post the Reply# of the post where the OP describes the current shunt you were talking about . I couldn't find it.

I know what a shunt is. We use them here at work all the time but they are not ordinary resistors. The are made out laminated copper with a plastic mounting base. (and expensive)
We never call them resistors. We call them shunts because if you know what that is you know it is a special form of resistor.
Well, that's one kind of shunt.  When I designed batteries for electric vehicles, I used shunts like you describe -> http://www.digikey.com/product-detail/en/ohmite/TGHGCR0005FE/TGHGCR0005FE-ND/1817152 (http://www.digikey.com/product-detail/en/ohmite/TGHGCR0005FE/TGHGCR0005FE-ND/1817152)

Now that I'm doing power IC designs, I have much smaller shunts -> http://www.digikey.com/en/product-highlight/s/stackpole-electronics/hcs-series-high-current-shunt-sense-chip-resistors (http://www.digikey.com/en/product-highlight/s/stackpole-electronics/hcs-series-high-current-shunt-sense-chip-resistors)

As long as it's a resistor being used for current measurement, it's perfectly correct to call it a shunt.

And yes...as they get more and more accurate, with lower and lower temperature coefficients, they can get very expensive.

Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 08, 2016, 11:02 pm
How did you deduce so much about the OP's circuit when none of that is mentioned ?
(no mention of shunts, operating in the linear region etc etc etc) There's certainly no code posted and the schematic is not what you describe (whether you are right or not, that is not what the schematic shows). I'm interested in building a circuit like that. Can you post a link to an example project that includes a "REAL" schematic and code ? (I also use ATtiny85s which I would think would be ideal for that application) I built my own ATmega328/ATtiny85 Programmer board.

Incidently, the schematic on page 9-2 of the tutorial on LDO's shows a circuit similar to the comparator circuit the OP posted, with the exception that it has a voltage divider for -V input, and also the comparator drives a Gate drive device for the fet. I don't see a shunt there but the circuit is very similar. I haven't read it yet because I'm at work. I'll check it out tonight.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 08, 2016, 11:42 pm
How did you deduce so much about the OP's circuit when none of that is mentioned ?
(no mention of shunts, operating in the linear region etc etc etc) There's certainly no code posted and the schematic is not what you describe (whether you are right or not, that is not what the schematic shows). I'm interested in building a circuit like that. Can you post a link to an example project that includes a "REAL" schematic and code ? (I also use ATtiny85s which I would think would be ideal for that application) I built my own ATmega328/ATtiny85 Programmer board.
Well, while I used to proudly develop products, my job now entails making demo circuits for and reviewing customers implementations of my company's ICs.  Customers expect me to be able to recognize circuits like these without explanation.  I was confused yesterday as I couldn't tell how he got a CV mode out of this, but then today I realized that NiMH didn't need a CV mode.

I'm afraid I could only give you a schematic snippet of the electronic load that operates from 1uA-2A.  Here it is, and I hope it's enough to help you:



Incidently, the schematic on page 9-2 of the tutorial on LDO's shows a circuit similar to the comparator circuit the OP posted, with the exception that it has a voltage divider for -V input, and also the comparator drives a Gate drive device for the fet. I don't see a shunt there but the circuit is very similar. I haven't read it yet because I'm at work. I'll check it out tonight.
Yes, the LDO uses voltage feedback as the input to its error amplifier (please stop calling it a comparator...it's marked as Error Amplifier in the figure).  I hope that it actually does blow your mind, as I remember my mind being blown when I first learned how LDOs work.  It's a really simple concept, using feedback so that a FET can find the exact resistance it needs to control some output.  Once you understand how it works, a lot of the behaviors of LDOs become clear too.

Many LDOs can be used with current feedback instead of voltage feedback to create a current source.  For example this datasheet shows the LDO being used as a current source on page 17 -> http://cds.linear.com/docs/en/datasheet/3080fc.pdf (http://cds.linear.com/docs/en/datasheet/3080fc.pdf)

Honestly, the best way for the OP to probably solve his problem is to replace his homegrown circuit with an IC.  Although the cartoon in the datasheet shows something with the same detail as the OP's block diagram, we can see in my schematic snippet that there are other components.  While I'm not a great analog engineer (I almost never have to think about FETS in any state other than off/on), I know that this control can get tricky as the system is open loop if the FET is ever totally off.  It's very likely that the OP has some instability under some condition, where the engineer who designed the LDO is unlikely to have missed a stability condition.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 09, 2016, 12:04 am
That's an integrator with a very small cap.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 09, 2016, 12:15 am
That's an integrator with a very small cap.
Heh...well...the other super cool concept found in LDO's, is feedback and control.  The cartoon on Fig 2 of the first link I posted absolutely has integral feedback.  It's one of the things necessary to eliminate steady state error.  Explaining that part of feedback and control would be pretty difficult in a forum post, however.

The diff amp is comparing the DAC signal from the ATmega32U (connected to the + input) to the current signal developed by R14 as ISENSE.  Its output then drives MN1 until the op amp's + and - terminals are ~equal.  You can ignore MN2 and the resistors connected to it:  they're just there to provide a high load range.

I'm sorry I can't share the whole schematic (it's not mine but the engineer who made it was OK with me sharing this part), but if you recreate the section in the snippet you will draw current from LOAD+ proportional to the voltage on the op amp + input.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 09, 2016, 12:46 am
Quote
Although the cartoon in the datasheet shows something with the same detail as the OP's block diagram
Cartoon ? I assume that's just your wording. You do mean one of the figures in the datasheet , right ? (I didn't know datasheets had cartoons  :D ) I thought cartoons were things like Bart Simpson or The Flintstones)

Quote
The diff amp is comparing the DAC signal from the ATmega32U (connected to the + input) to the current signal developed by R14 as ISENSE.  Its output then drives MN1 until the op amp's + and - terminals are ~equal.   
So if I connect a rechargable battery to the connector at the top and use a DAC (https://www.adafruit.com/products/935) output the voltage the battery should have when it is charged, then it will charge the battery and stop when it's charged ?

I can use a Pro Mini for something like this. Is there anything special about the code ?
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 09, 2016, 01:11 am
Cartoon ? I assume that's just your wording. You do mean one of the figures in the datasheet , right ? (I didn't know datasheets had cartoons  :D ) I thought cartoons were things like Bart Simpson or The Flintstones)

So if I connect a rechargable battery to the connector at the top and use a DAC (https://www.adafruit.com/products/935) output the voltage the battery should have when it is charged, then it will charge the battery and stop when it's charged ?

I can use a Pro Mini for something like this. Is there anything special about the code ?
Lol...yeah, it's my wording.  I've heard other people call block diagrams cartoons, however, when they contain almost no detail.

This is a load box, so if you attach a battery to LOAD+ it is going to discharge it.  If you want to charge then you need the switches and +12V supply in the OP's block diagram.  Honestly, aside from it having a glitch with the A/D measurement, he really does have a good design for cycling NiMH batteries with an Arduino.

This circuit is only going to be good for CC charges, however, like those required by NiCd and NiMH cells.  It won't do CC-CV charges like you'd need for PbAc or Lion.  If you want to use it for NiCd and NiMH charges, however, then feel free.  It won't stop on its own when the cells are charged, however.  It requires the Arduino to monitor the cell voltages with a diff amp, like the OP is doing.  When the cells are charged the Arduino would then open the switches and then begin the discharge.

If only the OP would come back, we could get to the bottom of his measurement problem :)
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 09, 2016, 02:01 am
I don't see it as a problem. We're talking about 15 mV. That's hardly a problem.
His circuit doesn't show the  integrating ereor amp.

Were you trying to say that what I called a comparator was a "cartoon" representing all the circuitry not shown ?
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 09, 2016, 03:45 am
I suppose you could say that the OPs block diagram was enough of a cartoon for you to believe an error amp was a comparator.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 09, 2016, 04:59 am
I think there is misunderstanding here. I was never referring to the LDO tutorial. I was referring to the OP's "cartoon".  The fact that there is a similarity between his cartoon and Fig. 2. "LDO block diagram" on page 9-2 of the LDO Tutorial PDF is purely coincidental , unless you can provide proof that the OP has read that tutorial (or one like it ) and understands that it is just a cartoon and not a working schematic you could build.  There's nothing wrong with posting cartoons on the forum, but there is when you post it saying :

Quote
I also attached a complete circuit diagram of the current controlled battery simulator. Yes, the battery is floating. But since the OP Amp gives me only the differential voltage between the battery poles it should be ok.  
(Reply#11)

I'm not contesting that his circuit is not what you say it is. I am contesting that it is a complete schematic, which clearly it is not. You couldn't build that circuit. He didn't even label what you CLAIM is a shunt as "Shunt" . There are no circuit parameters, setpoints , part numbers etc.
Only "12V".  There is no code posted. My point is that here on the forum, posting something like that isn't going to do anyone else any good. Posting that and then filling in the blanks and explaining all that is not shown (like the integrating error amp) would do a lot of good.
Is this post only for the benefit of the OP ? Or, is it on an International Forum for the world to see so everyone can learn from it ? That's my point. Not that your wrong, but that the OP's so called schematic is really a "cartoon" , like the one on page 9-2 of the LDO tutorial, only much much less complete. You have done a lot to fill in the blanks by posting the link to that tutorial, but the "schematic" (or cartoon) in Reply #11 , is not something other forum users will be able to use. Where's the "alleged" shunt resistance value ? He said it is a battery "simulator". If it works then why are there no details or code ? How can anyone call it "complete" ?
If he replaced the cartoon with a "real" schematic showing the integrating Error Amp with component values and labels and the circuit for thr "percision voltage reference " connected to the +V op amp input , then it would make sense because the end of the shunt not connected to the -V op amp input is GNDed so the voltage drop across it   would be an accurate current measurement If you knew the shunt resistance value /tolerance spec, ! ( which you don't) . Do you see my point ? Posting a "carton" with no component values, no part numbers, no labels, and cidcuitry that isn't shown  and calling it a "complete schematic" is a travesty and violates the unwritten forum policy that schematic should be complete and if not should so stated. The idea is that circuits should be reproducable and repeatable and complete with component purchasing details such as shunt resistance value and vendor part number and link. In addition, the "Error Amp " circuitry ,  component values and labels should be provided instead of abrevuated as an error amp symbol with no "error amp" label. The fet part number should be given and the code used to run the circuit should be posted along with a data file containing the RAW data, (not a plot unless it is posted WITH the raw data.) A photo of the working circuit would be a nice plus. As it is, the schematic given is not enough to reproduce the circuit because no component values are provided ( you would THINK the OP would at LEAST have the courtesey to label the shunt resistance value so we could could correlate the accuacy with the data plotted.) I admit you can coose whatever shunt value you like from the Digikey link you posted but if the OP really BUILT that circuit ( which I STRONGLY DOUBT),  then he would kniw the value.
Thanks for posting the "real" schematic of the Load Box. At least that helps to fill in some of the blanks. Considering how much time you put into this thread, it's too bad the OP hasn't had the decency to at least respond to your commens about his circuit. You have done a lot to make some sense out of what can only be called a "cartoon" in Reply#11. Thanks for that.

Your response :
Quote
I don't understand how your FET is controlled...  
Clearly I'm not the only one with questions about that circuit...
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 09, 2016, 03:06 pm
Heh...relax man.  If vague circuit descriptions upset you, then this might be a bad place to hang out :)

I do wish the OP would come back though.  It sounds like he does have some odd secondary problem going on, something like an unstable control coupled into the A/D through a(n equivalent to) a bad layout. 

I don't think the OP owes me anything.  I chose to put my time into his puzzle.  I really would like to know how the story ends though.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 09, 2016, 04:40 pm
Vague circuit descriptions pitched as "vague" don't bother me but vague pitched as "complete "do.
So you don't believe the 15 mV "jump" is the result of the battery current reducing to zero when the voltage drop across the two series batteries equals the op amp output and the difference of potential reduces to zero ? Or that one of the two batteries
is charged and stops conducting current causing the other to float ?

BTW,
I'm Googling "arduino based current controlled battery charger " and so far haven't found anything like you described but I'll keep looking.

I did find this (http://www.electro-labs.com/diy-lithium-battery-charger-shield-arduino/) though.

and,
this (http://www.allaboutcircuits.com/projects/create-an-arduino-controlled-battery-charger/)

and ,
this (http://forum.arduino.cc/index.php?topic=92143.0) (but I can't find the datasheet for the LM2961. I think it may be a variant of the LM317 regulator)

I did find a TI chip LM2951 :
LM2951 (http://www.ti.com/lit/an/snva557/snva557.pdf)

and ,
this (http://www.vwlowen.co.uk/arduino/charger/circuit-lt1510-2.png)

There's several designs using the LM317 (http://www.ti.com.cn/cn/lit/an/snva581/snva581.pdf)

battery-charger-circuit-using-lm317 (http://www.circuitstoday.com/battery-charger-circuit-using-lm317)

LM7805 Battery  Charger (http://www.eleccircuit.com/wp-content/uploads/2009/09/constant-current-nicad-battery-charger.jpg)

YALM317 Charger (http://www.electronicecircuits.com/electronic-circuits/lm317-regulated-battery-charger-circuit) (Yet Another LM317 charger)

TOO many LM317 Battery Chargers (https://www.google.com/search?q=LM317++current+controlled++NiMh+battery+charger&biw=865&bih=594&tbm=isch&tbo=u&source=univ&sa=X&ved=0ahUKEwji_PXk94HMAhUX2mMKHfflDGIQsAQIJQ)


Universal LM317 Battery Charger
 (http://www.eleccircuit.com/nicad-battery-charger-by-ic-lm317t/)
I have  some mosfets and LT1215 op amps and some small caps and other components. I could try to combine your LOAD box integrator with the OP's cartoon . I don't have any current shunts at home but there's an electronic surplus store in Sunnyvale, called Halted Specialties that has lots of stuff. (I'm in Los Gatos, CA,USA)

FYI, since you're relatively new here , I should mention that there is a forum protocol that dictates that the "post" (the question) takes the highest priority (for the benefit or other forum members) and that if the OP abandons his post, it can be adopted by the members who were responding at the time of the abandonment . It can even be adopted by other members who stumble across it after the OP has left. The Global Moderator will wait some reasonable amount of time for the OP to return and if he doesn't the post will be left to whoever picks it up as long as they stay on topic and direct their replys to the subject of the original post. In such cases, a post can take on a life of it's own because there is no longer an OP to decide how the question or issue is to be resolved. Sometimes an OP will lay down some restrictions saying " I don't want to use this kind of IC or I don't want to use that kind of component. If the OP leaves, then basically any approach that accomplishes the goal of the original post is acceptable. If , however, the post strays off topic or ceases to address the issue originally presented, the Global Moderator will just lock it and that's the end of it.

That being said, any current controlled design that charges a battery (presumably NiMh) using an arduino and doesn't have the 15 mV oscillation when the battery is fully charged, would meet the design criteria specified. Since the OP's design is incomplete, the missing part would have to be "filled in" .(all the part numbers, values (like the shunt resistance value) , specs, (such as the Setpoint) , voltages etc, including components not shown (like the integrating error amp)) and last but not least the code.

Since it doesn't look like anyone else is going to do it  I completed the design, made a schematic , wrote the code, tested it, collected the data and took a screenshot of the scope monitoring the mosfet gate drive signal.

Now the post is complete and can be reproduced and replicated by anyone because it contains a "real" schematic (with component values) , data, code, and scope screenshot.
The circuit does , however , assume the op amp Subtractor exists to monitor voltage of the charging battery and control dac output accordingly. Attached code does not include that because I did not have the battery voltage monitor circuit at the time. I will update the software when I add that.






NOTE: There is an anomoly/"bug" with the current measurement code. I am attempting to "debug" this issue. Feel free to comment out that code in the mean time. The "bug" is that only the FIRST current measurement value printed is correct. All subsequent values are way off. I don't know what the cause is yet. S/W is not really my area of expertise so I may have done something stupid.

ALSO, I am currently adding the relay to the schematic. The code has already been updated to include the relay so the schematic is "lagging" the code at the moment. Sorry about that.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 10, 2016, 03:38 pm
Wow, you did a ton of work for this thread!  I looked through some of your links.  It looks like some of them are for Lithium chargers, which wouldn't be the same as NiMH.

Your final implementation looks like a CC charger for NiMH, but your results look like there's a problem.  The charging current should follow the setpoint, but you're getting a big jump from 1600 to 1790.  Your gate drive signal look like you're unstable as well.

Maybe the op amp is having difficultly driving the gate capacitance?  The cartoons show a "gate driver" block for a reason, even though it's sometimes as simple as two resistors.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 10, 2016, 04:58 pm
Unabreviated Table

12-bit Value (0 to 4095)                Charging Current
        1550                                                     20 mA
        1600                                                     30 mA
        1625                                                     40 mA
        1650                                                     50 mA
        1655                                                     60 mA
        1670                                                     70 mA
        1675                                                     80 mA
        1680                                                     90 mA
        1690                                                    100 mA
        1700                                                    120 mA
        1710                                                    140 mA
        1720                                                    170 mA
        1730                                                    190 mA
        1740                                                    220 mA
        1750                                                    250 mA
        1760                                                    280 mA
        1770                                                    320 mA
        1780                                                    370 mA
        1790                                                    490 mA
        1800                                                    520 mA

That seems to be the entire working range.
I'm rebuilding the circuit now because I had problem when I added the op amp subtractor and the associated analogRead code and serial prints of the voltage read. I thought I must have plugged a wire in wrong somewhere or pulled one out but I completely lost regulation
(it was either OFF or 520 mA) I thought it must be hardware but when I went back to the older code with no serial prints or analogReads it followed the above table. I surmised that the feedback loop timing was critical and the serial prints resulted in too long a delay between feedback and correction because the serial print was in the middle. I tore the whole circuit apart and am starting over. I also have to add the relay for the SPDT function. I will say this though, the analogRead worked in so much as if the value read back was even 1 analog count over the maximum in my IF statement (not in the code I posted), it would turn off the fet completely. If it was < than the maximum , it would turn on the fet on FULL with 520 mA
charging current. I found a 50 mV/5A shunt at Halted Specialties yesterday for $6.50.


Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 10, 2016, 07:02 pm
I would have expected the relationship between the setpoint and the current to be linear, where your data shows it to be an exponential.

And I would definitely expect to see a steady gate voltage if all of the other conditions were constant.  Honestly though, I'll admit again that I'm not a great analog engineer and driving FETs in their linear region can be tricky.  I'd personally buy ICs that do this stuff for me, designed by people who are great analog engineers. 

From which link did you get the exact circuit that you built?  I think you might be showing how circuits that are simple in the cartoons are actually difficult to implement as real schematics.

I'm about to get on a plane for San Jose BTW.  From what you've put in this thread, it sounds like you're near there.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 10, 2016, 08:04 pm
Quote
I'm about to get on a plane for San Jose BTW.  From what you've put in this thread, it sounds like you're near there. 
Welcome to San Jose !

If you can get away long enough to visit Santana Row on Stevens Creek just East of Winchester Blvd. there's a lot of really good restaurants. My favorites are Fogo de Chou and Sino but there's lot's more (West Bank is really popular too). Bring lots of money ha ha !

FYI, I just realized I goofed on the schematic design. The Gain of 3 non-inverting amp is supposed to be right after the DAC to boost the DAC 0 to 5V signal up to the 0 to 12V range the rest of the circuit operates in. I'm fixing that now (I hope ).

Have a nice trip. You can use UBER instead of a taxi when you get here. It's cheaper and less run around.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: Karsten_87 on Apr 11, 2016, 04:35 am
Wow!

Two days without Internet and as a result I need days to catch up properly ;) Thank you for all the effort you put into my problem.

After quickly reading the general results I feel a bit that I have to apologize for my unprofessional circuit diagram (and for calling it complete). I am sorry for that. I just begin to realize how difficult it is to explain something technical properly. In this regards I think that doubting that I really build that circuit and that I am not replying because I don't care is a little over the top, especially when considering that this is my first post here. And by the way: my plots contained the raw data. The running code just includes analog read and devide the number by 409.2 (once per second) But fair enough - I will try to be more clear in the future.

Tomorrow after work I will read your posts more closely and answer properly. Just very quickly:

The general idea for the circuit I got from:
Everything Including the Kitchen (current) Sink!
(https://www.eeweb.com/blog/eric_holland/everything-including-the-kitchen-current-sink)

I just added the charge functionality as big bobby described correctly. The trick of the circuit is that the shunt resistor has 1ohm. That makes the input voltage to the mosfet (same as in named source) equal to the current.

I don't want to use an predesigned IC for that since this is a bit of a learning exercise for me, which I will share of course when it works properly. In this regards I want to make clear that 15mV is not the end of the world (especially when considering other uncertainties like the shunt resistor).  My problem is more a fundamental one since I want to know what can cause such an error in general.

As big bobby mentioned, it might be my input signal or other frequencies messing around. My input signal is also sent by the same Arduino with a simple PWM signal which is smoothend by a third order low pass filter.

One of the things I noticed so far is that my current starts to get really noisy when I just increase the cable lengh between my discharge board and battery.

But like I said, I will work on a prober circuit diagram and post more as soon as possible.

Thanks everybody




 


Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 11, 2016, 03:25 pm
see attached for linked schematic


Sorry for underestimating you motivation. On the subject of doubting you built the circuit, I guess my only reply is that if you had this schematic in your possession since the beginning of your post and didn't post it or link it, but rather added it to the op amp subtractor without mentioning this schematic or linking it, there was no reason to believe that you knew the component values (since you did not provide them). That being said, thanks for finnally linking the (complete "real" )schematic. Since most new posters are newbies this one place where you don't get the "benefit of the doubt".
For better or worse, here on the forum, if you don't post it , it's because you don't know it. If you know it and don't post it , you are expected to "volunteer" to post it:
ie:
"Here's a rough sketch of the circuit. I can post the complete schematic on request"

The reason for this attitude , is that if you are posting for help. it is assumed you realize we need ALL THE INFORMATION relevant to the issue or circuit. I would think that would be obvious. How else can we help you. Anyway, now you know.



Perhaps I should have waited a week.  ;D (I might be guilty of being impatient)

I don't have time for detailed reply at the moment but that op amp driving the fet is looking more like a comparator configuration with setpoint input on +V and Feedback on -V.

Comparator (http://www.electronics-tutorials.ws/opamp/op-amp-comparator.html)


If VIN=SETPOINT
VRef = FEEDBACK
Let Setpoint = 3.7V
Feedback = 3.705 V

If the voltage dropped across the shunt > setpoint then (the BATTERY is CHARGED)
Vin <  Vref => Vout = -Vcc (0 v) FET TURNS OFF

Looks like a comparator, works like a comparator...

If it looks like duck, walks like a duck and quacks like a duck...it's a duck...


All component values shown. That's a complete schematic alright.


Quote
rom which link did you get the exact circuit that you built?  I think you might be showing how circuits that are simple in the cartoons are actually difficult to implement as real schematics.  
I sort of winged it. I built each section and then modified that section to work best with the others. I needed a lot of gain to amplify the shunt voltage.( my shunt is 50 mohm,
(1/20th the resistance of the OP's shunt)  The dac runs off 5V and everything else runs on 12V. The integrator was your idea.(from the Load box schematic)

I did determine that all the extra code other than the dac output code is causing the time between fet on pulses to extend. If I remove everything but dac code the fet drive signal looks like the posted screenshot. Now that I've add all the other code , this are long off times between the fet on pulses. (timing details later) If basically lowers the switching frequency.

I did some testing of the latest schematic I posted which includes the op amp subtractor and I used serial prints to indicate "Below setpoint...charging" and "Above setpoint, not charging.." By putting two 150 ohm 3 W resistors in parallel with the battery, I was able to force the circuit to hover at the setpoint , alternately charging and discharging simultaneously to observe the circuit behavior. It would alternately print "charging" and print the charging current or "not charging " and no current was drawn from the supply
(which has a current LCD display). By removing the 150 ohm load resistors, I could force the circuit to stop at the setpoint and remain there without charging. I have the Terminal Capture files of the serial prints. and I tried plotting the battery voltage but I was having a little problem with Excel so I don't have any plots yet. I'l take a look at the schematic of the "kitchen current sink" circuit and compare it to mine and see where to go from there.
I may try building the current sink to compare it's performance with my current circuit.
At is stands, I could technically say I have a current controlled battery charger that stops charging at the setpoint. The charging current setpoint is hard coded (the "current" variable)
Code: [Select]
uint32_t  current_setpoint =20;

I need to generate a more detailed current lookup table. (see Reply#39 for lookup table)


Quote
Maybe the op amp is having difficultly driving the gate capacitance?  The cartoons show a "gate driver" block for a reason, even though it's sometimes as simple as two resistors.  
The only gate drivers I have on hand are digital TC4427s. I need a linear gate driver. I am using an IRF730A (http://www.vishay.com/docs/91045/91045.pdf)

I made the mistake of trying a Logic Level mosfet (FQP30N06 from Sparkfun) and quickly discovered it switched to on full as soon as the gate voltage reached some threshold. The IRF730?A works fairly well in the linear region. I might be able to build a gate driver using an op amp.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 11, 2016, 10:33 pm
As big bobby mentioned, it might be my input signal or other frequencies messing around. My input signal is also sent by the same Arduino with a simple PWM signal which is smoothend by a third order low pass filter.

One of the things I noticed so far is that my current starts to get really noisy when I just increase the cable lengh between my discharge board and battery.

But like I said, I will work on a prober circuit diagram and post more as soon as possible.
You came back :)

Thanks for posting the link to your circuit.  I understand your desire to do this on your own as a learning experience.  It isn't simple to make feedback amps drive big FETs like this without having any instabilities, but I give you credit for trying!

Do you have an oscilloscope that you can use to look at the gate voltage, like raschemmel did in an earlier post?  If so, you should take a look at it when you see your voltage measurement start to fluctuate.  It'd be best to prove that the root cause of the measurement issue actually is due to instability in your current source.

Once you've proven that it is an instability, well...that's when you get to try stuff to make the instability go away.  Sadly this isn't my area of expertise, but if I were you I'd add R7, R19, C4, R2 from the schematic in this post (http://forum.arduino.cc/index.php?topic=391965.msg2702494#msg2702494) first.  The eeweb circuit doesn't include these things, but it also doesn't show test results for the circuit.  The schematic that I posted with the extra components was done by a great analog designer who deemed them necessary for stability at all operating points, and I verified his circuits operation myself with a calibrated high-precision DMM.

Other things you'd want to do, is make your feedback circuit very small and tight (like the eeweb pictures).  If long leads affect operation, it suggests noise is affecting your loop.

You also might try, as a test, replacing your PWM signal with a steady voltage source.  If that affects the stability, then you need more filtering on your PWM (or a DAC).

There's honestly a second phenomenon at work here too, BTW.  Even if your current source is unstable, your Arduino should still be able to monitor the battery voltage correctly.  Getting rid of the instability would probably be good enough for your project to work, however.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: BigBobby on Apr 11, 2016, 10:34 pm
Welcome to San Jose !

If you can get away long enough to visit Santana Row on Stevens Creek just East of Winchester Blvd. there's a lot of really good restaurants. My favorites are Fogo de Chou and Sino but there's lot's more (West Bank is really popular too). Bring lots of money ha ha !

FYI, I just realized I goofed on the schematic design. The Gain of 3 non-inverting amp is supposed to be right after the DAC to boost the DAC 0 to 5V signal up to the 0 to 12V range the rest of the circuit operates in. I'm fixing that now (I hope ).

Have a nice trip. You can use UBER instead of a taxi when you get here. It's cheaper and less run around.
Thanks for the tips!  My hotel is actually in Milpitas where I'll be staying for the week.  Most nights are scheduled for work stuff, but maybe I'll be able to take an Uber someplace some night this week.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 11, 2016, 11:05 pm
FYI, I used a MCP4725 I2C DAC instead of filtered PWM. For $5, why mess around with PWM when you can get a DAC for $5 ?


Quote
My hotel is actually in Milpitas where I'll be staying for the week.  
That's really not that far from where I used to work (not far from Great Mall in Milpitas).


It seems I blew up my DAC somehow by connecting it to the input of an op amp running on 12V . That shouldn't happen but it seems to be dead now. I ordered another. Maybe I'll buffer it with a voltage follower next time.

After extensive testing and some redesign and rewiring I have arrived at the following:
1-battery voltage is only accurate if a sufficient delay is inserted after turning on the relay which switches the battery "-" terminal from the mosfet DRAIN to the op amp subtractor V1 input pin. Previous error was caused by trying to measure the voltage before the relay contacts settled.
2-the relay does it's job perfectly. If the batt voltage is high enough the relay is left off and the battery cannot be charged because it is not connected to the fet.
3-the current measurement error previously was due to the voltage output of the shunt amplifier > 5.0 V. Once I discovered this I added a voltage divider and the measurements were consistent after that.


Program Output ("counts" = arduino analogRead counts)

Quote
batt_voltage counts =692.00
batt_voltage =8.49
batt_voltage counts =693.00
batt_voltage =8.50
Above Setpoint, not charging 8.49
batt_voltage counts =691.00
batt_voltage =8.48
Below Setpoint, charging...
batt_charging_led ON...
Current counts =  444
Current  =  0.27
batt_charging_led OFF...
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.49
batt_voltage counts =691.00
batt_voltage =8.48
Below Setpoint, charging...
batt_charging_led ON...
Current counts =  569
Current  =  0.34
batt_charging_led OFF...
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.48
batt_voltage counts =693.00
batt_voltage =8.50
Above Setpoint, not charging 8.49
batt_voltage counts =691.00
batt_voltage =8.48
Below Setpoint, charging...
batt_charging_led ON...
Current counts =  537
Current  =  0.32
batt_charging_led OFF...
batt_voltage counts =693.00
batt_voltage =8.50
Above Setpoint, not charging 8.49
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.50
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.48
batt_voltage counts =690.00
batt_voltage =8.47
Below Setpoint, charging...
batt_charging_led ON...
Current counts =  486
Current  =  0.29
batt_charging_led OFF...
batt_voltage counts =691.00
batt_voltage =8.48
Below Setpoint, charging...
batt_charging_led ON...
Current counts =  479
Current  =  0.29
batt_charging_led OFF...
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.50
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.50
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.48
batt_voltage counts =692.00
batt_voltage =8.49
Above Setpoint, not charging 8.49
batt_voltage counts =692.00  
Scope Screenshot Settings:
TIME/DIV: 1uS
Ch-1 : 5V/Div (gate drive signal)
Ch-2: 1V/Div  (amplified shunt voltage)


Title: Re: Battery Voltage Measurement with OP Amp
Post by: Karsten_87 on Apr 16, 2016, 09:05 pm
Hi everybody,

Since I got so many good suggestions I wanted to post my (this time hopefully) complete circuit diagram (I will post the values for the resistors/capacitors soon). However, I made one unprofessional simplification to make it more easy to read: The orange lines to the relays: Here I just use one I/O output from the Arduino connected to all three relais to change between charge (Relay 1 closed, Relay 2 open, Relay 3 "down") and discharge (Relay 1 open, Relay 2 closed, Relay 3 "up"). I wanted to change this just with one command in the code. I hope that is alright.

To the question why I am not using a DAC I have to explain a tiny bit more about the project:

As general idea I wanted to create a battery simulator where I can create dynamic current profiles to get practical data for battery models, test state estimation etc., without the need for expensive test hardware. Since we work with MATLAB to simulate batteries, and test our algorithms, I wanted to have something simple, without the need of converting data all the time. Luckily, I quickly had reasonable results with combining the shown current controlled circuit with the MATLAB Arduino IO Package (http://www.mathworks.com/matlabcentral/fileexchange/32374-legacy-matlab-and-simulink-support-for-arduino), allowing to control the Arduino directly with matlab code (only basic commands like analog write/read etc.). So after a bit of fiddling, now I am able to create current profiles (see figure current profile generation) very simple with a Simulink model and then run a loop from matlab code that works like this:

•   Read the ith value from the Simulink current demand vector and send the corresponding integer to the Arduino microcontroller board via USB.
•   Ask for the measurement of the voltage over the power resistor, calculate the current and store the value.
•   Ask for the measurement of the battery terminal voltage, calculate the voltage and store the value.
•   Wait until the next second
•   If the battery terminal voltage is too low: STOP

The result you can see in the figure measurement (The circuit I used for that contained just the discharge function. I used six NiMH cells in series and had just a voltage devider for the terminal voltage measurement). Therefore I want to stick to the simple low pass filter and avoid the DAC, since it allows me to directly use the "matlab code".

@  BigBobby: Thank you for your general suggestions. I will try to implement R7, R19, C4 , R2 from your diagram and see whether things improve.
Regarding the 1k resistance between the OP Amp and MOSFET I have one question: I found that my current signal got really messy when I try to increase the current (see figure one OPAmp). My solution for this was to use two LT1001 in parallel to increase the current output and it worked fine.

Won't a resistor limit the current again so that there is the possibility to run into the same issue again?

@raschemmel:
Your circuit diagram looks really interesting. Thanks for all the effort you invested in this! I have one question: What is the advantage of your feedback loop, using three LT 1215 OP Amps to drive the MOSFET? The first one just amplifies when I am right. Then you have a low pass filter next to the PWM output. After that the OP Amp is clear as well. But the one directly in front of the MOSFET raises only question marks :)

Best regards
Karsten

Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 16, 2016, 10:48 pm
op amp Integrator
Output is triangle wave.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 17, 2016, 06:53 am
So after all that why didn't you post your code ?
Title: Re: Battery Voltage Measurement with OP Amp
Post by: Karsten_87 on Apr 17, 2016, 12:14 pm
Hi raschemmel,

the code on the Arduino is directly available from the matlab IO package I mentioned. They provide the pde file and to my knowledge it should be downloadable for free. I don't know if this is the right place to post matlab code. However, in matlab its just a simple while loop running with analogue-write and read commands anyway.
Title: Re: Battery Voltage Measurement with OP Amp
Post by: raschemmel on Apr 17, 2016, 04:52 pm
So all your plots came from Matlab ?

Is Matlab free ?
Title: Re: Battery Voltage Measurement with OP Amp
Post by: Karsten_87 on Apr 17, 2016, 08:49 pm
Yes, the plots are made with Matlab. It is certainly not free. However, I am sure you can find test versions or other stuff if you want to check it out.