Triggering the pump with the hygrometer

Now we need to update our sketch so that we trigger the relay for 5 minutes when the soil humidity sensor reads dry soil. Here is the updated sketch:

/* Gardening system
* Lets water our plants on time
*/

// humidity sensor is at analog port 0
const int SENSOR_PORT = A0;
#define RELAY_OUTPUT 9

int sensorValue = 0;

void setup() {
// We will initialize the serial port so we can print humidity data to the serial port.
Serial.begin(9600);
pinMode(RELAY_OUTPUT, OUTPUT); // Set the pin to digital output mode
digitalWrite(RELAY_OUTPUT,LOW);
}

// the loop routine runs over and over again forever:
void loop() {
// Read the input on analog pin 0:
sensorValue = analogRead(SENSOR_PORT);
// A well watered soil will give a reading of atleast 380 to 400, a fully moist soil will have reading of 1023.
sensorValue = constrain(sensorValue, 400, 1023);
// We will reverse the range so that we get a rough humidity percentage reading i.e.
// fully moist soil will give a reading of 100% for 100% humidity and
// dry soil will read 0% for 0% humidity
sensorValue = map(sensorValue,400,1023,100,0);

// Print humidity value
Serial.println(sensorValue);

if (sensorValue <= 20) {
Serial.println("soil is dry. switching on the PUMP");
digitalWrite(RELAY_OUTPUT,HIGH);
delay(300000);
} else
{
Serial.println("soil is wet");
digitalWrite(RELAY_OUTPUT,LOW);
}

delay(1000);
}

Upload this code to the sketch, dip the pump in the water reservoir, power everything up, and test this setup by dipping the hygrometer electrodes in water and taking them out to check that the pump triggers when the electrode is in the air (low humidity) and turns off when the electrodes are in water (high humidity).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.30.162