Arduino Project for Water Level Indicator: Simple Guide & Code
An Arduino water level indicator uses a
water sensor connected to an analog input pin to detect water level and lights up LEDs to show the level. The Arduino reads sensor values and controls LEDs based on thresholds to indicate low, medium, or high water levels.Syntax
The basic syntax involves reading an analog sensor value using analogRead(pin), then using digitalWrite(pin, HIGH/LOW) to turn LEDs on or off based on the sensor reading.
Key parts:
analogRead(pin): Reads sensor voltage as a number from 0 to 1023.digitalWrite(pin, HIGH): Turns an LED on.digitalWrite(pin, LOW): Turns an LED off.pinMode(pin, INPUT/OUTPUT): Sets pin mode for sensor or LED.
arduino
void setup() { pinMode(A0, INPUT); // Water sensor input pinMode(2, OUTPUT); // LED for low level pinMode(3, OUTPUT); // LED for medium level pinMode(4, OUTPUT); // LED for high level } void loop() { int sensorValue = analogRead(A0); // Read water level if(sensorValue < 300) { digitalWrite(2, HIGH); // Low level LED on digitalWrite(3, LOW); digitalWrite(4, LOW); } else if(sensorValue < 700) { digitalWrite(2, LOW); digitalWrite(3, HIGH); // Medium level LED on digitalWrite(4, LOW); } else { digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, HIGH); // High level LED on } delay(500); // Wait half a second }
Example
This example reads a water level sensor connected to analog pin A0 and lights up three LEDs connected to pins 2, 3, and 4 to show low, medium, and high water levels respectively.
The sensor value ranges from 0 (dry) to 1023 (fully wet). LEDs turn on based on thresholds to indicate the water level visually.
arduino
void setup() { pinMode(A0, INPUT); pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); Serial.begin(9600); // For monitoring sensor values } void loop() { int sensorValue = analogRead(A0); Serial.print("Water Sensor Value: "); Serial.println(sensorValue); if(sensorValue < 300) { digitalWrite(2, HIGH); digitalWrite(3, LOW); digitalWrite(4, LOW); } else if(sensorValue < 700) { digitalWrite(2, LOW); digitalWrite(3, HIGH); digitalWrite(4, LOW); } else { digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, HIGH); } delay(500); }
Output
Water Sensor Value: 250
Water Sensor Value: 260
Water Sensor Value: 280
... (values update every 500ms)
Common Pitfalls
Common mistakes include:
- Not setting
pinModecorrectly for sensor or LEDs. - Using digital pins for analog sensor input instead of analog pins.
- Ignoring sensor calibration; sensor values vary by model and environment.
- Not adding delays, causing rapid LED flickering.
- Wiring LEDs without current-limiting resistors, which can damage components.
arduino
/* Wrong: Using digital pin for analog sensor */ int sensorValue = analogRead(2); // Pin 2 is digital, not analog /* Right: Use analog pin */ int sensorValue = analogRead(A0);
Quick Reference
| Function | Purpose | Example |
|---|---|---|
| pinMode(pin, mode) | Set pin as INPUT or OUTPUT | pinMode(2, OUTPUT); |
| analogRead(pin) | Read analog sensor value 0-1023 | int val = analogRead(A0); |
| digitalWrite(pin, HIGH/LOW) | Turn LED on or off | digitalWrite(3, HIGH); |
| delay(ms) | Pause program for ms milliseconds | delay(500); |
Key Takeaways
Use analog pins to read water sensor values with analogRead().
Control LEDs with digitalWrite() to indicate water levels visually.
Set pin modes correctly with pinMode() before using pins.
Calibrate sensor thresholds based on your specific sensor and water conditions.
Always use current-limiting resistors with LEDs to protect components.