More Picaxe Battery Charger Issues

sniper887

Member
Original thread with background on this problem:
http://www.picaxeforum.co.uk/showthread.php?26183-Picaxe-battery-charger-issues

I've tried the suggestions and had a couple of the circuit boards work, and some not work. I've tried diodes on the output, both in series with the battery being charged, and across the output reverse-biased (like for a relay coil). I've even tried separate power supplies for the charging power and for the Picaxe power and still have issues. I'm seriously thinking of abandoning this project because I seem to have gotten in over my head.
 

premelec

Senior Member
Please be more specific about WHAT doesn't work - the unit doesn't seem very complicated - perhaps something in the software?
 

sniper887

Member
The picaxe resets whenever I connect a battery. From what I saw on my oscilloscope there is ringing on the supply when the MOSFET turns off. (There's a picture in the previous thread of the o'scope trace)
 

premelec

Senior Member
Yes I saw that - I assumed you might have traced it down and fixed it... usually you can put in a sub-regulator for the PICAXE if absolutely necessary to smooth out any ringing in the supply and or change lead lengths, common grounds [V- connections] shorten leads etc. This is when you get to the art of actually functioning circuitry rather than 'ideal' components - takes a bit of sleuthing and persistence. Sometimes the oscilloscope traces are artifacts from spurious pickup also... One possibility is to turn off the MOSFET more slowly - so faster changes in current don't occur - however this will increase dissipation in the MOSFET some as well - but may cure your problem... persist! :) [Unless it's no longer fun... :-( ]
 

sniper887

Member
I tried separate power supplies with the ground being common and still the issue persisted. I'll try positive common and see how that works.
 

hippy

Technical Support
Staff member
My suspicion is that the problem is simply as described in the earlier thread; there is no current limiting resistor in the charging circuit and in-rush current when the battery is connected causes the power rails to collapse or start ringing.

I suspect the design may be flawed but I will admit I don't really understand how it's meant to work, what the theory of operation is.
 

srnet

Senior Member
I suspect the design may be flawed but I will admit I don't really understand how it's meant to work, what the theory of operation is.
I think its a form of PWM averaging, so the peak currents can be very high (even though the average is low) and can indeed vary depending on the actual type of NiMh battery, some may be fairly low impedance and thus draw a very high current, some types might be relatively high impedance and draw a lower current.

Not the standard way of charging NiMh for sure, normally you would use a constant current generator.
 

rq3

Senior Member
Original thread with background on this problem:
http://www.picaxeforum.co.uk/showthread.php?26183-Picaxe-battery-charger-issues

I've tried the suggestions and had a couple of the circuit boards work, and some not work. I've tried diodes on the output, both in series with the battery being charged, and across the output reverse-biased (like for a relay coil). I've even tried separate power supplies for the charging power and for the Picaxe power and still have issues. I'm seriously thinking of abandoning this project because I seem to have gotten in over my head.
Without building your circuit and putting a scope on it, this is just a SWAG, but...
R1, the 0.25 ohm resistor from the mosfet source to ground, must carry the entire battery charging current when the mosfet is ON. If the mosfet (and R1) are conducting say, 4 amps (certainly possible), the the mosfet source (and drain) is being raised by 1 volt. This may push the mosfet to the point where the gate voltage is no longer keeping the mosfet on, AND the poor picaxe is seeing the drain/source differential at its inputs.

The whole thing may oscillate (which you may be seeing as "ringing"), and Lord only knows what the picaxe is doing with the wild inputs from the mosfet and battery.

Just for grins, I'd try connecting the mosfet source directly to ground, and move R1 (if you really need it at all) between the battery and the mosfet drain. The picaxe as currently configured seems to be trying to measure the voltage drop across the mosfet, so I'm not sure that R1 really serves any purpose. It would really be mosfet dependent, but you almost (if you delete R1 as mentioned) have a PWM averaged constant current source right now, assuming you can characterize the mosfet ON resistance tightly enough.

Rip
 

AllyCat

Senior Member
Hi,

