Seeking help to finish my project. I can measure my voltage, but i am not able to switch my relay modules by a specific voltage on and off.
For example switch relay 1 on when voltage is 10v and off by 14v.
This is my existing code :
// Define analog input
#define ANALOG_IN_PIN A5 // Voltage measurment
int RelayControl4 = 4; // K1 not used Digital Arduino Pin used to control the relais
int RelayControl5 = 5; // K2 cuttoff lifepo4 by 12.4volt
int RelayControl6 = 6; // K3 cuttoff solar panel if Battery charge goes higher 14.8v
int RelayControl7 = 7; // K4 extra charge when Battery is low (extra charger extern)
// 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);
//-------------------------------------------------------------------------------------------
// pinMode(RelayControl4, OUTPUT);
// pinMode(RelayControl5, OUTPUT);
// pinMode(RelayControl6, OUTPUT);
pinMode(RelayControl4, OUTPUT); // K4
}
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)) + 0.2; // OFFSET +0.2
// Print results to Serial Monitor to 2 decimal places
// Serial.print("Input Voltage = ");
// Serial.println(in_voltage, 2);
//----------------------------------------------------------------------------------------------
in_voltage >= 13.0; digitalWrite; RelayControl4, HIGH; // NO7 K4 and COM X Connected (LED off)
in_voltage <= 12.0; digitalWrite; RelayControl4, LOW; // NO7 K4 and COM X
Serial.print("in_voltage = ");
Serial.println(in_voltage, 2);
delay(500);
}
The relay goes on but it doesnt change by changing the voltage.
Thanks for any help in adcance