/*
Arduino | coding-help
Pls Help. Having Problems with I2C and water sensor. Might be with code.
RyanYTLive - Friday, May 8, 2026 10:39 PM
I have everything where it is supposed to be.
Sensor: S-GND, +-VIN, -_A0 , I2C: GND-GND, VCC-5V, SDA-A4, SCL-A5
*/
#include <LiquidCrystal_I2C.h>
const int SENSOR_PIN = A0;
const unsigned long ONE_SEC = 1000;
unsigned long prevTime = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2);
void showSplash() {
lcd.setCursor(2, 0);
lcd.print("Tank Sensor");
lcd.setCursor(6, 1);
lcd.print("V1.0");
delay(2000);
lcd.clear();
}
void updateDisplay(int value) {
char buffer[16];
char *tankLvl;
if (value == 0) {
tankLvl = "Empty ";
} else if (value < 33) {
tankLvl = "Low ";
} else if (value < 50) {
tankLvl = "Medium";
} else {
tankLvl = "High ";
}
lcd.setCursor(0, 0);
snprintf(buffer, 16, "Sensor: %3d%%", value);
lcd.print(buffer);
lcd.setCursor(0, 1);
snprintf(buffer, 16, "Level : %s", tankLvl);
lcd.print(buffer);
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
showSplash();
}
void loop() {
// read the sensor continuously
int rawValue = analogRead(SENSOR_PIN);
// update the display periodically
if (millis() - prevTime >= ONE_SEC) {
Serial.print("Raw value: ");
Serial.println(rawValue);
int mappedValue = map(rawValue, 0, 1023, 100, 0); // scales and inverts the pot
updateDisplay(mappedValue);
prevTime = millis();
}
}