Arduino Nano - Water Sensor

This tutorial instructs you how to use the water sensor with Arduino Nano. We will learn:

This can be applied to applications that detect water leakage, rainfall, tank overflow...

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Water level sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino Nano
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Water Level Sensor

Water Level Sensor Pinout

The water level sensor has 3 pins:

  • The S (Signal) pin is an analog output that will be connected to one of the analog inputs on your Arduino Nano.
  • The + (VCC) pin supplies power for the sensor and it is suggested to use a voltage between 3.3V – 5V.
  • The - (GND) pin is a ground connection.
water sensor pinout

How Water Level Sensor Works

In short, The greater the amount of water the sensor is submerged in, the higher the voltage of the signal pin will be.

Let's take a closer look.

This section includes advanced information that may be overwhelming. If you are unsure about the content, feel free to skip it and move on to the next sections.

The sensor has a set of ten exposed copper tracks:

  • Five of them are power tracks
  • The other five are sense tracks.

The tracks are arranged in parallel with one sense track between every two power tracks. Unless they are bridged by water when submerged, the tracks are not connected.

The traces act as a variable resistor, similar to a potentiometer, whose resistance varies with the water level:

  • The resistance is determined by the distance from the top of the sensor to the surface of the water.
  • The resistance is inversely proportional to the amount of water present:
  • When more water is present, the conductivity is improved and the resistance is lowered.
  • When less water is present, the conductivity is reduced and the resistance is increased.
  • The sensor produces an output voltage based on the resistance.

Determining the water level can be done by measuring the voltage.

Wiring Diagram

In theory, the VCC and GND pins of the sensor can be connected to the 5v and GND pins of the Arduino Nano in order to provide power to the sensor.

However, it is not advised to take this approach in practice. In a humid environment, if the sensor is supplied with power continuously, it will be electrochemically corroded faster, thus reducing its lifespan.

To avoid this, we suggest not to keep the sensor powered all the time, but only when reading its value. This can be done by connecting the sensor's VCC pin to a digital pin of an Arduino Nano, and setting the Arduino's pin to HIGH before reading and LOW afterwards.

The wiring diagram between Arduino Nano and water sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Reading Value from Water Sensor

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-water-sensor */ #define POWER_PIN 4 // The Arduino Nano pin connected to the power pin of water sensor #define SIGNAL_PIN A0 // The Arduino Nano pin connected to the signal pin of water sensor int value = 0; // variable to store the water sensor value void setup() { Serial.begin(9600); pinMode(POWER_PIN, OUTPUT); // Configure D7 pin as an OUTPUT digitalWrite(POWER_PIN, LOW); // turn the sensor OFF } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the water sensor ON delay(10); // wait 10 milliseconds value = analogRead(SIGNAL_PIN); // read the analog value from sensor digitalWrite(POWER_PIN, LOW); // turn the water sensor OFF Serial.print("Water sensor value: "); Serial.println(value); delay(1000); }

Detailed Instructions

  • Copy the code above and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Nano.
  • Gently lower the sensor into a glass of water.
  • Check the Serial Monitor to see the result; it should be 0 when the sensor is not touching anything.
COM6
Send
Water sensor value: 0 Water sensor value: 0 Water sensor value: 0 Water sensor value: 25 Water sensor value: 97 Water sensor value: 284 Water sensor value: 428 Water sensor value: 435 Water sensor value: 441 Water sensor value: 455 Water sensor value: 467 Water sensor value: 521 Water sensor value: 528 Water sensor value: 553
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The sensor should not be completely immersed in water; only the exposed traces on the PCB should come into contact with it. Please take care when installing it.

How To Detect Water Leakage

To identify water leakage, rainfall, and tank overflow, we just have to compare the reading value with a threshold value that is determined in the calibration section of this tutorial.

Let's look at a particular instance. If water is detected, Arduino Nano will activate an LED.

Wiring Diagram

