Hello All, I am trying to create my own auto irrigation system for a herb garden.
Components:
Arduino Uno R3 X1
Capacitive soil moisture sensors HW-390 X4
8 Channel Relay Module 2PH77506A X1
5V Pumps X4
Code:
// Auto irrigation system, capacitive soil moisture sensors, 8 channel relay controling 5v pumps,
int RelayControl1 = 6; //Pump 1 relay
int RelayControl2 = 7; //Pump 2 relay
int RelayControl3 = 8; //Pump 3 relay
int RelayControl4 = 9; //Pump 4 relay
//int RelayControl5 = 10; //Spare
//int RelayControl6 = 11; //Spare
//int RelayControl7 = 12; //Spare
//int RelayControl8 = 13; //Spare
int SMD1R; // = A3; //Soil Moisture Data 1 Raw
int SMD2R; // = A2; //Soil Moisture Data 2 Raw
int SMD3R; // = A1; //Soil Moisture Data 3 Raw
int SMD4R; // = A4; //Soil Moisture Data 4 Raw
int SMD1A; //Soil Moisture Data 1 Adjusted
int SMD2A; //Soil Moisture Data 2 Adjusted
int SMD3A; //Soil Moisture Data 3 Adjusted
int SMD4A; //Soil Moisture Data 4 Adjusted
//int SMP1 = 2; //Soil Moisture Power 1
//int SMP2 = 3; //Soil Moisture Power 2
//int SMP3 = 4; //Soil Moisture Power 3
//int SMP4 = 5; //Soil Moisture Power 4
void setup()
{
Serial.begin(9600);
pinMode(RelayControl1, OUTPUT);
pinMode(RelayControl2, OUTPUT);
pinMode(RelayControl3, OUTPUT);
pinMode(RelayControl4, OUTPUT);
// pinMode(RelayControl5, OUTPUT);
// pinMode(RelayControl6, OUTPUT);
// pinMode(RelayControl7, OUTPUT);
// pinMode(RelayControl8, OUTPUT);
// pinMode(SMP1, OUTPUT);
// pinMode(SMP2, OUTPUT);
// pinMode(SMP3, OUTPUT);
// pinMode(SMP4, OUTPUT);
}
void loop()
{
Serial.println("debug 0.1");
digitalWrite(RelayControl1,HIGH); // deactivate relay 1
digitalWrite(RelayControl2,HIGH); // deactivate relay 2
digitalWrite(RelayControl3,HIGH); // deactivate relay 3
digitalWrite(RelayControl4,HIGH); // deactivate relay 4
delay(5000);
Serial.println("debug 1.1");
// digitalWrite(SMP1,HIGH); //Power Soil Moisture Sensor, granularity of readings from sensor was very coarse
//range of diffrence between fully submerged in water to dry was 10units (dont know units).
//sensor directly powered from 5V rail provides range of ~400units
delay(10000);
int SMD1R = analogRead(A3); //Read output voltage from soil moisture 1
int SMD1A = map(SMD1R,256, 553, 100, 0); //Convert to %
Serial.println(SMD1R);
Serial.print(SMD1A);
Serial.println('%');
if(SMD1A <= 40) //if soil moisture % <= 40%
{
Serial.println("debug 1.2");
do
{
Serial.println("debug 1.3");
digitalWrite(RelayControl1,LOW); //turn pump 1 on
Serial.println("Pump 1 On");
Serial.println(SMD1R);
Serial.print(SMD1A);
Serial.println('%');
delay(10000);
int SMD1R = analogRead(A3); //Read output voltage from soil moisture 1
int SMD1A = map(SMD1R,256, 653, 100, 0); //Convert to %
} while(SMD1A <= 40); //Pump remains on while soil moisture % is <= 40%
Serial.println("debug 1.4");
digitalWrite(RelayControl1,HIGH); //turn pump 1 off
Serial.println("Pump 1 Off");
Serial.print(SMD1A); //print moisture %, move to next sensor
Serial.println('%');
}
else;
{
Serial.println("debug 1.5");
Serial.print(SMD1A); //print moisture %, move to next sensor
Serial.println('%');
}
//digitalWrite(SMP1,LOW); //De-power Soil Moisture Sensor
Serial.println("debug 2.1");
delay(10000);
// digitalWrite(SMP2,HIGH); //Power Soil Moisture Sensor, granularity of readings from sensor was very coarse
//range of diffrence between fully submerged in water to dry was 10units (dont know units).
//sensor directly powered from 5V rail provides range of ~400units
int SMD2R = analogRead(A2); //Read output voltage from soil moisture 2
int SMD2A = map(SMD2R,280, 639, 100, 0); //Convert to %
Serial.println(SMD2R);
Serial.print(SMD2A);
Serial.println('%');
if(SMD2A <= 40) //if soil moisture % <= 40%
{
Serial.println("debug 2.2");
do
{
Serial.println("debug 2.3");
digitalWrite(RelayControl2,LOW); //turn pump 2 on
Serial.println("Pump 2 On");
Serial.println(SMD2R);
Serial.print(SMD2A);
Serial.println('%');
delay(10000);
int SMD2R = analogRead(A2); //Read output voltage from soil moisture 2
int SMD2A = map(SMD2R,280, 639, 100, 0); //Convert to %
} while(SMD2A <= 40); //Pump remains on while soil moisture % is <= 40%
Serial.println("debug 2.4");
digitalWrite(RelayControl2,HIGH); //turn pump 2 off
Serial.println("Pump 2 Off");
Serial.print(SMD2A); //print moisture %, move to next sensor
Serial.println('%');
}
else;
{
Serial.println("debug 2.5");
Serial.print(SMD2A); //print moisture %, move to next sensor
Serial.println('%');
}
//digitalWrite(SMP2,LOW); //De-power Soil Moisture Sensor
Serial.println("debug 3.1");
delay(10000);
// digitalWrite(SMP3,HIGH); //Power Soil Moisture Sensor, granularity of readings from sensor was very coarse
//range of diffrence between fully submerged in water to dry was 10units (dont know units).
//sensor directly powered from 5V rail provides range of ~400units
int SMD3R = analogRead(A1); //Read output voltage from soil moisture 3
int SMD3A = map(SMD3R,280, 620, 100, 0); //Convert to %
Serial.println(SMD3R);
Serial.print(SMD3A);
Serial.println('%');
if(SMD3A <= 40) //if soil moisture % <= 40%
{
Serial.println("debug 3.2");
do
{
Serial.println("debug 3.3");
digitalWrite(RelayControl3,LOW); //turn pump 3 on
Serial.println("Pump 3 On");
Serial.println(SMD3R);
Serial.print(SMD3A);
Serial.println('%');
delay(10000);
int SMD3R = analogRead(A1); //Read output voltage from soil moisture 3
int SMD3A = map(SMD3R,280, 620, 100, 0); //Convert to %
} while(SMD3A <= 40); //Pump remains on while soil moisture % is <= 40%
Serial.println("debug 3.4");
digitalWrite(RelayControl3,LOW); //turn pump 3 off
Serial.println("Pump 3 Off");
Serial.print(SMD3A); //print moisture %, move to next sensor
Serial.println('%');
}
else;
{
Serial.println("debug 3.5");
Serial.print(SMD3A); //print moisture %, move to next sensor
Serial.println('%');
}
//digitalWrite(SMP3,LOW); //De-power Soil Moisture Sensor
Serial.println("debug 4.1");
delay(10000);
// digitalWrite(SMP4,HIGH); //Power Soil Moisture Sensor, granularity of readings from sensor was very coarse
//range of diffrence between fully submerged in water to dry was 10units (dont know units).
//sensor directly powered from 5V rail provides range of ~400units
int SMD4R = analogRead(A4); //Read output voltage from soil moisture 4
int SMD4A = map(SMD4R,262, 625, 100, 0); //Convert to %
Serial.println(SMD4R);
Serial.print(SMD4A);
Serial.println('%');
if(SMD4A <= 40) //if soil moisture % <= 40%
{
Serial.println("debug 4.2");
do
{
Serial.println("debug 4.3");
digitalWrite(RelayControl4,LOW); //turn pump 4 on
Serial.println("Pump 4 On");
Serial.println(SMD4R);
Serial.print(SMD4A);
Serial.println('%');
delay(10000);
int SMD4R = analogRead(A4); //Read output voltage from soil moisture 4
int SMD4A = map(SMD4R,262, 625, 100, 0); //Convert to %
} while(SMD4A <= 40); //Pump remains on while soil moisture % is <= 40%
Serial.println("debug 4.4");
digitalWrite(RelayControl4,HIGH); //turn pump 4 off
Serial.println("Pump 4 Off");
Serial.print(SMD4A); //print moisture %, move to next sensor
Serial.println('%');
}
else;
{
Serial.println("debug 4.5");
Serial.print(SMD4A); //print moisture %, move to next sensor
Serial.println('%');
}
//digitalWrite(SMP3,LOW); //De-power Soil Moisture Sensor
Serial.println("loop"); //end of loop
Serial.println();
}
Schematic:
(yeah its jank I cant find a schematic for the 8 channel relay)
So the problems I am having are:
Starting code with all sensors in fully submerged conditions.
Behaves correctly while all sensors are submerged.
Reads sensor value, converts to % and prints, moves to next sensor, repeat for each sensor
If a sensor (same behaviour for all 4) is removed from water on next pass of loop it correctly reads sensor change and turns pump on.
Fails to turn pump off when sensor is re-inserted into water, sensor value reported does not change upon re-insertion to water.
Starting code in open air conditions.
Reads sensor value, turns pump on.
Fails to turn pump off when inserted into water
When each sensor is tested and calibrated in isolation they behave as expected correctly reading for both fully submerged and open air.