LCDless auto irrigation(watering) code without delay


im trying to make auto irrigation code without delay (and also LCDless) by using 'millis()' .

Components are

  1. solenoide valve connected with relay.

  2. soil moisture sensor.

  3. arduino uno r3.

proto type is as follows.

int SMS = A0; // soil moisture sensor
int relay = 10; // relay
unsigned long t2 = 0; // previous time
const long delaytime = 30000; //  target time

void setup ()
  pinMode(relay, OUTPUT); 
  Serial.begin (9600);

void loop ()
  unsigned long t1 = millis(); // current time
  float output = analogRead(SMS); 
  float BP = 100; // base point water content


  if (t1 - t2 >= delaytime)
    if (output < BP)
      digitalWrite(relay, HIGH);

      digitalWrite(relay, LOW);
    t2 = t1;

but... it doesn't work thoughtfully and needs improvement.

purpose of this code is

  1. To provide sufficient water for a fixed time. (like 30000ms above)
    for a long-term experiment, to keep plants from dying in hot weather.

  2. Could i inversion if (output < BP) to if (t1 - t2 >= delaytime) ?
    coz i think irrigation is most important part of this code.

  3. If the soil moisture rises, I want to watering it for a fixed time.
    but now , when soil moisture increases, it stops in a short time.

if bros has some better way to make those things better, plz give me some advices.


I've done this.

You need t save off two values per sensor.
Dry when the sensor is being held in dry air. NOT laying on wood.
Mud when its sitting in a glass of water.

Then you use a mapper function to map A0 -> Dry..mud -> 0..100 giving you percent moisture. You need Dry & Mud values for each sensor.

User sets percentage of wetness she wants. Typically starting at 40%

CheckTimer : very about 10 sec or so check the moistures when in reading mode
waterTimer : How long the user wants the water to run when the moisture falls below set.
soakTimer : How long to ignore the sensor after watering to let the ground soak the water.

You will want to setup a max number of waterings in a row to kill the system if you are having no measurable effect. (Good way to burn out pumps.)

Good luck!

-jim lee

1 Like

thx bro!,
i ll try that to mine,

best wishes.

I would change this part to:

  if (t1 - t2 >= delaytime)

The value of BP is always 100 so no need to print it. Moving the printing inside the timing means it will print every 30 seconds instead of every second so you don't need the delay().

What is bad about the behavior of the sketch?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.