The wiring diagram between Arduino Nano and Water Sensor LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Detecting Water Leakage

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-water-sensor */ #define LED_PIN 9 // The Arduino Nano pin connected to the led #define POWER_PIN 4 // The Arduino Nano pin connected to the power pin of water sensor #define SIGNAL_PIN A0 // The Arduino Nano pin connected to the signal pin of water sensor #define THRESHOLD 300 int value = 0; // variable to store the sensor value void setup() { Serial.begin(9600); pinMode(LED_PIN, OUTPUT); // Configure D2 pin as an OUTPUT pinMode(POWER_PIN, OUTPUT); // Configure D7 pin as an OUTPUT digitalWrite(POWER_PIN, LOW); // turn the sensor OFF digitalWrite(LED_PIN, LOW); // turn LED OFF } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the sensor ON delay(10); // wait 10 milliseconds value = analogRead(SIGNAL_PIN); // read the analog value from sensor digitalWrite(POWER_PIN, LOW); // turn the sensor OFF if (value > THRESHOLD) { Serial.print("The water is detected"); digitalWrite(LED_PIN, HIGH); // turn LED ON } else { digitalWrite(LED_PIN, LOW); // turn LED OFF } }

How To Measure The Water Level

If you wish to partition the maximum water height into various levels and measure the present level, you can utilize the technique in the code below. Note that the maximum water height is equivalent to the height of the sensor. The code below divides the maximum height into 4 levels.

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-water-sensor */ #define POWER_PIN 4 // The Arduino Nano pin connected to the power pin of water sensor #define SIGNAL_PIN A0 // The Arduino Nano pin connected to the signal pin of water sensor #define SENSOR_MIN 0 #define SENSOR_MAX 521 int value = 0; // variable to store the sensor value int level = 0; // variable to store the water level void setup() { Serial.begin(9600); pinMode(POWER_PIN, OUTPUT); // Configure D7 pin as an OUTPUT digitalWrite(POWER_PIN, LOW); // turn the sensor OFF } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the sensor ON delay(10); // wait 10 milliseconds value = analogRead(SIGNAL_PIN); // read the analog value from sensor digitalWrite(POWER_PIN, LOW); // turn the sensor OFF level = map(value, SENSOR_MIN, SENSOR_MAX, 0, 4); // 4 levels Serial.print("Water level: "); Serial.println(level); delay(1000); }

※ NOTE THAT:

  • SENSOR_MIN and SENSOR_MAX are determined through calibration.
  • The mapping method mentioned is not precise, yet it is suitable for many applications.
  • If you wish to make it more accurate, you can measure the threshold values for each level. Refer to the calibration section of the manual for further information.

Water Level Sensor Calibration

The output of the sensor is influenced by both the water level and the conductivity of the water. Pure water has no conductivity, whereas water with minerals and impurities is conductive. The higher the conductivity of the water, the more sensitive the sensor will be. Additionally, the output value is also affected by the voltage supplied to the VCC pin of the sensor.

To ensure accuracy when reading the water sensor, we suggest calibrating the sensor for the specific type of water to be monitored.

Prior to establishing the limit for activating a response, it is necessary to assess the genuine value obtained from the sensor by conducting an experiment.

Instructions for the test:

  • Refer to the sketch provided above when reading sensor values.
  • Place the sensor in the water at the desired threshold level.
  • Record the value that the sensor displays in the Serial Monitor.
  • Utilize this value as the threshold to activate an action.

It may require experimentation to complete this test. Be prepared to try different approaches. Be ready to experiment with various strategies. Have patience and don't give up. Have perseverance and don't surrender.

The test can be utilized to uncover:

  • SENSOR_MIN value, when the sensor is not submerged in the liquid
  • SENSOR_MAX value, when the sensor is totally submerged in the water
  • A threshold value for recognizing water spillage
  • The threshold values for each level of your degree scale.

Video Tutorial

Challenge Yourself

  • When water leakage is detected:
  • Send an email
  • Send a SMS message
  • Make a sound alarm

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!