Arduino Nano 33 IoT - Water Sensor

This guide shows you how to use the Arduino Nano 33 IoT and a water sensor to detect rain, water leaks, a tank overflow, and measure the water level.

Arduino Nano 33 IoT water sensor

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Water level sensor
1×Breadboard
1×Jumper Wires
1×Optionally, 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

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 has 3 pins:

  • S (Signal) pin: This pin gives an analog voltage that changes with the water level. Connect it to an analog input on the Arduino Nano 33 IoT.
  • + (VCC) pin: Connect this pin to a 3.3V or 5V power source.
  • - (GND) pin: Connect this pin to ground.
water sensor pinout

How Water Level Sensor Works

The deeper the sensor goes into the water, the higher the voltage at the S pin becomes. By checking the voltage, we can tell the water level.

Wiring Diagram

You can power the water sensor by connecting its VCC pin to the 3.3V pin and its GND pin to the GND pin on the Arduino Nano 33 IoT.

This method is not the best idea in practice. Constantly powering the water sensor makes it corrode faster because of the wet environment. It's better to only power the sensor when you need to read its value. To do this, connect the sensor's VCC pin to a digital pin on the Arduino Nano 33 IoT. Then, program that pin to be HIGH before taking a reading and LOW after finishing.

The wiring diagram between Arduino Nano and 33 IoT Water Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT Code - Reading Value from Water Sensor

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-water-sensor */ #define POWER_PIN 4 // Arduino Nano 33 IoT pin D4 connected to sensor's VCC pin #define SIGNAL_PIN A0 // Arduino Nano 33 IoT pin A0 connected to sensor's signal pin int value = 0; // variable to store the sensor value void setup() { Serial.begin(9600); 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 you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Copy the code above and paste it into the Arduino IDE.
  • Click the Upload button to compile and send the code to your Arduino Nano 33 IoT board.
  • Slowly lower the sensor into a glass of water.
  • Look at the Serial Monitor to see the result. You will see a value of 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 meant to be completely underwater. Only the parts that are meant to be wet on the circuit board can touch the water. Please be careful when installing it.

How To Detect Water Leakage

To find water leaks, rain, or a tank overflow, you only need to compare the sensor reading with a set limit. This limit is chosen during the calibration step of this guide.

Let's program the Arduino Nano 33 IoT to turn on an LED when it detects a water leak.

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT Water Sensor LED

This image is created using Fritzing. Click to enlarge image

Arduino Nano 33 IoT Code - Detecting Water Leakage

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-water-sensor */ #define LED_PIN 9 // Arduino Nano 33 IoT pin D9 connected to LED pin #define POWER_PIN 4 // Arduino Nano 33 IoT pin D4 connected to sensor's VCC pin #define SIGNAL_PIN A0 // Arduino Nano 33 IoT 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); 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:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. This can affect analog readings, so it is recommended to avoid using these pins with any devices/sensors that relies on ADC.

How To Measure The Water Level

The following code splits the water level into four parts.

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-water-sensor */ #define LED_PIN 9 // Arduino Nano 33 IoT pin D9 connected to LED pin #define POWER_PIN 4 // Arduino Nano 33 IoT pin D4 connected to sensor's VCC pin #define SIGNAL_PIN A0 // Arduino Nano 33 IoT 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); 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 are set during the calibration process. The mapping method above is not perfectly accurate, but it works well for many uses.

Water Level Sensor Calibration

Check out this guide to learn how to adjust the water level sensor: https://arduinogetstarted.com/tutorials/arduino-water-sensor#content_water_level_sensor_calibration

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!