A circuit like that might be "good enough" to dim a LED (string), but IMHO it's quite unsuitable for charging a NiMH cell. Furthermore, with few resistor values shown and no indication what the software is trying to do, it's hard to know if you're even on the right track. How is the PICaxe supposed to measure the average charging current to the battery (i.e. from the voltage across the 0.25 ohm resistor) when the measured voltage/current wll be either zero or "very high" depending on the instanataneous phase of the PWM signal?

So first, is the PWM intended just as a method of A-D conversion (i.e. to allow the PICaxe to create an analogue voltage to control the charge current), or is it intended as an "efficient" method of controlling the power supplied to the battery? :

(1) In the former case, you need a low-pass filter before the gate of the FET, for example a large electrolytic from gate to ground. Then the FET will operate in a "linear" mode, dissipating significant power (i.e. the product of the current and proportion of the supply rail not "required" by the battery) so you will need to consider the FET heat sinking requirements.

(2) If the latter case, then you must put a filter choke (inductor) in series with the FET, together with a "flywheel" (energy recovery) diode from the Drain to the supply rail and a large decoupling capacitor on that supply rail. It would also be wise to use independent supply decoupling for the PICaxe. The required value for the inductor will depend on the charging current and the PWM frequency (neither of which you have told us about), but it might need to be quite large (i.e. having a "closed" ferrite or even iron core).

Once you have a circuit that is capable of controlling the charging current reliably, and then accurately measuring the current and the voltage across the battery, then we can consider how the PICaxe code might work.

Concerning your later question, there are numerous configurations to create a (hardware) "constant current" supply, but IMHO it can be easier to use a bipolar transistor, because its Base-Emitter voltage is more predictable (~700 mV) than the Gate-Source voltage of a FET.

Cheers, Alan.
 

sniper887

Member
The pwm is for efficient control of the power going to the battery. The code takes 100 readings on the current sense resistor and averages them to get a current reading. Otherwise I'd get large values or zero like you said. The code is on the previous thread linked to on the first page. PWM freq is about 1khz or so. If I did the buck topology you describe I'd need about 32kHz and 330 uh for the inductor, and a MOSFET driver (all of which I have)
 

premelec

Senior Member
You could try a larger drain resistor - which will limit current. R25 allows very large currents to occur - changing to 2R could help you track down the problem - you could just put 2R from the drain to R25 to still use the R25 as current sense - or you could follow Alan's good advice... :)
 

sniper887

Member
14m2 buck charger.jpg

How's this for a buck converter setup? I'm not sure how to hook up the Picaxe inputs to the circuit. I have one for the current-sense resistor I moved to between the 5V supply and the battery being charged, and one for the negative terminal of the battery. I just put in series resistors to (in theory) limit the current to the inputs. (NOTE: I later added the +5V to the MOSFET driver)
 
Last edited:

rq3

Senior Member
View attachment 17067

How's this for a buck converter setup? I'm not sure how to hook up the Picaxe inputs to the circuit. I have one for the current-sense resistor I moved to between the 5V supply and the battery being charged, and one for the negative terminal of the battery. I just put in series resistors to (in theory) limit the current to the inputs. (NOTE: I later added the +5V to the MOSFET driver)
How's this? Not good. You are chasing a lot of variables in a huge circle! First, you need to decide what it is you are trying to do. Charge a battery? To what end point voltage, and at what current? What battery? A lead-acid will tolerate a great deal of abuse, a Li-Ion, not so much (expect flames).

Your basic idea is OK. PWM a mosfet to provide pulses of current to the battery, and monitor the battery voltage to modulate the PWM to the mosfet.

If you're charging a 3.6 volt cell (or battery), you certainly don't need a buck converter since everything is running from 5 volts. What you DO need is tight control of the average current into the battery, based on some measured battery parameter. Take a deep breath, and re-think your approach. I think you've gotten into the classical "I'll do this because I can" method of engineering.

Take the Picaxe out of the design, temporarily, and consider only your mosfet, battery, and 5 volt supply. What do you need to provide to the gate of the mosfet to get current into the battery? If the current is too little, or too much, where can you get a signal to detect that? Now, can you use a Picaxe to measure the one, and control the other?

Hint: Yes you can! And it sounds like you have. But the translation from your breadboard to something "cleaner" and "tighter" is raising issues. Right? Now you're into power supply current capability, supply impedance, decoupling, etc. These are layout and supply issues, not schematic issues.
 

