Arduion Based Water Level Control System
This project will help you to maintain the water level in water tank. We used sonar to detect the water level. Connect Arduino as per below diagram
Below is a sample code for this project
#include <LCD.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal.h>
LiquidCrystal_SR lcd(12, 13, 8);
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int left=2;
const int right=3;
const int fwd=4;
const int back=5;
const int motorpin=6;
// defines variables
long duration;
int distance;
int upperlevel=50,lowerlevel=150;
bool motorstat=false;
bool leftpin=true,rightpin=true,backpin=true,fwdpin=true;
void setup() {
// put your setup code here, to run once:
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:
lcd.clear(); // clear the screen
pinMode(left, INPUT);
pinMode(right, INPUT);
pinMode(fwd, INPUT);
pinMode(back, INPUT);
pinMode(motorpin, OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
digitalWrite(motorpin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0, 0);
lcd.print(“U:”); // print Upper level
lcd.print(upperlevel);
lcd.print(“::L:”); // print Lower Level
lcd.print(lowerlevel);
lcd.print(“:::”);
leftpin = digitalRead(left);
rightpin = digitalRead(right);
fwdpin = digitalRead(fwd);
backpin = digitalRead(back);
if(leftpin==false){
Serial.println(“left press”);
lowerlevel=lowerlevel-1;
if(lowerlevel<0)
lowerlevel=0;
delay(2000);
}
if(rightpin==false){
Serial.println(“right press”);
lowerlevel=lowerlevel+1;
delay(500);
}
if(fwdpin==false){
Serial.println(“fwd press”);
upperlevel=upperlevel+1;
delay(500);
}
if(backpin==false){
Serial.println(“back press”);
upperlevel=upperlevel-1;
if(upperlevel<0)
upperlevel=0;
delay(500);
}
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance= duration*0.034/2;
Serial.print(“Cur: “);
Serial.println(distance);
lcd.setCursor(0, 1);
lcd.print(“Cur:”); //Print current water level
lcd.print(distance);lcd.print(“:Motor:”);
if(motorstat==true)
lcd.print(“ON:”);
else
lcd.print(“OFF:”);
if(distance>lowerlevel && motorstat==false){
motorstat=true;
digitalWrite(motorpin, HIGH);
}
if(distance<upperlevel && motorstat==true){
motorstat=false;
digitalWrite(motorpin, LOW);
}
}
Below project will help you to understand the level calculation using sonar