Skip to content

Auto-Watering System

Automatic Garden Irrigation

Garden Watering System

Automated Garden Irrigation

  • 2 x LM 2596 Board - StepDown

    The LM2596 is a commonly used popular step-down switching regulator IC. The adjustable version can take in input voltage from 4.5V to 40V (Here 19.3V from an old Laptop Power Supply) and convert it to variable voltage sourcing upto of 3A of continues current.

  • 1 x Arduino Nano

    Open-source electronic prototyping platform enabling users to create interactive electronic objects.

  • 1 x Relay

    A relay simply is an electrically operated switch.

  • 1 x Rectifier Diode L4007

    Will absorbe Parasites generated by the movement of the solenoïd when it's turning On / Off.

  • 2 x Resistor 220 Ohm + LED

    To indicate the Mode selected

  • 1 x 12V DC Electrovalve

    Normally Closed type. Will let water run under 12V DC

0
Hours of conception
0
Hours of Coding
0
Years of Automated Watering
0
Breakdown

Rectifier Diode

The rectifier diode is mandatory. If missing, some parasites may disturb the functionment of the entire system each time the solenoïd turn on/off. The Rectifier Diode must be as close as possible from the solenoïd itself.

It’s possible to use only one LM2596 with 12V Output since both the Arduino and the Valve work with that  voltage and wouldn’t reach a consumption  of 3 Ampers.

// Creative Commons - www.ben-amar.com - 2021
// This Code was written for Ben-Amar.com under CC license: BY-NC-SA  (Attribution Mandatory, NonCommercial & ShareAlike)
// You must you credit "Ben-Amar.com" to remix, adapt, and build upon your work non-commercially. You must license your new creations under the identical terms.

//===========================================
//=========== VARIABLES DECLARATION =========
//===========================================

const byte ledhiv = 7;                // Indication LED "Winter"
const byte ledete = 8;                // Indication LED "Summer"
const byte interruptPin = 3;          // Pin for Push Button to switch Summer to Winter
const byte relay1 = 12;               // Relay to operat the Electric Transformer (230V)
volatile long jour = 86400000;        // Millis in 24h
unsigned long debut = 0;              // Variable to start counting a 24h or 12h sequence
unsigned long gHiv = 120000;           // Time to irrigate during winter (120s)
unsigned long gEte = 160000;           // Time to irrigate during winter (160s)
volatile int season = 0;              // Variable for season in ISR
int seasonapplied = season;           // Variable for season in void Loop (Is it necessary? Does Volatile In works in "Switch" and "If"?)
int trigger = 0;                      // Button trigger: variable used to detect a change of season and turn the adequat ledPin High
volatile long debounce = 0;           // To insure NO button bouncing
volatile long test = 0;
volatile long testint = 0;

//===========================================
//===================== SETUP ===============
//===========================================



void setup() {
  Serial.begin(9600);                                                               // Start Serial Monitor to follow step on Screen (Problem solving)
  Serial.println("--- Start Serial Monitor ---");                                   // Serial Monitor Information
  pinMode(ledhiv, OUTPUT);                                                          // Set ledhiv pin as Output
  pinMode(ledete, OUTPUT);                                                          // Set ledete pin as Output
  pinMode(relay1, OUTPUT);                                                          // Set relay1 pin as Output
  pinMode(interruptPin, INPUT_PULLUP);                                              // Set button pin as Interrupt
  Serial.println("...");                                                            // Serial Monitor Information
  Serial.println("--- PinMode Set");                                                // Serial Monitor Information
  attachInterrupt(digitalPinToInterrupt(interruptPin), timetogive, FALLING);        // Interrupt mode Definition (Pin, ISR loop to execute, Mode)
  Serial.println("--- Interrupt Set");                                              // Serial Monitor Information
  digitalWrite (ledhiv, HIGH);
  digitalWrite (relay1, LOW);                                                       // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW")
  Serial.println("--- Setup Watering initiated");                                   // Serial Monitor Information
  delay (120000);                                                                    // Delay 120 seconds to water as Initialisation mode
  digitalWrite (relay1, HIGH);                                                      // Set relay1 pin LOW => Watering OFF (the Relay trigger "LOW")
  digitalWrite (ledhiv, LOW);
  Serial.println("--- Setup Watering Finished: 30 seconds");                        // Serial Monitor Information
  season = 0;                                                                       // Set season variable as Winter (0) - if Value = 1: Summer
  Serial.print("Season : ");
  Serial.println(season);
}

//===========================================
//======================== LOOP =============
//===========================================


