Devices & Components
Arduino Uno Rev3
Alphanumeric LCD, 20 x 4
Water pump DC 3-5V
1N4007 – High Voltage, High Current Rated Diode
Water Level Sensor
Rotary potentiometer (generic)
General Purpose Transistor NPN
Ultrasonic Sensor - HC-SR04 (Generic)
Project description
Code
lcd_test.ino
c_cpp
1// Include the library code 2// 3 4#include <LiquidCrystal_I2C.h> // Downloaded library to manage the lcd with I2C 5#include <Wire.h> // This library allows Arduino to communicate with I2C devices 6LiquidCrystal_I2C lcd(0x27, 16, 2); //Create the object lcd with lcd_Addr 0x27 (0x3F) 7 8 9// Sensor pins 10 11#define sensorPower 7 12#define sensorPin A1 13#define trigPin 9 14#define echoPin 10 15#define pump_switch 3 16 17// Value for storing variables 18int waterval = 0; 19int ml = 0; 20long ultra_duration; 21int ultra_distance; 22 23void setup() { 24 Serial.begin(9600); 25 26 pinMode(sensorPower, OUTPUT); // Set D7 as an OUTPUT 27 pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output 28 pinMode(echoPin, INPUT); // Sets the echoPin as an Input 29 pinMode(pump_switch,OUTPUT); // Sets the pumpswitch as an Output 30 31 // Set to LOW so no power flows through the sensor 32 digitalWrite(sensorPower, LOW); 33 digitalWrite(pump_switch, LOW); //safety speed reset of the motor 34 35 startlcd(); // Print constant text 36} 37 38void loop() { 39 // put your main code here, to run repeatedly: 40 int sensorValue = analogRead(A0); //read the input on analog pin 0 41 42 //get the reading from the function below and print it 43 int level = readWaterSensor(); 44 int days = sensorValue/16.7; 45 int presence = readUltraSensor(); 46 47 Serial.print("Water level:"); 48 Serial.println(level); //print out the value you read 49 50 lcd.setCursor(6, 1); 51 lcd.print(days); // print days on lcd 52 lcd.setCursor(13, 3); 53 lcd.print(level); // print water level variable on lcd 54 55 if (presence <= 5) { 56 lcd.setCursor(6, 2); 57 lcd.print("PUMP ON "); // print water level 58 digitalWrite(pump_switch, HIGH); // Turn the switch ON 59 } 60 61 else { 62 lcd.setCursor(6, 2); 63 lcd.print("PUMP OFF"); // print water level 64 digitalWrite(pump_switch, LOW); // Turn the switch OFF 65 } 66 67 delay(200); //delay in between reads for stability 68 69} 70 71//This is a function used to get the reading 72int readWaterSensor() { 73 digitalWrite(sensorPower, HIGH); // Turn the sensor ON 74 delay(10); // wait 10 milliseconds 75 waterval = analogRead(sensorPin); // Read the analog value form sensor 76 waterval = waterval/2.2; 77 digitalWrite(sensorPower, LOW); // Turn the sensor OFF 78 return waterval; // send current reading 79} 80 81int readUltraSensor() { 82 // Clears the trigPin 83 digitalWrite(trigPin, LOW); 84 delayMicroseconds(2); 85 // Sets the trigPin on HIGH state for 10 micro seconds 86 digitalWrite(trigPin, HIGH); 87 delayMicroseconds(10); 88 digitalWrite(trigPin, LOW); 89 // Reads the echoPin, returns the sound wave travel time in microseconds 90 ultra_duration = pulseIn(echoPin, HIGH); 91 // Calculating the distance 92 ultra_distance= ultra_duration*0.034/2; 93 94 return ultra_distance; 95} 96 97void startlcd() { 98 lcd.init(); // Initiate LCD 99 lcd.backlight(); // Turn on backlight 100 lcd.begin(20, 4); // set up the LCD's number of columns and rows: 101 lcd.setCursor(4, 0); // set the cursor to column 4, line 2 102 lcd.print("INSERT TIME"); // Print a message to the LCD. 103 lcd.setCursor(8, 1); 104 lcd.print(" DAYS"); 105 lcd.setCursor(0, 3); 106 lcd.print("Water Level:"); 107 lcd.setCursor(17, 3); 108 lcd.print("ml"); // print water level 109} 110
Downloadable files
Circuit diagram
Circuit diagram

Comments
Only logged in users can leave comments