Skip to Content
This is the Beta version of our new Learning Paths approach. Please email feedback.

Water Level Sensor

A water level sensor and a rain sensor, while both related to water detection, serve different purposes. A rain sensor detects the presence or absence of rainfall, often used to trigger actions like shutting off sprinklers or turning on car windshield wipers. A water level sensor, on the other hand, measures the depth of water in a container or body of water, and can be used to turn off water supply to the container or turn on a pump to drain water depending on the situation.

Rain sensors use moisture-sensitive materials or optical sensors to detect the presence of water while water sensors use capacitance measurements to determine the water level.

Components

ComponentPurpose
Arduino NanoThe microcontroller
Water Level SensorDetects the water level and outputs an analog voltage

Circuit Diagram

Nano Water Level Sensor Fritzing

Connections

Water Level Sensor

Powering the sensor programmatically

We are going to do something different here with the power to the sensor. Normally we would connect the sensor power pin to the power supply pin of the board so that it will remain powered on as long as the board is powered on. This time we will connect the sensor power pin to one of the board GPIOs. The sensor can then be turned on or off in the sketch by sending a HIGH or a LOW signal to that pin.

Nano PinWater Level Sensor Module Pin
A0Signal
D13VCC (see note above)
GNDGND

Code

water_level_sensor.ino
/* Project: Physical Computing Circuit: Water Level Sensor Boards Supported: UNO R3/R4, Nano, ESP32 Function: This project read the value from a water level sensor. Author: STEMVentor Educonsulting This code is copyrighted. Please do not reuse or share for any purpose other than for learning with subscribed STEMVentor programs. This code is for educational purposes only and is not for production use. */ // LIBRARIES // PIN DEFINITIONS #define WATER_LEVEL_SENSOR_ANALOG_PIN A0 // We will be programmatically turning the sensor on and off with this pin. #define WATER_LEVEL_SENSOR_POWER_PIN 13 // GLOBAL CONSTANTS // GLOBAL VARIABLES // INITIALIZE OBJECTS // LOCAL FUNCTIONS // Read Rain sensor values. void readWaterLevelSensorValue() { digitalWrite(WATER_LEVEL_SENSOR_POWER_PIN, HIGH); // Turn the sensor ON delay(300); // Allow power to settle uint8_t water_level = analogRead(WATER_LEVEL_SENSOR_ANALOG_PIN); // Read the sensor output digitalWrite(WATER_LEVEL_SENSOR_POWER_PIN, LOW); // Turn the sensor OFF // Print the water level, which will be a number between 0 and 1023. Serial.print(water_level); // Then print the value as a percentage. Serial.print(map(water_level, 0, 1023, 0, 100)); Serial.println("% full"); } // THE setup FUNCTION RUNS ONCE WHEN YOU PRESS RESET OR POWER THE BOARD. void setup() { Serial.begin(9600); pinMode(WATER_LEVEL_SENSOR_POWER_PIN, OUTPUT); // Make sure the sensor starts turned off. digitalWrite(WATER_LEVEL_SENSOR_POWER_PIN, LOW); Serial.println("The Water Level sensor is ready!"); } // THE loop FUNCTION RUNS OVER AND OVER AGAIN FOREVER UNTIL THE BOARD IS POWERED OFF. void loop() { readWaterLevelSensorValue(); delay(10000*10); // In milliseconds }