void loop() {
  seasonapplied = season;                                                           // copy the ISR variable value to the Variable used in the loop
  if (seasonapplied == 0) {                                                         // Main IF : Season checking
    if ((millis() - debut >= (jour - gHiv)) && (digitalRead(relay1) == HIGH)) {     // Compare "jour" Value - time to give in winter with the actual amount of millis (interval)
      digitalWrite (relay1, LOW);                                                   // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW")
      Serial.print(" Season = ");                                                   // Serial Monitor Information
      Serial.print(seasonapplied);                                                  // Serial Monitor Information
      Serial.print(" --- Hiver watering ON: ");                                     // Serial Monitor Information
      Serial.print(" millis = ");                                                   // Serial Monitor Information
      Serial.println(millis());                                                     // Serial Monitor Information
      digitalWrite (ledhiv, HIGH);                                                  // Turn On the LED indicating the WINTER mode is giving water
    }

    else if ((millis() - debut >= jour) && (digitalRead(relay1) == LOW)) {            // Compare "jour" Value with the actual amount of millis (jour is the laps of time during watering (here 23h:59m:30s)
      digitalWrite (relay1, HIGH);                                                    // Set relay1 pin HIGH => Watering OFF (the Relay trigger "LOW")
      debut = millis();                                                               // Update of the value debut to check the following interval starting from this instant!
      Serial.print(" Season = ");                                                     // Serial Monitor Information
      Serial.print(seasonapplied);                                                    // Serial Monitor Information
      Serial.print(" --- Hiver watering OFF: ");                                      // Serial Monitor Information
      Serial.print(" millis = ");                                                     // Serial Monitor Information
      Serial.println(millis());                                                       // Serial Monitor Information
      digitalWrite (ledhiv, LOW);                                                     // Turn OHH the LED indicating the WINTER mode
      season = 0;
    }
  }
  if (seasonapplied == 1) {                                                             // Main IF : Season checking
    if ((millis() - debut >= ((jour / 2) - gEte)) && (digitalRead(relay1) == HIGH)) {   // Compare "jour" Value - time to give in summer with the actual amount of millis (interval)
      digitalWrite (relay1, LOW);                                                       // Set relay1 pin LOW => Power transformer ON (the Relay trigger "LOW")
      Serial.print(" Season = ");                                                       // Serial Monitor Information
      Serial.print(seasonapplied);                                                      // Serial Monitor Information
      Serial.print(" --- Summer watering ON: ");                                        // Serial Monitor Information
      Serial.print(" millis = ");                                                       // Serial Monitor Information
      Serial.println(millis());                                                         // Serial Monitor Information
      digitalWrite (ledete, HIGH);                                                      // Turn On the LED indicating the SUMMER mode
    }
    else if ((millis() - debut >= (jour / 2)) && (digitalRead(relay1) == LOW)) {          // Compare "jour" Value with the actual amount of millis (jour is the laps of time during watering (here 11h:59m:00s)
      digitalWrite (relay1, HIGH);                                                        // Set relay1 pin HIGH => Watering OFF (the Relay trigger "LOW")
      debut = millis();                                                                   // Update of the value debut to check the following interval starting from this instant!
      Serial.print(" Season = ");                                                         // Serial Monitor Information
      Serial.print(seasonapplied);                                                        // Serial Monitor Information
      Serial.print(" --- Summer watering OFF: ");                                         // Serial Monitor Information
      Serial.print(" millis = ");                                                         // Serial Monitor Information
      Serial.println(millis());                                                           // Serial Monitor Information
      digitalWrite (ledete, LOW);                                                         // Turn OFF the LED indicating the SUMMER mode
      season = 1;
    }
  }
  if (trigger != season) {                                                              // This if detect the change of season with the varible volatile in the ISR. Then it light the appropriate LED.
    trigger = season;                                                                   // We reset trigger as Season to detect further change
    if (season == 0) {                                                                  // If variable season = 0 : winter light activated
      digitalWrite (relay1, HIGH);                                                      // Set relay1 pin LOW => Power transformer OFF (the Relay trigger "LOW") FOR SAFETY : Don't let the water Run by mistake.
      digitalWrite (ledhiv, HIGH);                                                      // Turn ON the LED indicating the WINTER mode. It will stay On until next watering.
      digitalWrite (ledete, LOW);                                                       // Turn OFF the LED indicating the SUMMER mode
      Serial.println("button has been pressed now Hiver mode");                         // Serial Monitor Information
    }
    else if (season == 1) {                                                             // If variable season = 1 : summer light activated
      digitalWrite (relay1, HIGH);                                                      // Set relay1 pin LOW => Power transformer OFF (the Relay trigger "LOW") FOR SAFETY : Don't let the water Run by mistake.
      digitalWrite (ledete, HIGH);                                                      // Turn ON the LED indicating the SUMMER mode. It will stay On until next watering.
      digitalWrite (ledhiv, LOW);                                                       // Turn OFF the LED indicating the WINTER mode
      Serial.println("button has been pressed now summer mode");                        // Serial Monitor Information
    }
  }
}

//===========================================
//========================= ISR =============
//===========================================

//=== ISR ===
void timetogive() {                 // Name the ISR, it will be executed if the pushbutton pressed
if (millis() - debounce > 500) {    //Comparison to avoid Bouncing Button!
if (season == 1) {                  // Change the Variable Season value IF == 1 turn it to 0
season = 0;
}
else if (season == 0) {             // Change the Variable Season value IF == 0 turn it to 1
season = 1;
}
debounce = millis ();               //New value for further comparison
}
}

// Creative Commons - www.ben-amar.com - 2021
// This Code was written for Ben-Amar.com under CC license: BY-NC-SA  (Attribution Mandatory, NonCommercial & ShareAlike)
// You must you credit "Ben-Amar.com" to remix, adapt, and build upon your work non-commercially. You must license your new creations under the identical terms.