制造商零件编号 A000066
ARDUINO UNO R3 ATMEGA328P BOARD
Arduino
License: General Public License Arduino
The water level sensor operates based on the change in resistance as water touches different parts of its conductive path. The sensor consists of 10 exposed traces out of which 5 are power traces and 5 are sensor traces.
When the water reaches different heights on the sensor, the water bridges the traces, altering the sensor's resistance. The sensor outputs an analog signal that corresponds to the water level, which is then fed into the Arduino for processing. This analog value changes with the level of immersion of the sensor in the water, allowing us to detect the height of the water.
Arduino Nano/Uno
Water level sensor module
10kΩ Resistor (optional for signal stabilization)
Buzzer (for audio alert)
LED (for visual indicator)
Jumper wires
Breadboard
USB cable for Arduino
Power supply
The water level sensor module typically has three pins:
VCC Power input (3.3V–5V, connect to Arduino 5V).
GND Ground (connect to Arduino GND).
SIG Signal output pin (connect to Arduino analog input, usually A0).
This simple setup allows the Arduino to read the water level from the sensor and trigger the RGB LED and buzzer when the water level crosses the threshold.
The Arduino controls the RGB LED to display different colors for different water levels. When the water level is below 10%, the RGB LED will glow red. If the water level is between 10% and 99%, the LED will emit a blue light. Once the water level reaches 99% or more, the LED turns green, and the buzzer activates, signaling that the container is full and should no longer be filled.
Here is the wiring diagram:
First, we connect the VCC pin of the water level sensor to the 5V pin of the Arduino. Then we connect the GND pin of the sensor to the GND pin on the Arduino. Then connect the SIG pin of the sensor to the Arduino's A0 pin (Analog input). Now we connect a common cathode LED connected with the Arduino at pins 10, 9 and 8 for colors red, blue and green respectively. A buzzer is connected at pin 2.
Arduino Code
#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);
}
Once the circuit is set up and the Arduino code is uploaded, the project works by continuously monitoring the analog signal from the water level sensor. The sensor detects the current water level and sends an analog signal to the Arduino, which reads it through pin A0. The Arduino then compares the sensor reading to a predefined threshold. If the water level exceeds this threshold, the Arduino triggers both the buzzer and the LED to notify that the water has reached a critical level.