Program code for Automatic Water Overflow Controller

 This is the last part of the tutorial on how one can build an automatic water overflow controller with Arduino and soil moisture sensor.

The first part of this tutorial Water Tank Overflow Detector using Soil Moisture Sensor explains with circuit diagram how the control system works.

The second part of this tutorial Water Overflow Controller Proteus Simulation explains how the water controller system works with simulation in proteus electronics design software.

The third part of this tutorial Automatic Water Overflow Controller with Arduino shows the actual system build and demonstrates with video how it works.

The following picture shows the Arduino interfaced with water pump motor, push button, N7000 MOSFET transistor and diode.

circuit of water controller

The following is the program code for automatic water overflow controller.


//Water Tank Overflow Controller with Soil Moisture Sensor Demonstration
//Source: https://ee-diary.com

//pin names
const int MotorPin = 8;
const int SensorPin = 9;
const int BtnPin = 2;
//boolean variable to track push button state
boolean ON = false;
//variable to track pushbutton status
int BtnStatus = HIGH;
//variable to track sensor state
int SensorState = 0;


void setup () {
  pinMode(MotorPin, OUTPUT);
  pinMode(SensorPin, INPUT);
  pinMode(BtnPin, INPUT_PULLUP);
}

void loop() {
    checkPushButton();

  SensorState = digitalRead(SensorPin);

  if(SensorState == LOW && ON == true){
    digitalWrite(MotorPin, LOW);
   ON = false;
  }
  if(SensorState == LOW && ON==false){
    digitalWrite(MotorPin, LOW);
ON = false;
  }
  if(SensorState == HIGH && ON == true){
    digitalWrite(MotorPin, HIGH);
  }
  if(SensorState == HIGH && ON==false){
    digitalWrite(MotorPin, LOW);
  }

}


void checkPushButton(){
  //read push button state
  int BtnState = digitalRead(BtnPin);
  //check for button press
  if(BtnState == LOW && BtnState != BtnStatus){
  //swap on/off states
  ON =! ON;
  //update buttonStatus;
  BtnStatus = BtnState;
  //delay to cancel any debouncing faulty read
  delay(20);
  }

  //check for button release
  if(BtnState == HIGH && BtnState != BtnStatus){
  //updtate the pushbutton status to off
  BtnStatus = BtnState;
        //delay to cancel any debouncing faulty read
  delay(20);
  }
}

In the above program code, pin 8, pin 9 and pin 2 of Arduino is used for motor control, reading sensor and push button respectively. Some variable required to monitor the push button state are then declared. In the setup() function, we make the sensor pin as input, push button pin as input with internal pull-up resistor and make the motor pin as output. In the loop() function we call the checkPushButton() function to monitor the state of the push button. We also read in the value of the sensor digital output DO. Then depending upon the state of the push button and the signal from the sensor we either turn off or turn on the dc motor water pump.

Post a Comment

Previous Post Next Post