You are on page 1of 3

Soil Moisture and Relay code with Arduino

int Moisture = 4; // connect ir sensor to arduino pin 2


int Relay = 6; // conect Led to arduino pin 5
void setup()
{
Serial.begin(9600);
pinMode (Moisture, INPUT); // sensor pin INPUT

pinMode (Relay, OUTPUT); // Led pin OUTPUT


}

void loop()
{
int statusSensor = digitalRead (Moisture);
Serial.print(statusSensor);
Serial.print("\t");
if (statusSensor == 0 )
{
digitalWrite(Relay, HIGH); //Relay LOW
}

else
{
digitalWrite(Relay, LOW); // Relay High
}

}
ANALOG PIN Code Soil Moisture and Relay code with Arduino

int relayPin = 6;
int sensor_pin = A0;
int output_value ;
void setup()
{
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
pinMode(sensor_pin, INPUT);
Serial.println("Reading From the Sensor ...");
delay(2000);
}

void loop()
{
output_value= analogRead(sensor_pin);
output_value = map(output_value,1024,10,0,100);
Serial.print("Mositure : ");
Serial.print(output_value);
Serial.println("%");
if(output_value<20){
digitalWrite(relayPin, LOW);
}
else
{
digitalWrite(relayPin, HIGH);
}
delay(1000);
}

You might also like