Hello,
I am trying to cut off charging for a battery when the voltage reaches 13.8V and as well turn on a green Led to indicate battery is full. similarly, i want the same for the low voltage cutoff; That means when voltage gets to 10V and below, it triggers a relay that disconnects the load from it. I modified a sketch from the lessons i got from Dronebot on the use of voltage sensor and my code is not working whether on proteus or on ground. kindly assist.
// Define analog input
#define ANALOG_IN_PIN A0
// Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;
// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;
// Float for Reference Voltage
float ref_voltage = 5.0;
// Integer for ADC value
int adc_value = 0;
// other parameters for battery control and user interfaces
int redled = 13;
int greenled = 12;
int blueled = 8;
int cutoffhigh = 7;
int cutofflow = 4;
void setup(){
// Setup Serial Monitor
Serial.begin(115200);
Serial.println("DC Voltage Test");
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(blueled, OUTPUT);
pinMode(cutoffhigh, OUTPUT);
pinMode(cutofflow, OUTPUT);
}
void loop(){
// Read the Analog Input
adc_value = analogRead(ANALOG_IN_PIN);
// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 1024.0;
// Calculate voltage at divider input
in_voltage = adc_voltage / (R2/(R1+R2));
// Print results to Serial Monitor to 2 decimal places
Serial.print("Input Voltage = ");
Serial.println(in_voltage, 2);
// Short delay
delay(500);
if( in_voltage >=0.00 && in_voltage <=10.00){
digitalWrite(cutofflow, HIGH);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);
}
else if(in_voltage >=10.10 && in_voltage <= 10.80){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);
}
else if( in_voltage >10.81 && in_voltage <= 12.29 ){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, HIGH);
}
else if (in_voltage >= 12.30 && in_voltage <= 12.49){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, HIGH);
digitalWrite(blueled, LOW);
}
else if (in_voltage >12.50 && in_voltage >=12.6){
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, HIGH);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);
}
else {
digitalWrite(cutofflow, LOW);
digitalWrite(cutoffhigh, LOW);
digitalWrite(redled, LOW);
digitalWrite(greenled, LOW);
digitalWrite(blueled, LOW);
}
}