Hello All!
So I have built a watering and heating system for my plants, I have chosen to use a relay to control all these systems, below is my code:
#define ntc_pin A2 // Pin, to which the voltage divider is connected
#define vd_power_pin 2 // 5V for the potential divider
#define temp_activate 4 // to activate the heater
#define nominal_resistance 10000 //Nominal resistance at 25⁰C
#define nominal_temeprature 25 // temperature for nominal resistance (almost always 25⁰ C)
#define samplingrate 10 // Number of samples
#define beta 3950 // The beta coefficient or the B value of the thermistor (usually 3000-4000) check the datasheet for the accurate value.
#define Rref 10000 //Value of resistor used for the voltage divider
int samples = 0; //array to store the samples
bool heater_on = false;
unsigned long heater_start_time = 0;
#define watering 7
const unsigned long day_since_watering = 1512000000L;
const unsigned long time_to_water = 100000L;
unsigned long startdate;
unsigned long currentdate;
unsigned long start_water_time;
unsigned long current_water_time;
bool pump_on = false;
void setup() {
pinMode(vd_power_pin, OUTPUT);
digitalWrite (watering, HIGH);
digitalWrite(temp_activate, HIGH);
pinMode(temp_activate, OUTPUT);
pinMode(watering,OUTPUT);
analogReference(EXTERNAL);
Serial.begin(9600);
startdate = millis();
}
void loop() {
//------------- boring calculation to find temp -------------//
uint8_t i;
float average;
samples = 0;
// take voltage readings from the voltage divider
digitalWrite(vd_power_pin, HIGH);
for (i = 0; i < samplingrate; i++) {
samples += analogRead(ntc_pin);
delay(10);
}
digitalWrite(vd_power_pin, LOW);
average = 0;
average = samples / samplingrate;
// Calculate NTC resistance
average = 1023 / average - 1;
average = Rref / average;
float temperature;
temperature = average / nominal_resistance; // (R/Ro)
temperature = log(temperature); // ln(R/Ro)
temperature /= beta; // 1/B * ln(R/Ro)
temperature += 1.0 / (nominal_temeprature + 273.15); // + (1/To)
temperature = 1.0 / temperature; // Invert
temperature -= 273.15; // convert absolute temp to C
Serial.print("Temperature ");
Serial.print(temperature);
Serial.println(" ");
delay(100);
if (( temperature <= 10) && (heater_on == false))
// Temperature less than 10 and the heater is off
// so turn the heater on
{
digitalWrite (temp_activate, LOW);
heater_on = true;
// Get the heater start time
heater_start_time = millis();
}
// Check if the heater has been on for 3 hours
// If yes, then turn it off
unsigned long check_time = millis()- heater_start_time;
if (((heater_on == true) && (check_time >= 1800000L)) || ((heater_on == true) && (temperature >= 15)))
{
digitalWrite (temp_activate, HIGH);
heater_on = false;
}
currentdate = millis();
if((currentdate - startdate >= day_since_watering) && (pump_on == false))
{
digitalWrite(watering, LOW);
pump_on = true;
startdate = currentdate;
start_water_time = millis();
}
current_water_time = millis();
if((current_water_time - start_water_time >= time_to_water) && (pump_on == true))
{
digitalWrite(watering, HIGH);
pump_on = false;
start_water_time = current_water_time;
startdate = millis();
}
}
The relay's schematic is linked here:
I have pretty much followed this man on Youtube, everything should be the same except the IN1 and IN2 pins (those are 4 and 7 respectively) :
My pump and heater is connected to the NO (Normally open) and COM of the relay, the relay is working but I have noticed that every time I power it on, it is connected automatically, (as in the NO and COM are connected) unless I have reach the temperature of 10C and then bring it back up to 15C. This is rather annoying because that would mean I have to wait 2.5 weeks for my watering system to work. I am not sure how I would fix this. I have tested this code with LED lights and it works normally.
Too Long Didn't Read: Relay only start working properly when it has reach the code's variables/ limits?
Many thanks in advance !!