Rachana Jain
Published © Apache-2.0

Water Level Sensor Interfacing with Arduino

Explore how to interface water level sensor with an Arduino and create a water level indicator that activates an LED and buzzer.

BeginnerFull instructions provided2 hours365
Water Level Sensor Interfacing with Arduino

Things used in this project

Hardware components

Arduino UNO R3
×1
Water Level Sensor
×1
I2C 16x2 Arduino LCD Display Module
DFRobot I2C 16x2 Arduino LCD Display Module
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Solderless Breadboard Full Size
Solderless Breadboard Full Size
×1
Buzzer
Buzzer
×1
Through Hole Resistor, 220 kohm
Through Hole Resistor, 220 kohm
×1
RGB LED
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Water Level Indicator

Wiring diagram

Code

Arduino Code for Water Level Indicator

C/C++
Upload the code to Arduino
/* 
Water Level Indicator– Arduino Project
by www.playwithcircuit.com
*/
#include <LiquidCrystal_I2C.h>  // Library to Run I2C LCD
// define the size of filter array
#define FILTER_SIZE 20
#define LOWER_THRESHOLD 4
#define UPPER_THRESHOLD 650
#define RED_PIN 10
#define BLUE_PIN 9
#define GREEN_PIN 8
#define BUZZER_PIN 2
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the Analog pin for the soil moisture sensor
const int WaterSensorPin = A0;
// Analog Value filter
int Filter(int sensorValue);
void setup() {
  // initialize the lcd
  lcd.init();
  // Turn on the Backlight
  lcd.backlight();
  // Clear the display buffer
  lcd.clear();
  // Make LED pins and Buzzer pin as output
  pinMode(RED_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  // Turn Off all the pins
  digitalWrite(RED_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BUZZER_PIN, LOW);
  // Print a message to the LCD
  lcd.setCursor(0, 0);
  lcd.print("Fill Percent:");
}
void loop() {
  // Variable to store sensor values
  int sensorValue;
  // Variable to store filtered Value
  int filteredValue;
  // Variable to store fill percentage
  int fillPercent;
  // Read the value from the soil moisture sensor
  sensorValue = analogRead(WaterSensorPin);
  filteredValue = Filter(sensorValue);
  fillPercent = map(filteredValue,LOWER_THRESHOLD, UPPER_THRESHOLD, 0, 100);
  // Display the filtered Analog Value on the LCD
  lcd.setCursor(0, 1);
  lcd.print(fillPercent);
  // Clear Previous Data
  lcd.print("%  ");
  // Change the color of LED as per water level
  if (fillPercent >= 99) {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(BLUE_PIN, LOW);
    digitalWrite(GREEN_PIN, HIGH);
    digitalWrite(BUZZER_PIN, HIGH);
  } else if (fillPercent >= 10 && fillPercent < 99) {
    digitalWrite(RED_PIN, LOW);
    digitalWrite(BLUE_PIN, HIGH);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BUZZER_PIN, LOW);
  } else if (fillPercent < 10) {
    digitalWrite(RED_PIN, HIGH);
    digitalWrite(BLUE_PIN, LOW);
    digitalWrite(GREEN_PIN, LOW);
    digitalWrite(BUZZER_PIN, LOW);
  }
  // Wait for 50ms before the next loop
  delay(50);
}
// Averaging filter to filter Analog Values
int Filter(int sensorValue) {
  static int analogArray[FILTER_SIZE] = { 0 };
  int filteredValue = 0;
  int i;
  // Shift the Element removing the oldest value stored at index 0
  for (i = 0; i < (FILTER_SIZE - 1); i++) {
    analogArray[i] = analogArray[i + 1];
  }
  // Put the current value in the last element of Array i.e. at index FILTER_SIZE-1
  analogArray[FILTER_SIZE-1] = sensorValue;
  for (i = 0; i < FILTER_SIZE; i++) {
    filteredValue += analogArray[i];
  }
  // Return Filtered Analog Value
  return (filteredValue / FILTER_SIZE);
}

Credits

Rachana Jain
15 projects • 8 followers
I'm an avid Arduino and electronics enthusiast with a passion for tinkering, experimenting, and bringing innovative ideas to life.

Comments