Built-in project · Arduino Uno R3

Water level sensor on an Arduino Uno

A water level sensor on A0: the sketch sorts the reading into dry, low, half and full and prints each change. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

Water level sensor · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Water level: the sensor's traces on A0, and the depth in words.
//
//   sensor S -> A0    sensor + -> 5V    sensor - -> GND
//
// The sensor is a row of bare copper traces, every other one tied to power
// and the rest to a transistor's input. Water bridges the traces, and the
// deeper it goes, the more of them it bridges and the higher S reads. It is
// a rough gauge, not a ruler: it tells dry from a splash from half from full.
//
// On a real one, power it from a pin and switch it on only to read, because
// the traces corrode while current flows through water.

const int SENSOR = A0;

const char *levelOf(int reading) {
  if (reading < 100) return "dry";
  if (reading < 400) return "low";
  if (reading < 600) return "half";
  return "full";
}

const char *lastLevel = "";

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: water level sensor");
}

void loop() {
  int reading = analogRead(SENSOR);
  const char *level = levelOf(reading);
  if (level != lastLevel) {
    lastLevel = level;
    Serial.print("reading ");
    Serial.print(reading);
    Serial.print(": ");
    Serial.println(level);
  }
  delay(200);
}

Parts list

3 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Water level sensor pin GND
  • Arduino Uno R3 pin 5V; Water level sensor pin VCC
  • Arduino Uno R3 pin A0; Water level sensor pin S

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.