Show a water level with three LEDs

Dip the water level sensor into a glass and light a green, yellow or red LED to show whether it is full, half full or nearly empty.
  • Level:Beginner
  • Duration: 35 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Show a water level with three LEDs – EEMENTOR STEM™ lesson

Coffee machines, fish tanks and washing machines all need to know how much water they hold. In this workshop project, the water level sensor measures how far it is dipped into a glass, and three LEDs show the level like a traffic light: green for full, yellow for half and red for nearly empty.

What you'll learn

  • How the water level sensor works
  • How to calibrate a sensor with two reference points
  • How to show a value with several LEDs using if and else if
  • Why powering a sensor only while measuring makes it last longer

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Water level sensor (45-in-1 Sensor Kit)
  • Green, yellow and red LEDs and 3 resistors, 220 Ω (UNO R3 Starter Kit)
  • A glass of water
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Show a water level with three LEDs
Breadboard view. Check each connection against the wiring table below.
From To
Water level sensor S UNO pin A0
Water level sensor + UNO pin 7 (powers the sensor only while measuring)
Water level sensor − GND
UNO pin 2 220 Ω resistor, then the red LED's long leg
UNO pin 3 220 Ω resistor, then the yellow LED's long leg
UNO pin 4 220 Ω resistor, then the green LED's long leg
All LED short legs GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Only the part of the sensor with the metal lines may go into the water. Keep the connector and the wires dry.

The sketch

const int SENSOR_PIN = A0;
const int SENSOR_POWER = 7;
const int RED_LED = 2;
const int YELLOW_LED = 3;
const int GREEN_LED = 4;

// Calibrate these two readings for your sensor (see below)
const int EMPTY_READING = 0;     // sensor dry
const int FULL_READING = 600;    // sensor in water up to the top of the lines

int readLevel() {
  digitalWrite(SENSOR_POWER, HIGH);
  delay(20);
  int reading = analogRead(SENSOR_PIN);
  digitalWrite(SENSOR_POWER, LOW);
  Serial.print("Reading: ");
  Serial.print(reading);
  int percent = constrain(map(reading, EMPTY_READING, FULL_READING, 0, 100), 0, 100);
  Serial.print("  level: ");
  Serial.print(percent);
  Serial.println(" %");
  return percent;
}

void showLevel(bool red, bool yellow, bool green) {
  digitalWrite(RED_LED, red);
  digitalWrite(YELLOW_LED, yellow);
  digitalWrite(GREEN_LED, green);
}

void setup() {
  Serial.begin(9600);
  pinMode(SENSOR_POWER, OUTPUT);
  pinMode(RED_LED, OUTPUT);
  pinMode(YELLOW_LED, OUTPUT);
  pinMode(GREEN_LED, OUTPUT);
}

void loop() {
  int level = readLevel();
  if (level >= 70) showLevel(false, false, true);        // full
  else if (level >= 30) showLevel(false, true, false);   // half
  else showLevel(true, false, false);                    // nearly empty
  delay(500);
}

Calibrate the sensor

  1. Upload the sketch and open the Serial Monitor at 9600 baud.
  2. With the sensor dry, note the reading and put it in EMPTY_READING (it is usually 0 or close to it).
  3. Dip the sensor into the water up to the top of the metal lines and put that reading in FULL_READING.
  4. Upload again and slowly lift the sensor out of the water: the LEDs change from green to yellow to red.

How it works

The sensor has two sets of metal lines, side by side. Water conducts a little electricity, so the deeper the sensor goes in, the more of the lines are bridged and the higher the voltage on S. With the two calibration readings, map() turns that into a percentage.

The if / else if / else chain checks the levels from the top down and stops at the first match, so exactly one LED is on. Electricity flowing through the wet lines slowly corrodes them, so the sensor is powered from pin 7 and switched on only for a moment while the UNO measures.

Try this next

  • Make the red LED blink when the level is below 10 %.
  • Add the active buzzer as an "almost empty" alarm.
  • Use five LEDs for a more detailed bar.

Troubleshooting

  • The level is always 0 %: check the + wire on pin 7 and S on A0.
  • The level jumps around: hold the sensor still and straight; waves change the reading.
  • The reading slowly drifts over days: the lines are corroding. Rinse and dry the sensor after use.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit