How do I charge a Li-Ion Battery In A Charger.

Do I just put the battery in? Then what?

I admire your spirit of steadfast determination. Apparently you have never had a LiOn battery explode with you - I have. But, that is another story...

Looking at your circuit and proposal, I'm uncertain about the PWM concept - you are NOT changing current, rather you are changing "power". Just think, the peak voltage of the waveform and the internal resistance of the cell under load will determine the peak current. By varying the time interval, you put yourself into a temporal calculation: watt hours.
Check out the section Charging procedure here:

Good luck,


Isn’t that a little complex? I just want to know how to put a battery in a charger.

Do you need a Darlington? Your circuit looks like a credible complementary darlington to me, but if there were any mistakes I probably wouldn't spot them. How much current do you expect to flow through the LiPo?

Assuming that the circuit is correct, you have current control and voltage sense so the hardware side looks complete.

Is the charging algorithm one you made up, or one you know to be correct?

For the floating point math part I suggest you use float values for all literals to avoid the possibility of any integer intermediate values.

I'm not keen on the multiple conditional returns style of programming. I'd rather see the code measure the voltage (why not put the logic to do that in a separate function that returns a voltage?), do your range checks to decide what to do to the charge current, and then do it.

I don't understand the significance of the return value from this function - I assume it's just for status displays and not affecting any logic.

I'm not keen on repurposing the voltage variable d as the pwm duty cycle d. They are logically distinct and don't even have the same type. I'd prefer to have the duty cycle explicitly calculated and saved in a separate variable. And then give both variables meaningful names. I'm not sure I trust the algorithm you're using to calculate pwm. Can you explain in words what it's intended to do? How about extracting that algorithm in a separate function?

I recommend always using braces { and } to form compound statements around any conditional code even when there is only a single statement inside them - it avoids ambiguity when you have nested expressions, and also avoids the possibility of nasty errors if the single statement is ever expanded in future.

The logic of the if statements looks credible but personally I'd prefer to see them implemented as a sequence of if / else if in numerical order.

Thanks Peter but I'm not sure what you mean.

Bugger it.
I’ll try to get a straight answer elsewhere.
< omitted>
… so yes, it’s not feasible. You could have just said that.

I did say that. You went on and on about how you thought you had come up with a cheap charging circuit. You ask for a validation. I pointed out what I thought was an issue. I give you a link to a few words about constant current and constant voltage AND you show your gratitude by being rude.



Bear in mind also Peter that the arduino is limited in RAM and storage. So called "conventional" programming techniques can be memory hungry. This is not a problem on a small sketch but when you have a larger sketch doing various things, you have to compromise programming neatness for space considerations.

Thank you, I will do my best to bear those things in mind next time you ask for help with your code. :roll_eyes:

Ok Peter, you bear it in mind. It will serve you well.

I do get a little cross when people erase their original post so the entire thread now makes no sense :frowning:

@mmcp42, thanks for mentioning that. I was wondering why it didn't make sense to me.

I had been thinking of making a contribution but as the OP is messing about I won't bother.