sniper887

Member
I am wanting to charge AA size NiMH batteries at 1A using a peak detection algorithm. Supply voltage is 5 volts. Taking the Picaxe out of the equation I would just do a constant current trickle charge at 200 mA seeing as I don't know how I could detect peak with discrete components.
 

rossko57

Senior Member
Taking the Picaxe out of the equation I would just do a constant current trickle charge at 200 mA seeing as I don't know how I could detect peak with discrete components.
That might be an area to look at. Picaxe doesn't know how to do it without external components either, so that can't just be thrown at the problem.
The suggested approach would be to 'invent' a peak current measuring technique. For simple example, a series resistor with some associated circuit to capture the peak voltage developed across it, and hold it long enough for you to read it with your multimeter. If you can read it, it should be simple enough to get Picaxe to read it.
 

Goeytex

Senior Member
Picaxe is probably not fast enough to read & respond to a quickly rising/falling current signal. At 64 Mhz it takes approximately 30 - 70 microseconds to execute a command, depending upon the command. By the time the Picaxe "sees" the 1 amp signal ( voltage across the sense resistor) and turns the FET gate off, probably 80 us will have passed and the current will have risen above 1 amp.

Using an X2 Picaxe with the internal comparator will speed things up possibly allow a crude buck type controller. However, this is somewhat advanced electronics with a lot of potential "gotchas". And, it does not lend itself well to prototyping on a breadboard due to the demands of a closed loop switching scheme.

I would think it would be cheaper and less frustrating for someone with limited electronics experience to simply use a battery charger IC than to try to shoehorn a Picaxe into an advanced application that is is not particularly well suited for.
 
Last edited:

premelec

Senior Member
@sniper887 - as Goeytex says there are a lot of chargers available - however perhaps if you want to try more things there could be a 'hybrid' solution where you use a commercially built 1 amp switching power unit with variable capabilities [many cheap ones on Ebay] and vary it's voltage / current with the PICAXE and then you don't need a lot of speed to detect trends which are measured and responded to by the PICAXE program and it could log maHr data and such if you wanted that......
 

hippy

Technical Support
Staff member
I am wanting to charge AA size NiMH batteries at 1A using a peak detection algorithm.
That should be possible but I don't see how PWM to a MOSFET will control the current by itself. The MOSFET will either be off and delivering no current or on and delivering the full current the battery tries to sink - which I think is where your problem is because you don't have the required resistive current limiting.

As noted in the earlier thread; when you add that current limiting you don't get the problems you see without it.

Perhaps start with something simpler and less demanding, build up to where you want to end. I created a proof of concept NiCd charger which proved batteries can be charged, delta-peaks can be detected. I can't find the thread but did (sursprisingly) find the source code on an old web site and that is fairly well documented and includes the full circuit used -

http://homepage.ntlworld.com/the.happy.hippy/picaxe/charger.txt

Note that code uses a couple of tricks, notable "FOR var = value TO samevalue" without any NEXT commands, which need to be change to "LET var=value" to make it pass syntax checking with the compilers we now have.

That set up was constrained to low current charging ( 25mA up to 75mA ) because I charge via the PICAXE pins to keep the hardware simple, but it shows the theory of operation and proves it can work. To go to higher currents you just need to switch on the charging voltage through a current limiting resistor rather than provide the charging voltage straight out of a PICAXE pin.

I connected battery -Ve to 0V and +Ve to the ADC because that was natural and allowed for easy measurement and calculation of the battery voltage. It should be possible to connect +Ve to +5V and use low-side switched charging though more complicated to determine the battery voltage.
 

AllyCat

Senior Member
Hi,

The code takes 100 readings on the current sense resistor and averages them to get a current reading. Otherwise I'd get large values or zero like you said.
It would be much easier to put a capacitor (from ground) on the relevant PICaxe A-D input pin(s) to act as a low pass filter. 100 iterations through PICaxe's interpreted Basic is likely far too slow for any "real-time" control loop.

