Water Level Monitoring System

Here’s how you can build a water level monitoring system using an Arduino and a water level sensor, with the water percentage displayed on an I2C LCD.

Components Required:

  1. Arduino (Uno, Nano, etc.)
  2. Water Level Sensor
  3. I2C LCD Display (e.g., 16×2 LCD with I2C interface)
  4. Jumper Wires
  5. Breadboard (optional)

1. Wiring the Components:

Water Level Sensor:

  • VCC pin: Connect to 5V on the Arduino (or 3.3V if using a 3.3V Arduino).
  • GND pin: Connect to GND on the Arduino.
  • A0 (Signal) pin: Connect to an Analog input pin on the Arduino (e.g., A0). This is the pin that will output the water level sensor value.

I2C LCD:

  • Connect the VCC and GND pins of the LCD to the 5V and GND of the Arduino.
  • Connect the SDA and SCL pins of the LCD to A4 (SDA) and A5 (SCL) pins of the Arduino (for Uno; for other boards, the pins might vary).

2. Arduino Code:

Here’s a simple example code using an analog water level sensor and displaying the water level percentage on the I2C LCD.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Initialize the LCD (address 0x27 is common for I2C LCDs)
// If this doesn't work, try adjusting the 16, 2 to the size of your LCD (e.g., 20x4).
LiquidCrystal_I2C lcd(0x27, 16, 2);

// Pin connected to the water level sensor (analog sensor)
int waterLevelPin = A0;

// Variables for storing the sensor readings and water level percentage
int sensorValue = 0;
float waterLevelPercent = 0;

void setup() {
  // Initialize the LCD, specify size here if needed (e.g., 16, 2 for a 16x2 display)
  lcd.begin(16, 2);  // This is the method that could be causing the error
  lcd.backlight();   // Turn on the backlight
  
  // Print a message on the first row
  lcd.setCursor(0, 0);
  lcd.print("Water Level: ");
  delay(1000); // Delay for initial LCD setup
  
  // Start the serial monitor for debugging
  Serial.begin(9600);
}

void loop() {
  // Read the sensor value (0-1023 for analog input)
  sensorValue = analogRead(waterLevelPin);

  // Map the sensor value to a percentage (you may need to adjust the range based on your sensor)
  waterLevelPercent = map(sensorValue, 0, 1023, 0, 100);

  // Display the water level percentage on the LCD
  lcd.setCursor(0, 1);  // Move cursor to the second row
  lcd.print("Level: ");
  lcd.print(waterLevelPercent);
  lcd.print("%   "); // Extra spaces to clear any leftover characters

  // Print the water level on the serial monitor (optional)
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  Serial.print(" | Water Level: ");
  Serial.println(waterLevelPercent);

  delay(1000);  // Update every 1 second
}

3. Explanation:

  • Water Level Sensor: If using an analog sensor, the analogRead() function gets a value from 0 to 1023, which represents the water level. The map() function then converts this value to a percentage (0-100%).
  • I2C LCD: We initialize the LCD, and then in the loop, we update the water level percentage on the display.
  • Serial Monitor: Optionally, you can view the sensor value and water level percentage on the Serial Monitor for debugging.

4. Adjusting the Range (if needed):

  • Depending on the sensor you’re using, you might need to calibrate the sensor’s readings. You can check the sensor’s output when the water level is at minimum (dry) and maximum (full), and adjust the map() function accordingly.

5. Uploading the Code:

  • Connect your Arduino to your computer using a USB cable.
  • Select the appropriate board and port in the Arduino IDE.
  • Click the upload button to load the code onto your Arduino.

6. Testing:

  • Power on the system and test by dipping the water level sensor into water. You should see the water level percentage displayed on the I2C LCD and also printed on the Serial Monitor.