Arduino Mega - Water Sensor

This guide shows how to use a water sensor with the Arduino Mega. A water sensor can tell you if there is a water leak, measure rainfall, see if a tank is overflowing, or check the water level. Here is what we will learn:

Arduino Mega and water sensor module

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Water level sensor
1×Jumper Wires

Or you can buy the following 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 has three pins:

  • S (Signal) pin: It sends an analog signal. Connect it to an analog input on your Arduino Mega.
  • + (VCC) pin: It powers the sensor. Use 3.3V to 5V.
  • - (GND) pin: Ground connection.
water sensor pinout

※ NOTE THAT:

The sensor's signal pin output changes depending on the voltage you apply to its VCC pin.

How Water Level Sensor Works

When there is more water around the sensor, the voltage on the signal pin goes up.

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 ten copper wires you can see. It has five power wires and five sensor wires. The wires are placed side by side, with one sensor wire between every two power wires. Normally the wires do not touch, but if the sensor is put in water, the water connects the wires.

The metal lines on the circuit board act like an adjustable resistor, like a potentiometer, and their resistance changes with the water level.

  • The resistance changes to show how far the top of the sensor is from the water surface.
  • Resistance goes down as the water level rises.
  • When the sensor is deeper in the water, it conducts electricity better, so the resistance is lower.
  • When the sensor is shallower in the water, it conducts electricity worse, so the resistance is higher.
  • The sensor gives an output voltage that depends on the resistance.

We can tell the water level by measuring the voltage.

Wiring Diagram

Power the sensor by connecting its VCC to the Arduino Mega's 5V and its GND to the Arduino's GND.

But it's not a good idea to power the sensor all the time in a wet environment, because it can corrode quickly and shorten its life. To avoid this, we recommend powering the sensor only when you need to read its data. You can do this by connecting the sensor's VCC pin to a digital pin on an Arduino Mega. Set the pin to HIGH to read the sensor, and then set it to LOW when you're done.

The wiring diagram between Arduino Mega Water Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code - Reading Value from Water Sensor

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-water-sensor */ #define POWER_PIN 5 // Pin used to supply power to the water sensor (VCC control) #define SIGNAL_PIN A0 // Analog input connected to the water sensor's output signal int value = 0; // Stores the most recent reading from the sensor void setup() { Serial.begin(9600); // Initialize serial communication at 9600 baud pinMode(POWER_PIN, OUTPUT); // Configure POWER_PIN as an output to switch the sensor power digitalWrite(POWER_PIN, LOW); // Ensure the sensor is powered off at startup } void loop() { digitalWrite(POWER_PIN, HIGH); // Power the sensor by setting POWER_PIN high delay(10); // Wait briefly for the sensor to stabilize value = analogRead(SIGNAL_PIN); // Read the sensor's analog signal digitalWrite(POWER_PIN, LOW); // Power down the sensor to save energy Serial.print("Sensor value: "); // Output a label for the reading Serial.println(value); // Print the numeric sensor value delay(1000); // Wait 1 second before the next reading }

Detailed Instructions

Follow these steps one by one:

  • Connect the water sensor to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the Arduino Mega board and the correct COM port.
  • Copy the given code and open it in the Arduino IDE.
  • Click the Upload button to send the code to the Arduino Mega.
  • Slowly lower the sensor into the water (use a glass of water).
  • Check the result in the Serial Monitor. It shows 0 when the sensor is not touching anything.
COM6
Send
Sensor value: 0 Sensor value: 0 Sensor value: 0 Sensor value: 25 Sensor value: 97 Sensor value: 284 Sensor value: 428 Sensor value: 435 Sensor value: 441 Sensor value: 455 Sensor value: 467 Sensor value: 521 Sensor value: 528 Sensor value: 553
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

Don't put the sensor completely underwater. Only the exposed parts on the circuit board may touch water. Please install it carefully.

How To Detect Water Leakage

To check if there is a water leak, rain, or the tank is overflowing, we simply compare the reading to a fixed limit. We set this limit during the setup in this guide.

Arduino Mega Code - Water Leak Detection

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-water-sensor */ #define POWER_PIN 5 // Arduino Mega pin that powers the water sensor by feeding its VCC #define SIGNAL_PIN A0 // Arduino Mega analog pin connected to the sensor's output signal #define THRESHOLD 300 int value = 0; // Holds the latest sensor reading void setup() { Serial.begin(9600); pinMode(POWER_PIN, OUTPUT); // Configure the power control pin as an output digitalWrite(POWER_PIN, LOW); // Start with the sensor powered down } void loop() { digitalWrite(POWER_PIN, HIGH); // Turn on the sensor power delay(10); // Short delay for sensor stabilization value = analogRead(SIGNAL_PIN); // Read sensor voltage from the signal pin digitalWrite(POWER_PIN, LOW); // Turn off the sensor power if (value > THRESHOLD) { // Compare the reading to the threshold Serial.print("The water is dedected"); // Output status to the serial monitor } }

How To Measure The Water Level

To divide the highest water level into several stages and check which stage you are on, use the method shown in the code below. Remember, the highest water level matches the sensor’s height. This code divides the highest height into 4 stages.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-water-sensor */ #define POWER_PIN 5 // Controls power to the water sensor by feeding its VCC from the Arduino Mega #define SIGNAL_PIN A0 // Reads the sensor output from analog pin A0 #define SENSOR_MIN 0 // Sensor's minimum value used for mapping #define SENSOR_MAX 521 // Sensor's maximum value used for mapping int value = 0; // Stores the most recent raw ADC value int level = 0; // Stores the computed water level indicator (0-4) void setup() { Serial.begin(9600); // Initialize serial communication pinMode(POWER_PIN, OUTPUT); // Configure the power control pin as an output digitalWrite(POWER_PIN, LOW); // Ensure the sensor is powered off on startup } void loop() { digitalWrite(POWER_PIN, HIGH); // Power the sensor on delay(10); // Allow sensor to stabilize value = analogRead(SIGNAL_PIN); // Read the sensor's analog value digitalWrite(POWER_PIN, LOW); // Turn the sensor off to save power level = map(value, SENSOR_MIN, SENSOR_MAX, 0, 4); // Convert the sensor value to a 0-4 scale Serial.print("Water level: "); // Output the label Serial.println(level); // Output the calculated level delay(1000); // Wait for 1 second before the next reading }

※ NOTE THAT:

  • The minimum sensor value (SENSOR_MIN) and the maximum sensor value (SENSOR_MAX) are set during calibration.
  • The mapping method shown above may not be exact, but it works well in many cases. To be more accurate, measure the threshold values for each level as explained in the calibration section.

Water Level Sensor Calibration

How the sensor shows results depends on the water level and how well the water conducts electricity. Pure water does not conduct electricity. But water with minerals or other substances does conduct electricity. The more conductive the water, the more sensitive the sensor is. Also, the reading changes when you use different voltages on the sensor’s VCC pin.

To get accurate readings from the water sensor, set it up for the exact kind of water you will measure.

Before you set a limit for an action, first get the real reading from the sensor by testing it.

How to do the test

  • Look at the picture to read the sensor value.
  • Put the sensor in the water up to the level you want.
  • Note the number shown in the Serial Monitor.
  • Use this number as the limit to trigger an action.

This test may need a few tries to get it right.

The test can also help us find out:

  • The smallest sensor reading when the sensor is out of water.
  • The largest sensor reading when it is fully in water.
  • A limit to detect water leaks.
  • The limits for each level on your scale.

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!