Arduino Nano ESP32 - Water Sensor

This tutorial provides instructions on how to use Arduino Nano ESP32 and water sensor to detect rainfall, water leakage, tank overflow, and measure the water level.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Water level sensor
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano ESP32

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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Water Level Sensor

Water Level Sensor Pinout

The water level sensor includes 3 pins:

  • S (Signal) pin: This pin outputs analog voltage that is in proportion to the water level. This pin should be connected to an analog input pin of Arduino Nano ESP32.
  • + (VCC) pin: connects this pin to 3.3V or 5V.
  • - (GND) pin: connects this pin to GND.
water sensor pinout

How Water Level Sensor Works

Simply, the more water the sensor is sank in, the higher the output voltage in the S pin is. By measuring the voltage, we can know the water level.

Wiring Diagram

In theory, we can provide power to the water sensor by connecting the sensor's VCC and GND pins to ESP32's 3.3V and GND pins, respectively.

However, that way is not recommended in practice. If we supply power to the water sensor constantly, the water sensor is electrochemically corroded faster by the moist environment ⇒ It is better to provide power to the water sensor only and only when reading the value from the sensor. To do so, We can connect the sensor's VCC pin to an ESP32's digital pin, and program the ESP32's pin to HIGH before reading and LOW after reading.

The wiring diagram between Arduino Nano ESP32 and Water Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code - Reading Value from Water Sensor

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-water-sensor */ #define POWER_PIN D4 // Arduino Nano ESP32 pin D4 connected to sensor's VCC pin #define SIGNAL_PIN A0 // Arduino Nano ESP32 pin A0 connected to sensor's signal pin int value = 0; // variable to store the sensor value void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); pinMode(POWER_PIN, OUTPUT); // Configure 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 Serial.print("The water sensor value: "); Serial.println(value); delay(1000); }

Detailed Instructions

  • If this is the first time you use Arduino Nano ESP32, see how to setup environment for Arduino Nano ESP32 on Arduino IDE.
  • Copy the above code and paste it to Arduino IDE.
  • Compile and upload code to Arduino Nano ESP32 board by clicking Upload button on Arduino IDE
  • Slowly dip the sensor into the water ( a glass of water).
  • Check out the result on the Serial Monitor. It looks like the below:. The value 0 when the sensor is not touching anything.
COM6
Send
The water sensor value: 0 The water sensor value: 0 The water sensor value: 0 The water sensor value: 25 The water sensor value: 196 The water sensor value: 587 The water sensor value: 625 The water sensor value: 1434 The water sensor value: 1449 The water sensor value: 1454 The water sensor value: 1469 The water sensor value: 2525 The water sensor value: 2526 The water sensor value: 2558
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The water sensor is not designed to be completely sank, only the exposed traces on the PCB can embed into the water. Please be careful to install it.

How To Detect Water Leakage

To detect the water leakage, rainfall, and tank overflow, we just need to compare the sensor's value with a threshold. The threshold is determined in the calibration part of this tutorial.

Let's make Arduino Nano ESP32 turns on an LED if the water leakage is detected.

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Water Sensor LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano ESP32 Code - Detecting Water Leakage

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-water-sensor */ #define LED_PIN D9 // Arduino Nano ESP32 pin D9 connected to LED pin #define POWER_PIN D4 // Arduino Nano ESP32 pin D4 connected to sensor's VCC pin #define SIGNAL_PIN A0 // Arduino Nano ESP32 pin A0 connected to sensor's signal pin #define THRESHOLD 1000 int value = 0; // variable to store the sensor value void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); pinMode(LED_PIN, OUTPUT); // Configure pin as an OUTPUT pinMode(POWER_PIN, OUTPUT); // Configure 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

The below code divides the water level into 4 levels.

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-water-sensor */ #define LED_PIN D9 // Arduino Nano ESP32 pin D9 connected to LED pin #define POWER_PIN D4 // Arduino Nano ESP32 pin D4 connected to sensor's VCC pin #define SIGNAL_PIN A0 // Arduino Nano ESP32 pin A0 connected to sensor's signal pin #define THRESHOLD 1000 int value = 0; // variable to store the sensor value void setup() { Serial.begin(9600); // set the ADC attenuation to 11 dB (up to ~3.3V input) analogSetAttenuation(ADC_11db); pinMode(LED_PIN, OUTPUT); // Configure pin as an OUTPUT pinMode(POWER_PIN, OUTPUT); // Configure 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 } }

※ NOTE THAT:

  • SENSOR_MIN and SENSOR_MAX is determined by the calibration process.
  • The above mapping method is not accurate. However, it is acceptable in many applications.

Water Level Sensor Calibration

See how to calibrate the water lever sensor

※ NOTE THAT:

This tutorial uses the analogRead() function to read values from an ADC (Analog-to-Digital Converter) connected to a sensor or component. The Arduino Nano ESP32's ADC is suitable for projects that do not require high accuracy. However, for projects needing precise measurements, keep the following in mind:

  • The Arduino Nano ESP32's ADC is not perfectly accurate and might require calibration for correct results. Each Arduino Nano ESP32 board can vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you want.

For projects requiring high precision, consider using an external ADC (e.g ADS1115) with the Arduino Nano ESP32 or using another Arduino, such as the Arduino Uno R4 WiFi, which has a more reliable ADC. If you still want to calibrate the Arduino Nano ESP32's ADC, refer to the ESP32 ADC Calibration Driver.

Video Tutorial

※ 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!