Turning on Solenoid for 45 seconds once it reaches soil moisture low value set

Hi ,

I have hooked up solenoid and soil sensor , so that when soil is dry it turns on solenoid,

however data coming from soil sensor is not always sticking to a particular value, so even if it touches a default set value that defines dry state of soil , next cycle of loop it may jump to not dry .

In that case solenoid will turn on and off quickly which I think can damage it.

so I have decided to turn solenoid on for 45 seconds no matter what

#include <elapsedMillis.h>

const int solenoidPin = 6;    // D6 : This is the output pin on the Arduino we are using
#define DHT11_PIN 8 // D8 to 2nd pin on DHT, leave 3rd pin unconnected 4th is gnd

const int8_t rainsense = 7; // analog sensor input pin A7
const int buzzerout = 2; // digital output pin D2 - buzzer output

int SENSE = 1; // Soil Sensor input at Analog PIN A1 Green wire
int val = 0;

int lightSensorPin = A8;
int analogValue = 0;

elapsedMillis timer0;

// the interval in mS
#define interval 45000

void setup() {
  timer0 = 0; // clear the timer at the end of startup
  pinMode(solenoidPin, OUTPUT); // Sets the pin as an output
  pinMode(buzzerout, OUTPUT);


void loop() {

  val = analogRead(SENSE);
  val = val / 10;

  int rainSenseReading = analogRead(rainsense);
  analogValue = analogRead(lightSensorPin);
  if (rainSenseReading < 500)  {
    rainMsg = F("It is raining heavily !");
  if (rainSenseReading < 300) {
    rainMsg = F("Moderate rain.");
  if (rainSenseReading < 200) {
    rainMsg = F("Light Rain Showers !");
  if (rainSenseReading > 500) {
    rainMsg = F("Not Raining.");
    SOLENOID_STATE = true;
  else SOLENOID_STATE == false;

  // Turn on Solenoid Valve if soil moisture value less than 25
  if (val < DRY_SOIL_DEFAULT && SOLENOID_STATE == true) {
    soilMsg = F("Soil is dry. ");
    // do not water pot if its raining or if it is night.
    if (SOLENOID_STATE || analogValue > 300) {
      soilMsg = soilMsg +  F("Watering the plant.");
      // turn solenoid off after 45 sec
      digitalWrite(solenoidPin, HIGH);
      digitalWrite(buzzerout, HIGH);
      if ((!SOLENOID_STATE) && (timer0 > interval)) {
        SOLENOID_STATE = false; // don't execute this again
        digitalWrite(solenoidPin, LOW);
        digitalWrite(buzzerout, LOW);


  else {
    digitalWrite(solenoidPin, LOW);
    soilMsg = F("Soil is damp.");
    SOLENOID_STATE = false;
    digitalWrite(buzzerout, LOW);

am I going correct? :o

so I have decided to turn solenoid on for 45 seconds no matter what

That is generally not a good idea. What you should do is have a dryEnoughToNeedWater value and a wetEnoughToNotNeedWater value that are NOT the same value.

Suppose that 19% relative humidity is what triggers the watering. You'd want to stop watering only when the RH got to %20, for instance.

  else SOLENOID_STATE == false;

== is NOT the assignment operator.

NOTHJNG belongs on the line after the else statement.

    // do not water pot if its raining or if it is night.
    if (SOLENOID_STATE || analogValue > 300) {

I can not see that the code matches the comment. analogValue is a dumb name. Is that supposed to be the light level or the soil moisture level? Why not use a meaningful name and eliminate the ambiguity.

SOLENOID_STATE is also a useless name.The solenoid either exists or it doesn't. The code doesn't make is exist or not. So, the variable must mean something else. The name should, therefore, be something else that reflects what the value does mean.

Thanks PaulS, I have fixed the assignment part to

else SOLENOID_STATE = false;

I was sceptical, whether should I be using this name, but got stuck in code so it was going, think I can call it


think I can call it

The solenoid may be moving, but it doesn't have feet, so it can't run.

By convention, all capital letter names are reserved for constants, which never appear on the left of an equal sign (after the initial assignment).