How to control moisture sensor using arduino

Materials:-



Diagram:-

Connections:-

moisture sensor to arduino
  • Senor VCC = Arduino 5v
  • Sensor GND = Arduino GND
  • Sensor D0 = Arduino digital pin 8
LED to arduino

  • LED ( - )  = Arduino GND
  • LED ( + ) = Arduino digital pin13  

Code:-

int led = 13;
int sensor = 8; 
int val; 
void setup() {
  pinMode(13,OUTPUT); //Set pin 13 as OUTPUT pin
  pinMode(8,INPUT); //Set pin 8 as input pin
}

void loop() { 
  val = digitalRead(8); //read the sensor
  if(val == HIGH) 
  {
  digitalWrite(13,LOW); //LED turn on
  }
  else
  {
  digitalWrite(13,HIGH); //led turn off
  }
  delay(400);

}