Go Down

### Topic: Battery Voltage Measurement with OP Amp (Read 9523 times)previous topic - next topic

#### BigBobby

#30
##### Apr 09, 2016, 12:15 amLast Edit: Apr 09, 2016, 12:25 am by BigBobby
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.

#### raschemmel

#31
##### Apr 09, 2016, 12:46 amLast Edit: Apr 09, 2016, 12:58 am by raschemmel
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   ) 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 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 ?

#### BigBobby

#32
##### Apr 09, 2016, 01:11 amLast Edit: Apr 09, 2016, 01:16 am by BigBobby
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  ) 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 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

#### raschemmel

#33
##### Apr 09, 2016, 02:01 amLast Edit: Apr 09, 2016, 02:16 am by raschemmel
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 ?

#### BigBobby

#34
##### 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.

#### raschemmel

#35
##### Apr 09, 2016, 04:59 amLast Edit: Apr 10, 2016, 08:33 am by raschemmel
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.

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.

Quote
I don't understand how your FET is controlled...
Clearly I'm not the only one with questions about that circuit...

#### BigBobby

#36
##### 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.

#### raschemmel

#37
##### Apr 09, 2016, 04:40 pmLast Edit: Apr 13, 2016, 06:00 am by raschemmel
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 though.

and,
this

and ,
this (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

and ,
this

There's several designs using the LM317

battery-charger-circuit-using-lm317

LM7805 Battery  Charger

YALM317 Charger (Yet Another LM317 charger)

TOO many LM317 Battery Chargers

Universal LM317 Battery Charger

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.

#### BigBobby

#38
##### 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.

#### raschemmel

#39
##### Apr 10, 2016, 04:58 pmLast Edit: Apr 13, 2016, 04:13 am by raschemmel
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.

#### BigBobby

#40
##### 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.

#### raschemmel

#41
##### 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.

#### Karsten_87

#42
##### 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

#### raschemmel

#43
##### Apr 11, 2016, 03:25 pmLast Edit: Apr 11, 2016, 09:00 pm by raschemmel
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.   (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

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

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.

#### BigBobby

#44
##### Apr 11, 2016, 10:33 pmLast Edit: Apr 12, 2016, 05:51 am by BigBobby
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 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.

Go Up