The pwm is for efficient control of the power going to the battery. ....... PWM freq is about 1khz or so. If I did the buck topology you describe I'd need about 32kHz and 330 uh for the inductor, and a MOSFET driver (all of which I have)
If you want efficient power control (which is perhaps debatable for a 5V, 1A charger application) then you MUST use a buck (or similar) configuration. The inductor is essential for the energy conversion/recovery process. But what are you actually using? Also, I believe the flywheel diode cathode should go directly to the supply rail, since you (presumably) want the current sense resistor to monitor the total current flowing into the battery? My back-of-envelope calculation suggests about 0.5 A ripple with 330 uH at 30kHz; so I'd probably be looking at 1mH or above.

Finally, I thought that -dv/dt detection is the preferred method for end-of-charge detection of NiMH cells, particularly with higher charging currents. As others have said, why are you trying to reinvent the (square?) wheel?

Cheers, Alan.
 

sniper887

Member
Hi,

It would be much easier to put a capacitor (from ground) on the relevant PICaxe A-D input pin(s) to act as a low pass filter. 100 iterations through PICaxe's interpreted Basic is likely far too slow for any "real-time" control loop.

If you want efficient power control (which is perhaps debatable for a 5V, 1A charger application) then you MUST use a buck (or similar) configuration. The inductor is essential for the energy conversion/recovery process. But what are you actually using? Also, I believe the flywheel diode cathode should go directly to the supply rail, since you (presumably) want the current sense resistor to monitor the total current flowing into the battery? My back-of-envelope calculation suggests about 0.5 A ripple with 330 uH at 30kHz; so I'd probably be looking at 1mH or above.

Finally, I thought that -dv/dt detection is the preferred method for end-of-charge detection of NiMH cells, particularly with higher charging currents. As others have said, why are you trying to reinvent the (square?) wheel?

Cheers, Alan.
I guess my equation was different than yours for figuring out the inductor size. I used this I got from another forum user: L=[(Vin-Vout)*(D/Fsw)]/Iripple. My code does have a sort of -dv/dt detection. It first of all has closed-loop current control using the method I described above. Periodically it shuts off the PWM and samples the battery voltage, and stores the peak and the actual voltage. If the next voltage sample is higher than the last, the new peak is stored. If not, the peak remains unchanged and the actual is compared to the peak. If the actual voltage drops a certain level below the peak, a variable is incremented. If a new peak then occurs again, the same variable is reset to zero. If the actual voltage remains below the peak for a certain number of cycles(incrementing that variable each time), the charge terminates. I have had a couple circuits of this design actually work, and watched the battery voltage ADC value on the debugger hit peak and drop off.
 

AllyCat

Senior Member
Hi,

I think the difference in inductor value is only in the degree of "acceptable" current ripple, so perhaps 330 uH at 32 kHz would be "good enough".

What you describe seems reasonable enough, but doesn't really correlate with the (IMHO) "horrible" Schematic Diagram. If you show us the complete arrangement that you're presently using, we may be able to iron out any major issues.

I haven't looked in detail at the code posted in the previous thread as it has so few comments or meaningful variable names (hint), but the "constant current" loop must run extremely slowly, with 100 A-D PICaxe measurements between each INC or DEC of a PMW value with range 0 - 1023. BTW w10 = w10 - 1 min 0 is a bug and doesn't do what you think (hint: 0 - 1 MAX 0 = 65535).

Cheers, Alan.
 

sniper887

Member
Hi,

I think the difference in inductor value is only in the degree of "acceptable" current ripple, so perhaps 330 uH at 32 kHz would be "good enough".

What you describe seems reasonable enough, but doesn't really correlate with the (IMHO) "horrible" Schematic Diagram. If you show us the complete arrangement that you're presently using, we may be able to iron out any major issues.

I haven't looked in detail at the code posted in the previous thread as it has so few comments or meaningful variable names (hint), but the "constant current" loop must run extremely slowly, with 100 A-D PICaxe measurements between each INC or DEC of a PMW value with range 0 - 1023. BTW w10 = w10 - 1 min 0 is a bug and doesn't do what you think (hint: 0 - 1 MAX 0 = 65535).

