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
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
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.
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.
This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code - Reading Value from Water Sensor
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.
※ 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
This image is created using Fritzing. Click to enlarge image
Arduino Nano ESP32 Code - Detecting Water Leakage
How To Measure The Water Level
The below code divides the water level into 4 levels.
※ 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.