r/ArduinoProjects • • Nov 15 '23

Battery charging circuit switch problem

We are charging a battery that is suppose to switch. It will be charging when its below 4.99, the range is between 4.65 and 4.99. Anything above 4.99 should go to load.

Our problem is that it doesn't switch. We messed with the code (that code isn't uploaded) and when it would switch, the battery would discharge and need to be charge again, so it switches back and forth rapidly. We added a delay but the delay seemed to mess with parts of our code. We made changes and now it doesn't switch.

We are using a relay and the arduino will be reading 5V and tell the relay to switch.

We asked chatgpt but it made the code more complicated.

// constants won't change

const int RELAY_PIN = 7;// the Arduino pin, which connects to the IN pin of relay

float readpinA = A3;

int readval;

float V=0;

String myString(" V = ");

// the setup function runs once when you press reset or power the board

void setup() {

// initialize digital pin as an output.

pinMode(RELAY_PIN, OUTPUT);

pinMode (readpinA, INPUT);

Serial.begin(9600);

}

// the loop function runs over and over again forever

void loop() {

readval=analogRead(readpinA);

V= (5./1023.)*readval;

Serial.print(myString);

Serial.println(V);

if ( ((V>=4.65) && (V<=4.99)) ||(V>4.99)) {

digitalWrite(RELAY_PIN, HIGH);

delay(10000);

}

else {

digitalWrite(RELAY_PIN, LOW);

delay(10000);

}

}

0 Upvotes

1 comment sorted by

1

u/tipppo Nov 16 '23

It's not really clear exactly what you want to do, your description doesn't make sense, but maybe this will work:

if (V<4.65) {

digitalWrite(RELAY_PIN, HIGH); // turn off load, turn on charging delay(10000); } if (V<=4.99) { digitalWrite(RELAY_PIN, LOW); // turn off charger, turn on load delay(10000); }

When voltage goes below 4.65V the relay activates. It will remain activated until voltage reaches 5.0V. What kind of battery are you using? Are you measuring the output of a voltage regulator and no actually the battery? It would help to see a drawing of your circuit.