Nur Syafiiqah Rizal
Published

Water Tank Filling and Level Control System

Automated water tank system using ATmega328P, water level sensor, LCD, push buttons, and L298N-controlled DC pump

BeginnerFull instructions provided5 hours58
Water Tank Filling and Level Control System

Things used in this project

Story

Read more

Schematics

Water Tank Filling and Level Control System

Code

Untitled file

C/C++
#include <Arduino.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <LiquidCrystal.h>

// ================= LCD =================
LiquidCrystal lcd(8, 10, A1, A2, A3, A4);

// ================= Pin =================
#define IN1 PD6
#define IN2 PD7

volatile int targetLevel = 1000;
int currentLevel = 0;

unsigned long lastInterruptTime = 0;

const int HYSTERESIS = 1;

// ======================================
// ADC
// ======================================
void setupADC()
{
    ADMUX = (1 << REFS0);

    ADCSRA =
        (1 << ADEN) |
        (1 << ADPS2) |
        (1 << ADPS1) |
        (1 << ADPS0);
}

uint16_t readADC()
{
    ADCSRA |= (1 << ADSC);

    while (ADCSRA & (1 << ADSC));

    return ADC;
}

int readWaterLevel()
{
    long total = 0;

    for (int i = 0; i < 50; i++)
    {
        total += readADC();
    }

    return total / 50;
}

// ======================================
// PWM TIMER1
// ENA = D9 = PB1 = OC1A
// ======================================
void setupPWM()
{
    DDRB |= (1 << DDB1);

    TCCR1A =
        (1 << COM1A1) |
        (1 << WGM10);

    TCCR1B =
        (1 << WGM12) |
        (1 << CS11);

    OCR1A = 0;
}

// ======================================
// MOTOR
// ======================================
void pumpON()
{
    PORTD |= (1 << PORTD6);
    PORTD &= ~(1 << PORTD7);

    OCR1A = 255;
}

void pumpOFF()
{
    PORTD &= ~(1 << PORTD6);
    PORTD &= ~(1 << PORTD7);

    OCR1A = 0;
}

// ======================================
// INTERRUPT
// D2 = INT0
// D3 = INT1
// ======================================
ISR(INT0_vect)
{
    if (millis() - lastInterruptTime > 50)
    {
        targetLevel += 10;

        if (targetLevel > 1023)
            targetLevel = 1023;

        lastInterruptTime = millis();
    }
}

ISR(INT1_vect)
{
    if (millis() - lastInterruptTime > 50)
    {
        targetLevel -= 10;

        if (targetLevel < 0)
            targetLevel = 0;

        lastInterruptTime = millis();
    }
}

void setupInterrupt()
{
    EICRA |= (1 << ISC01);
    EICRA |= (1 << ISC11);

    EIMSK |= (1 << INT0);
    EIMSK |= (1 << INT1);

    sei();
}

// ======================================
// SETUP
// ======================================
void setup()
{
    // Motor D6 D7 output
    DDRD |= (1 << DDD6);
    DDRD |= (1 << DDD7);

    // Button D2 D3 input pullup
    DDRD &= ~(1 << DDD2);
    DDRD &= ~(1 << DDD3);

    PORTD |= (1 << PORTD2);
    PORTD |= (1 << PORTD3);

    setupADC();
    setupPWM();
    setupInterrupt();

    lcd.begin(16, 2);

    lcd.setCursor(0, 0);
    lcd.print("Water Control");

    lcd.setCursor(0, 1);
    lcd.print("by Team 4");

    delay(50);

    lcd.clear();

    pumpOFF();
}

// ======================================
// LOOP
// ======================================
void loop()
{
    currentLevel = readWaterLevel();

    int targetCopy = targetLevel;

    int persen = 0;

    if (targetCopy > 0)
    {
        persen = (currentLevel * 100L) / targetCopy;
    }

    if (persen > 100)
    {
        persen = 100;
    }

    bool pumpStatus = false;

    if (currentLevel < targetCopy - HYSTERESIS)
    {
        pumpON();
        pumpStatus = true;
    }
    else if (currentLevel > targetCopy + HYSTERESIS)
    {
        pumpOFF();
        pumpStatus = false;
    }

    lcd.setCursor(0, 0);
    lcd.print("L:");
    lcd.print(currentLevel);
    lcd.print("(");
    lcd.print(persen);
    lcd.print("%)");

    lcd.print("   ");

    lcd.setCursor(0, 1);
    lcd.print("T:");
    lcd.print(targetCopy);

    lcd.print("|");

    if (pumpStatus)
        lcd.print("ON ");
    else
        lcd.print("OFF");

    lcd.print("   ");

    delay(5);
}

Credits

Nur Syafiiqah Rizal
1 project • 0 followers

Comments