Tank level gauge (water level sensor)
Show how full a tank is on an LED bar graph and warn when it is nearly empty or about to overflow.
Sensors · Beginner
In a real product: Water tank monitor (Agriculture): Shows how full the tank is and warns before it runs dry.
The water level sensor's traces conduct more the deeper they sit in water, so its output voltage rises with the level. Ten LEDs make a gauge anyone can read at a glance.
What you'll learn
- Scaling an analog reading to a number of LEDs
- Averaging readings to steady a noisy sensor
- Warning thresholds at both ends of a range
How it works
The sensor's S output rises from 0 V (dry) to about two thirds of the supply when fully covered. The sketch averages eight readings, scales the result between "empty" and "full" calibration values, and lights that many of the bar graph's ten segments. Below 10 % it prints a refill warning; above 90 %, an overflow warning.
Try this
- Click the sensor while it runs and raise the water level.
- Make the top segment blink when the tank is about to overflow.
- Calibrate: set the level to 0 % and 100 % and print the raw readings to find EMPTY and FULL.
sketch.ino
// Water level sensor (S on A0) shown on a 10-segment LED bar graph on pins 2-11.
const int SENSOR = A0;
const int EMPTY = 0, FULL = 696; // raw readings dry and fully covered
int readLevel() {
long sum = 0;
for (int i = 0; i < 8; i++) sum += analogRead(SENSOR);
return constrain(map(sum / 8, EMPTY, FULL, 0, 100), 0, 100);
}
void setup() {
Serial.begin(9600);
for (int pin = 2; pin <= 11; pin++) pinMode(pin, OUTPUT);
}
void loop() {
int level = readLevel();
int bars = (level + 5) / 10;
for (int i = 0; i < 10; i++) digitalWrite(2 + i, i < bars);
Serial.print("Level ");
Serial.print(level);
Serial.print(" %");
if (level < 10) Serial.print(" refill!");
if (level > 90) Serial.print(" overflow!");
Serial.println();
delay(500);
}