Martin
Published © CC BY-NC-SA

Cheap Controller for 8 Irrigation Valves

Low-cost, battery-powered irrigation controller for up to 8 valves, expandable with Wi-Fi/Bluetooth, designed for energy efficiency.

IntermediateShowcase (no instructions)3 hours192
Cheap Controller for 8 Irrigation Valves

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Breadboard (generic)
Breadboard (generic)
×1
Rezistor 8.2 khom
×2
DC Motor, 12 V
DC Motor, 12 V
×1
L293D motor driver shield
×1
Optical endstop
×1
Mechanical endstop button
×1
Servo Motor 25kg
×1
Wires
×1
GT2 belt 6mm (920mm)
×1
M3 Screws
×1
Bradas Water Valve - 4 chanels
×2
Threaded rod (500mm)
×5
Aluminium Bar 10x10 mm (500mm)
×2

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Custom parts and enclosures

3D models for print

Code

Arduino Code

Arduino
Arduino Code to control the automatic irigation system
#include <Servo.h>

// Motor control pins
const int AIA = 3;
const int AIB = 4;
const int BIA = 5;
const int BIB = 6;
const int controlPin = 7;    // Direction control pin
const int blinkLed = 13;      // LED pin for error indication
byte speed = 255;

const int endStopPin = 12;    // Endstop pin

int direction = 1;
int valveCount = 0;

Servo myServo;               // Servo motor instance
const int servoPin = 9;       // Servo motor pin

// Move motor forward
void forward() {
  analogWrite(AIA, 0);
  analogWrite(AIB, speed);
  analogWrite(BIA, 0);
  analogWrite(BIB, speed);
}

// Move motor backward
void backward() {
  analogWrite(AIA, speed);
  analogWrite(AIB, 0);
  analogWrite(BIA, speed);
  analogWrite(BIB, 0);
}

// Stop the motor
void stopMotor() {
  digitalWrite(AIA, LOW);
  digitalWrite(AIB, LOW);
  digitalWrite(BIA, LOW);
  digitalWrite(BIB, LOW);
}

// Blink LED indefinitely to indicate error
void startBlinkLed() {
  while (true) {
    digitalWrite(blinkLed, HIGH);
    delay(50);
    digitalWrite(blinkLed, LOW);
    delay(300);
  }
}

// Open the valve for a specified time in seconds
bool openValve(int timeInSeconds) {
  myServo.write(0);
  delay(3000);

  Serial.println("Irrigation started.");

  myServo.write(5);
  delay(500);
  delay(timeInSeconds * 1000);

  myServo.write(175);
  delay(3000);
  myServo.write(167);
  delay(500);

  if (digitalRead(controlPin) == HIGH) {
    Serial.println("Attempting to fully close the valve...");
    myServo.write(180);
    delay(1000);
    myServo.write(167);
    delay(500);
  }

  Serial.println("Irrigation finished.");
  return true;
}

// Move to a specific valve
bool moveToValve(int valveNumber) {
  if (valveCount == valveNumber) return true;

  if (valveCount < valveNumber) {
    forward();
    delay(200);
    while (valveCount != valveNumber) {
      if (digitalRead(controlPin) == LOW) {
        valveCount++;
        if (valveCount == valveNumber) break;
        while (digitalRead(controlPin) == LOW) {
          delay(5);
        }
      }
      delay(10);
    }
  } else {
    backward();
    delay(200);
    while (valveCount != valveNumber) {
      if (digitalRead(controlPin) == LOW) {
        valveCount--;
        if (valveCount == valveNumber) break;
        while (digitalRead(controlPin) == LOW) {
          delay(5);
        }
      }
      delay(10);
    }
  }

  stopMotor();
  delay(100);
  return true;
}

void setup() {
  pinMode(AIA, OUTPUT);
  pinMode(AIB, OUTPUT);
  pinMode(BIA, OUTPUT);
  pinMode(BIB, OUTPUT);
  pinMode(controlPin, INPUT);
  pinMode(blinkLed, OUTPUT);
  pinMode(endStopPin, INPUT);
  myServo.attach(servoPin);

  myServo.write(170);
  delay(3000);

  Serial.begin(9600);

  backward();
  while (digitalRead(endStopPin) == 0) {
    delay(10);
  }
  stopMotor();
  delay(100);
  forward();
  delay(40);
  stopMotor();
  delay(100);

  valveCount = 0;
  Serial.println("Starting...");
}

void loop() {
  delay(10);

  if (Serial.available()) {
    delay(10);

    char valveCharArray[2];
    char minutesCharArray[3];

    valveCharArray[0] = Serial.read();
    valveCharArray[1] = '\0';

    minutesCharArray[0] = Serial.read();
    minutesCharArray[1] = Serial.read();
    minutesCharArray[2] = '\0';

    Serial.read(); // To clear extra input
    Serial.read();

    int valveInt = atoi(valveCharArray);
    int minutesInt = atoi(minutesCharArray);

    Serial.print("Opening valve ");
    Serial.print(valveInt);
    Serial.print(" for ");
    Serial.print(minutesInt);
    Serial.println(" minutes.");

    moveToValve(valveInt);
    openValve(minutesInt);

    delay(1000);
    Serial.println(valveCount);
  }
}

Credits

Martin
6 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.