Cheers, Alan.
Making easy-to-read schematics is something I need to work on. As for labels in the code, I should do that so I don't confuse myself. The updated schematic I haven't tried out yet, but it'll use a 5V switching power supply, and a single AA battery holder, both directly soldered to the PC board. For the bug in the code, I think I've been corrected on it before with W10 = W10 MIN 1 - 1 which should get me zero and not an overflow to 65535.
 

sniper887

Member
I decided to build the buck topology charger and so far it works. Still playing with the code but so far none of the previous problems have occurred.
 

AllyCat

Senior Member
Hi,

Thanks for the feedback; it's always encouraging when "theory" is proved to be correct. :)

In addition to increasing the speed of your (software) current feedback control loop, the other factor which worried me is that AFAIK the -dv/dt of a single NiMH cell at end-of-charge is only about 5 - 10 mV, i.e. very close to the resolution limit of the PIC(axe) A-D conveter (particularly when referenced to Vdd).

However, a well-known method to increase the (apparent) resolution of an A-D converter is to accumulate (sum) about 8 - 16 consecutive readings when there is a small amount of noise on the A-D input. The peak amplitude of the noise needs to be just a very small number of A-D quantising levels (i.e. LSBs), so you might find that running the PWM at a "trickle charge" current during the measurement gives a useful result.

Cheers, Alan.
 

sniper887

Member
Hi,

Thanks for the feedback; it's always encouraging when "theory" is proved to be correct. :)

In addition to increasing the speed of your (software) current feedback control loop, the other factor which worried me is that AFAIK the -dv/dt of a single NiMH cell at end-of-charge is only about 5 - 10 mV, i.e. very close to the resolution limit of the PIC(axe) A-D conveter (particularly when referenced to Vdd).

However, a well-known method to increase the (apparent) resolution of an A-D converter is to accumulate (sum) about 8 - 16 consecutive readings when there is a small amount of noise on the A-D input. The peak amplitude of the noise needs to be just a very small number of A-D quantising levels (i.e. LSBs), so you might find that running the PWM at a "trickle charge" current during the measurement gives a useful result.

Cheers, Alan.
I may try that sometime. Here's the code I'm using:
Code:
init:
setfreq m32
symbol lowbattery = b.4
symbol highbattery = b.1
symbol pwmoutput = b.2
symbol led = b.5


symbol current = w0
symbol currentcount = w1
symbol currentsum = w2
symbol dutycyc = w3
symbol voltcount = w4
symbol volts = w5
symbol voltavg1 = w6
symbol voltsum = w7
symbol peakvolt = w8
symbol deltapeak = w9
symbol modulus = w10
dutycyc = 0
current = 0
currentcount = 0
currentsum = 0
voltcount = 0
volts = 0
voltavg1 = 0
voltsum = 0
peakvolt = 0
deltapeak = 0


pwmout pwmoutput, 200, dutycyc 
low led

toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000
toggle led
pause 1000

pause 16000


start:
gosub battdetect


main:


for voltcount = 0 to 199


modulus = voltcount // 5
if modulus = 0 then
	toggle led
endif


current = 0
gosub getcurrent


if current < 48 then
	dutycyc = dutycyc + 1 max 900
endif

if current > 48 then
	dutycyc = dutycyc min 1 - 1
endif

pwmduty pwmoutput, dutycyc


next voltcount

gosub getvolts
if volts < 100 then init
if volts > peakvolt then
	peakvolt = volts
	deltapeak = 0
endif

if volts <= peakvolt then
	inc deltapeak
endif
if deltapeak > 5 then endchg

debug
goto main

getcurrent:

current = 0
currentcount = 0
currentsum = 0
for currentcount = 0 to 49
pause 7
current = 0
readadc10 highbattery, current
current = 1023 - current

currentsum = current + currentsum
next currentcount

current = currentsum / 50


return


getvolts:
volts = 0
voltsum = 0
voltavg1 = 0
pwmduty pwmoutput, 0

pause 8000

for voltavg1 = 0 to 19


readadc10 lowbattery, volts 
volts = 1023 - volts
voltsum = volts + voltsum
next voltavg1

volts = voltsum / 20

pwmduty pwmoutput, dutycyc



return

battdetect:
dutycyc = 500
pwmduty pwmoutput, dutycyc

