Sunday, 22 October 2017

EXAMPLE: SOIL MOISTURE SENSOR WITH THE ARDUINO.

This is a simple example for you to understand how you can use the soil moisture sensor in your projects with Arduino.
In this example, you’ll read the analog sensor output values using the Arduino and print those readings in the Arduino IDE serial monitor.

Parts required
For this example, you’ll need the following components:
  • 1x YL-69 moisture sensor
  • 1x Arduino
  • 1x Breadboard
  • 2x 220 Ohm Resistors
  • 1x Red LED
  • 1x Green LED
  • Jumper wires
CODE

Upload the following sketch to your Arduino board:

int rainPin = A0;
int greenLED = 6;
int redLED = 7;
// you can adjust the threshold value
int thresholdValue = 800;

void setup(){
  pinMode(rainPin, INPUT);
  pinMode(greenLED, OUTPUT);
  pinMode(redLED, OUTPUT);
  digitalWrite(greenLED, LOW);
  digitalWrite(redLED, LOW);
  Serial.begin(9600);
}

void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(rainPin);
  Serial.print(sensorValue);
  if(sensorValue < thresholdValue){
    Serial.println(" - Doesn't need watering");
    digitalWrite(redLED, LOW);
    digitalWrite(greenLED, HIGH);
  }
  else {
    Serial.println(" - Time to water your plant");
    digitalWrite(redLED, HIGH);
    digitalWrite(greenLED, LOW);
  }
  delay(500);
}

Open the Arduino IDE serial monitor to see the values. Then, try your sensor in a wet and in a dry soil and see what happens.
When the analog value goes above a certain threshold, a red LED will turn on (indicates that the plant needs watering), and when the value goes below a certain threshold, a green LED will turn on (indicates that the plant is ok).

No comments:

Post a Comment