/*
Wokwi | general
PATTY - Tuesday, May 26, 2026 6:51 AM
Project Link: https://wokwi.com/projects/465078381043980289?gh=1
Error message: sketch.ino: In function 'void setup()':
sketch.ino:20:18: error: no matching function for call to 'LiquidCrystal_I2C::begin()'
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int TRIG_PIN = 19;
const int ECHO_PIN = 18;
const int RELAY_PIN = 17;
const int TANK_FULL = 15; // tank full height in cm
LiquidCrystal_I2C lcd(0x27, 16, 2);
int getDistance() {
// send trigger pulse
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// read echo
long duration = pulseIn(ECHO_PIN, HIGH);
// calculate distance
long distance = duration * 0.0343 / 2;
// return distance as an integer
return (int) distance + 0.5;
}
void showSplash() {
lcd.setCursor(4, 0);
lcd.print("Rainwater");
lcd.setCursor(2, 1);
lcd.print("Control V1.0");
delay(2000);
lcd.clear();
Serial.println("\nReady!\n");
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
// initialize
digitalWrite(RELAY_PIN, LOW);
showSplash();
}
void loop() {
char buffer[16];
char pumpStatus;
// get distance
int distance = getDistance();
// Limit readings
if (distance > TANK_FULL) {
distance = TANK_FULL;
}
if (distance <= 1) {
distance = 0;
}
// Convert to percentage
int percentage = map(distance, 0, TANK_FULL, 0, 100);
// Pump Control
if (percentage >= 100) {
// Turn OFF pump
digitalWrite(RELAY_PIN, LOW);
pumpStatus = 'F';
} else {
// Turn ON pump
digitalWrite(RELAY_PIN, HIGH);
pumpStatus = 'P';
}
// Display on LCD
snprintf(buffer, 16, "Level: %3d%% %c", percentage, pumpStatus);
lcd.setCursor(0, 0);
lcd.print(buffer);
snprintf(buffer, 16, "Dist : %3d cm", distance);
lcd.setCursor(0, 1);
lcd.print(buffer);
delay(1000);
}Pump