Turn on/off LED using voltage range as a criteria

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);

}

}

Welcome to the forum

  if ( in_voltage & gt; = 0.00 & amp; & in_voltage & lt; = 10.00)

Where did you get the code ?
It appears to be full of HTML commands

I dont understand this. Could you help modify my code

Please start by explaining where you got the code as I assume that you did not write it. If you did write it then please explain what you think that line of code and other lines like

Serial.print("Input Voltage = ");

actually do

this is dronebots leson on interfacing voltage sensor with arduino for printing out dc voltages on the serial monitor. I modified it by adding output pins that represent different voltage levels with the aim of turning on/off leds or relays at those levels

You seem to have messed up the original code when you copied it. How did you copy it ?

Here is the first example

 Arduino DC Voltage Demo 1
  dc-voltage-demo.ino
  Use Arduino A/D converter to measure voltage
  Use external voltage divider with 30k & 7.5k resistors
  Results displayed on Serial Monitor
 
  DroneBot Workshop 2021
  https://dronebotworkshop.com
*/
 
// 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;
 
void setup(){
   // Setup Serial Monitor
   Serial.begin(9600);
   Serial.println("DC Voltage Test");
}
 
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);
}

Which of the examples did you start with ?

Is it even getting compiled?

Do you have an IDE available ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.