Automatic Water Tank Filling - Arduino

by JingaL in Circuits > Arduino

536 Views, 0 Favorites, 0 Comments

Automatic Water Tank Filling - Arduino

main.png

 Automatic water tank filling from well. Uses 2 float switches to indicate when water tank it's full and one that check the if there is water in the well. 3 led to indicate; power state, pump state and tank full.

Float switches on fritzing are just switches...

Supplies

arduino pro mini.jpg
barell jack.jpg
relay.jpg
floar switch.jpg
3d-realistic-render-of-tricolor-3-mm-led-diode-rgb-red-green-blue-JR4Y7M.jpg
resistors.jpg

1 x Arduino Pro mini

1 x Barrel Jack

1 x 5v Relay

2 x Water level Sensor (switch)

3 leds

3 resistors (220 ohms)

Untitled Sketch 2_bb.png
#define pumpRelay 2       // Pump Relay pin
#define waterLevelWell 3  // Well water level switch pin
#define waterLevelFull 4  // Tank full level switch pin
#define pumpLed 7         // Board Status Led pin
#define tankFullLed 8     // Pump Status Led pin
#define powerLed 9        // Tank Full Status Led pin

enum relay_state { PUMP_OFF, PUMP_ON };                // Relay States
const unsigned long eventInterval = (2 * 60 * 1000UL); // Pump Timeout x minutes (x * 60 * 1000UL) = x minutes
unsigned long timeoutStart;                            // Timeout last time pump stoped
bool startPump = true;                                 // Pump status flag

void fillWaterTank(void);
bool readWellHasWater(void);
bool readTankIsFull(void);

void setup() {
  pinMode(powerLed, OUTPUT);
  digitalWrite(powerLed, HIGH);
  pinMode(pumpLed, OUTPUT);
  pinMode(tankFullLed, OUTPUT);
  pinMode(pumpRelay, OUTPUT);
  pinMode(waterLevelWell, INPUT_PULLUP);
  pinMode(waterLevelFull, INPUT_PULLUP);
}

void loop() {
  fillWaterTank();
  delay(2000);
}

/**
* @brief fill water tank
*
*/
void fillWaterTank(void) {

  bool tankFull = readTankIsFull();
  bool wellHasWater = readWellHasWater();
  
  if (tankFull) {
    startPump = false;                    // if tank is full, stop pump
    timeoutStart = millis();
  }

  digitalWrite(tankFullLed, tankFull);    // tank Full status
  digitalWrite(pumpLed, startPump);       // pumpLed status

  if (startPump) {

    // start pump if well has water
    if (wellHasWater) {
      digitalWrite(pumpRelay, PUMP_ON);   // start pump if well has water
    } else {
      digitalWrite(pumpRelay, PUMP_OFF);  // stop pump if well has no water
      startPump = false;                  // set startPump flag
      timeoutStart = millis(); // reset timeout
    }

  } else {

    // if tank is not full and timeout has passed, start pump
    if (!tankFull && (millis() - timeoutStart) > eventInterval) {
      
      if (wellHasWater) {
        startPump = true; // set startPump flag
        digitalWrite(pumpRelay, PUMP_ON); // start pump if well has water
      } else {
        timeoutStart = millis(); // reset timeout
      }

    } else {
      digitalWrite(pumpRelay, PUMP_OFF); // stop pump
    }
  }
}

/**
* @brief read water level in the well
*
* @return true
* @return false
*/
bool readWellHasWater(void) {
  return (!digitalRead(waterLevelWell)) ? true : false ;
}

/**
* @brief read tank full level
*
* @return true
* @return false
*/
bool readTankIsFull(void) {
  return (!digitalRead(waterLevelFull)) ? true : false ;
}