For my project, I designed a boost converter with arduino. I need to charge a 24 v Lead-acid battery with a converter.My arduino knowledge is not good. I can't keep the current constant in the beginning.I am using the ACS712 to measure the current of the battery. I would be very grateful if you could help me write the code.
thanks.
Sure. Put on a pot of coffee and I'll be right over to have a look.
Post the schematic, a few images of your project, spec sheets, and the code you wrote so far and what help you need with the next step.
Circuit
int potentiometer = A6;
int feedback = A0;
int PWM = 3;
int pwm = 100; //duty
int ACS712=A4; // current feedback
//const int chgVoltage = 520; // charging 28.2V
//const int stbyVoltage = 500; // float 26.8V
//const int chgCurrent = 533; // charging 0.5A --> 511 + (1 * 37.8) = 529
//const int stbyCurrent = 516; // float 0.1A --> 511 + (0.1 * 37.8) = 515
void setup() {
pinMode(ACS712,INPUT);
pinMode(feedback, INPUT);
pinMode(PWM, OUTPUT);
TCCR2B = TCCR2B & B11111000 | B00000001; // pin 3 and 11 PWM frequency of 31372.55 Hz
analogWrite(PWM,100);
delay(2000);
}
void loop() {
float voltageFeedBack = analogRead(feedback);
int currentFeedBack=analogRead(ACS712);
/////float///////
if (currentFeedBack < 515) { // float charge stage (CV at 26.8V)
if (voltageFeedBack < 500)
{
pwm = pwm+1;
pwm = constrain(pwm, 0, 245);
}
if (voltageFeedBack > 500)
{
pwm = pwm-1;
pwm = constrain(pwm, 0, 245);
}
analogWrite(PWM,pwm); //Finally, we create the PWM signal
}
///////abs charge//////
if (currentFeedBack > 515 && currentFeedBack < 533) { // topping charge stage (CV at 28.2V )
if (voltageFeedBack < 520.0)
{
pwm = pwm+1;
pwm = constrain(pwm, 0, 245);
}
if (voltageFeedBack > 520.0)
{
pwm = pwm-1;
pwm = constrain(pwm, 0, 245);
}
analogWrite(PWM,pwm);
}
/////////////////////////
if (currentFeedBack > 533) {
pwm = pwm-1;
pwm = constrain(pwm, 0, 245);
}
analogWrite(PWM,pwm);
}
edit post#4 and put the code in code tags, please.
The schematic the OP was not able to post.
The OP wired 24 volts into A6 of an Uno? Why?
R4 should be 10K, Voltage Divider Calculator might give clues to a Vdivider. Use 10K for R2.
U1 input is short circuited. There is a lot missing from the diagram, where is the rest of it? For example, the current sensor that is mentioned in the code...
Are you sure? That would produce max 12V at A0.
I typically default to R2 as being 10K for analog input impedance matching.
What happens in the case that voltageFeedBack == 500?
For that, to maintain a 0-2.4V output, the divider should be R2=100k R4=10k. It would drain the battery less.
We never got the "few images of your project and what help you need with the next step." parts of what was asked for.
If voltageFeedBack == 500 I don't want the duty to change.
I understand, I will replace that part with 10k. For reasons beyond my control, I could not get back to you right away.
Is the MCU a Uno? If so for more stable readings put a ceramic 103 cap into aref and gnd.
If there is a spare A:D pin, ground it. Before taking a reading read the grounded A:D to bleed off any residual charge on the sample and hold cap and then take a desired reading.
Next pick a software filter that will be used. I use the Simple Kalman Filter. A rolling or moving average filter will work well with a Uno.
Good luck.
There are also available some dedicated battery charger chips which will do the whole job for you .
Well, a PCB layout is "something". But typically when people ask to know how a circuit works it is the schematic they are looking for...
Why? If it is a method of hysteresis, it's set arbitrarily to 1/(number of ADC steps). It is probably too small in that case.
Here are some links that may help you. You may get idea about the coding from here: https://maker.pro/arduino/tutorial/how-to-build-an-arduino-based-buckboost-converter
If you ever consider making a Smart boost converter, here is a design that you may like to follow.
Smart PCB Design of Boost Converter - PCB Design & Layout - PCBway
We do not need constant current to charge lead-acid batteries.
I'm not sure you do. Because then the wording would be "replace those parts" because two changes should be made. But they are optional changes, the divider ratio itself is not a problem.

