Back to Parent

/*  Soil Mositure Basic Example
    This sketch was written by SparkFun Electronics
    Joel Bartlett
    August 31, 2015

    Basic skecth to print out soil moisture values to the Serial Monitor

    Released under the MIT License(http://opensource.org/licenses/MIT)
*/


int val = 0; //value for storing moisture value
int soilPin = A0;//Declare a variable for the soil moisture sensor
int soilPower = D7;//Variable for Soil moisture Power
int ledPin = D2;
int buttonPin = D3;
int buttonState = 0;
int previousState = 0;
//Rather than powering the sensor through the 3.3V or 5V pins,
//we'll use a digital pin to power the sensor. This will
//prevent corrosion of the sensor as it sits in the soil.

void setup()
{
  Serial.begin(9600);   // open serial over USB
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(soilPower, OUTPUT);//Set D7 as an OUTPUT
  digitalWrite(soilPower, LOW);//Set to LOW so no power is flowing through the sensor
  Particle.variable("buttonState", buttonState);

}

void loop()
{
  buttonState = digitalRead(buttonPin);
  digitalWrite(ledPin, buttonState);
  if(buttonState == HIGH && previousState == LOW)
  {
    //do something
    Serial.print("Stop it");
    Particle.publish("stopIt", "HIGH");
  }
    else
    {
      Serial.print("Soil Moisture = ");
      //get soil moisture value from the function below and print it
      Serial.println(readSoil());

      //This 1 second timefrme is used so you can test the sensor and see it change in real-time.
      //For in-plant applications, you will want to take readings much less frequently.
      delay(1000);//take a reading every second
    }

  previousState =  buttonState;

}
//This is a function used to get the soil moisture content
int readSoil()
{

    digitalWrite(soilPower, HIGH);//turn D7 "On"
    delay(1000);//wait 1000 milliseconds
    val = analogRead(soilPin);//Read the SIG value form sensor
    if(val < 1000)
      {
        Particle.publish("soilDry", "HIGH");
        digitalWrite(ledPin, HIGH);

      }
    else
      {
          digitalWrite(ledPin, LOW);


      }

    digitalWrite(soilPower, LOW);//turn D7 "Off"
    return val;//send current moisture value
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0