gosub getvolts

if volts < 100 then start
dutycyc = 0
return

endchg:
pwmduty pwmoutput, 5
debug
high led
  pause 8000
  gosub getvolts
  
  if volts < 100 then 
  	goto init
  endif
pwmduty pwmoutput, 0
pause 24000
debug
goto endchg
 

Goeytex

Senior Member
The code is nice, but of no real value without an accurate schematic that shows all components and their values.
 

premelec

Senior Member
@hippy - I wonder what that reason was for using what you mention in 2006: [post #23 this thread]

"Note that code uses a couple of tricks, notable "FOR var = value TO samevalue" without any NEXT commands, which need to be change to "LET var=value" to make it pass syntax checking with the compilers we now have."

I changed these in your code and it still seems to have the same memory use with ed 5 - was it faster?
 

sniper887

Member
The code is nice, but of no real value without an accurate schematic that shows all components and their values.
Post #15 of this thread has the schematic used. I will eventually post a hopefully more legible one with all the component values. (not something I have much experience in though) The Picaxe has a .1uf decoupling cap, and there's also a 1000uf electrolytic cap on the board. I need to add a pulldown resistor between the pwm output and the MOSFET driver so it doens't float when the picaxe is being programmed.
 

hippy

Technical Support
Staff member
@hippy - I wonder what that reason was for using what you mention in 2006: [post #23 this thread]

"Note that code uses a couple of tricks, notable "FOR var = value TO samevalue" without any NEXT commands, which need to be change to "LET var=value" to make it pass syntax checking with the compilers we now have."

I changed these in your code and it still seems to have the same memory use with ed 5 - was it faster?
The FOR part of FOR-NEXT is just an initialisation, equivalent to "LET var = startValue", but it's optimised because there won't ever be any following maths in the assignment. Using that allowed a few bits of memory to be saved which was necessary at the time, and, conveniently, the old compilers never checked a FOR had a corresponding NEXT.

The later compilers better optimise "LET var = value" without additional maths in the assignment so they are the same size.
 

AllyCat

Senior Member
Hi,

Yes, that's much better. :)

Personally, I would have used a different pin than b.4 for the analogue voltage input, just in case I wanted to use the I2C bus (SDA) at a later date. Then put another 100nF on this voltage input because Microchip recommend a maximum source resistance of 10 kohms, unless a capacitor is fitted. It would also generally clean up the voltage input signal; then it should be possible to remove (or greatly reduce) the digital filtering (averaging) presently included in the software.

However, a low-pass filter incorporating 22 kohms and 100nF has a time constant of 2.2 ms, so if there is a large (step) change in the voltage or current level, then it's necessary for the software to pause for up to 10 ms for the transient to die away to a negligible level.

Perhaps grounding the battery and using a high side current source would have given a more bullet-proof design, but the AO3414 does look to be quite a nice device for 5 volt driver use.

Cheers, Alan.
 

sniper887

Member
Hi,

Yes, that's much better. :)

Personally, I would have used a different pin than b.4 for the analogue voltage input, just in case I wanted to use the I2C bus (SDA) at a later date. Then put another 100nF on this voltage input because Microchip recommend a maximum source resistance of 10 kohms, unless a capacitor is fitted. It would also generally clean up the voltage input signal; then it should be possible to remove (or greatly reduce) the digital filtering (averaging) presently included in the software.

However, a low-pass filter incorporating 22 kohms and 100nF has a time constant of 2.2 ms, so if there is a large (step) change in the voltage or current level, then it's necessary for the software to pause for up to 10 ms for the transient to die away to a negligible level.

Perhaps grounding the battery and using a high side current source would have given a more bullet-proof design, but the AO3414 does look to be quite a nice device for 5 volt driver use.

Cheers, Alan.
The use of pin b.4 was more of a concession to the circuit board design. I figured out with a .1 uF cap I'd need about a 330 ohm resistor to get a time constant about equal to 10 pwm cycles. I may still play with hardware filtering for the ADC inputs, as well as high side driving in later boards. I'll have to look at P-Channel MOSFETs. I appreciate the insight from all who have chimed in on this thread!
 
Top