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 About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $31. On Mokxi they're free.
Step by step, with the wiring and the common mistakes: Arduino Water Level Sensor: Wiring and Code
What you'll learn
- How a water level sensor reads
- Sorting an analog reading into ranges
- Thresholds
- Printing only when the state changes
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
The water level sensor is a row of bare copper traces. Water bridges them, and the deeper it goes, the more traces it bridges, so the signal pin S reads higher on A0. It is a rough gauge, not a ruler.
The sketch reads A0 and sorts the number into a word: below 100 is dry, below 400 is low, below 600 is half, and anything higher is full. It prints a line only when the word changes, so the monitor stays quiet while the level holds steady.
Parts list
3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Half breadboard
- 1 × Water level sensor
Kits that can build it
These 3 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Water level sensor pin GND | Arduino Uno R3 pin GND |
| Water level sensor pin VCC | Arduino Uno R3 pin 5V |
| Water level sensor pin S | Arduino Uno R3 pin A0 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Code walkthrough
The key functions of the sketch, and what each one does.
levelOf sketch.ino
Takes a reading and returns a word. It checks from the bottom up: under 100 is "dry", under 400 is "low", under 600 is "half", and anything else is "full". Change these numbers to tune the sensor for your setup.
const char *levelOf(int reading) {
if (reading < 100) return "dry";
if (reading < 400) return "low";
if (reading < 600) return "half";
return "full";
}setup sketch.ino
Starts serial at 9600 and prints a title. The sensor pin A0 needs no setup.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: water level sensor");
}loop sketch.ino
Reads SENSOR, gets the word from levelOf, and compares it to lastLevel. If it is different, it saves it and prints the reading and the word. It checks again every 200 ms.
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);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It says dry even in water This project
Check that S goes to A0, + to 5V and - to GND. Retune the numbers inlevelOfif needed. - The level flips between two words This project
A reading near a threshold can bounce. Move the thresholds inlevelOfaway from where you usually sit. - The reading keeps changing and the probe traces corrode. Water level sensor
Do not leave power on all the time. Power the sensor only when you read it, since DC current across wet traces causes corrosion. - The values are not the same depth to depth. Water level sensor
The output depends on the water, so it is not a precise gauge. Read it with analogRead and calibrate with your own liquid at known levels. Keep the electronics above the waterline. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.