Arduino MicroPython Water Sensor

This guide will show you how to use a water sensor with Arduino and MicroPython to detect water leaks, measure rainfall, check if a tank is overflowing, or monitor water levels. You will learn:

Arduino MicroPython and water sensor module

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Water level sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 three pins:

  • S (Signal) pin: Analog output; connect to the Arduino's analog input
  • + (VCC) pin: Provides power to the sensor; use between 3.3V and 5V
  • - (GND) pin: Ground connection
water sensor pinout

How Water Level Sensor Works

The sensor is made up of ten visible copper wires: five for power and five for sensing. They are arranged in a pattern, with one sensor wire between every two power wires. Normally, these wires don't touch, but when the sensor is in water, the water connects them.

Think of the wires like a special knob that changes resistance based on how much water is around:

  • Higher water levels mean lower resistance.
  • More water improves conductivity, lowering resistance.
  • Less water reduces conductivity, raising resistance.

The sensor uses this resistance to produce an output voltage, which helps us measure the water level.

Wiring Diagram

Connect the sensor’s VCC pin to the 5V pin on the Arduino and the GND pin to the GND pin on the Arduino to power it. It’s not a good idea to keep the sensor powered on in a wet environment, as this can damage it and shorten its lifespan. To prevent this, only power the sensor when you need to read its data. Connect the sensor’s VCC pin to a digital pin on the Arduino. To read the sensor, set the Arduino pin to HIGH, and after reading, set it to LOW.

The wiring diagram between Arduino MicroPython Water Sensor

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code - Reading Value from Water Sensor

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-water-sensor """ from machine import Pin, ADC import time POWER_PIN = 'D3' # The Arduino Giga WiFi pin D3 connected to the VCC pin of water sensor SIGNAL_PIN = 'A0' # The Arduino Giga WiFi pin A0 connected to the S pin of water sensor # Setup power pin power = Pin(POWER_PIN, Pin.OUT) power.value(0) # Initially turn off the sensor # Setup ADC for reading the water sensor signal = ADC(Pin(SIGNAL_PIN)) while True: power.value(1) # Power on the sensor time.sleep(0.01) # Wait 10ms for sensor to stabilize value = signal.read_u16() >> 4 # Read the 16-bit analog value from the water sensor and convert back to 12-bit ADC power.value(0) # Power off the sensor print("Sensor value:", value) # Print the value time.sleep(1) # Wait for a second before next read

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the water sensor according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Slowly put the sensor into a glass of water.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Sensor value: 3 Sensor value: 10 Sensor value: 69 Sensor value: 487 Sensor value: 1435 Sensor value: 1611 Sensor value: 1637 Sensor value: 1713 Sensor value: 1839 Sensor value: 1965 Sensor value: 2082 Sensor value: 2111 Sensor value: 2210
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

※ NOTE THAT:

Don’t fully submerge the sensor; only let the visible parts of the electronic board touch the water. Be careful when setting it up.

How To Detect Water Leakage

To check for water leaks, rain, or overflowing tanks, compare the sensor reading to a preset limit. This limit is set during the calibration step of this tutorial. The Arduino MicroPython code below shows how to detect water leaks.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-water-sensor """ from machine import Pin, ADC import time POWER_PIN = 'D3' # The Arduino Giga WiFi pin D3 connected to the VCC pin of water sensor SIGNAL_PIN = 'A0' # The Arduino Giga WiFi pin A0 connected to the S pin of water sensor THRESHOLD = 1000 # Setup the power pin power = Pin(POWER_PIN, Pin.OUT) power.value(0) # Initially turn off the sensor # Setup ADC for reading the water sensor signal = ADC(Pin(SIGNAL_PIN)) # Initialize the power pin to off power.value(0) while True: power.value(1) # Turn on power to the sensor time.sleep_ms(10) # Short delay to allow sensor to stabilize value = signal.read_u16() >> 4 # Read the 16-bit analog value from the water sensor and convert back to 12-bit ADC power.value(0) # Turn off power to the sensor if value > THRESHOLD: # Check if the water level exceeds the threshold print("Water detected") time.sleep(1) # Delay for a second before the next reading

How To Measure The Water Level

To divide the highest water level into different stages and find the current stage, use the method in the code below. Remember, the highest water level means the sensor is fully submerged. This code breaks the full height into 4 stages.

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-water-sensor """ from machine import Pin, ADC import time POWER_PIN = 'D3' # The Arduino Giga WiFi pin D3 connected to the VCC pin of water sensor SIGNAL_PIN = 'A0' # The Arduino Giga WiFi pin A0 connected to the S pin of water sensor SENSOR_MIN = 0 SENSOR_MAX = 2088 # Setup the power pin power = Pin(POWER_PIN, Pin.OUT) # Setup ADC for reading the water sensor signal = ADC(Pin(SIGNAL_PIN)) # Function to map the sensor value from one range to another def map_value(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min power.value(0) # Initially turn off the sensor while True: power.value(1) # Activate the sensor time.sleep_ms(10) # Wait for the sensor to stabilize value = signal.read_u16() >> 4 # Read the 16-bit analog value from the water sensor and convert back to 12-bit ADC power.value(0) # Deactivate the sensor # Map the value to a 0-4 scale, adjusting for 16-bit ADC value level = map_value(value, SENSOR_MIN, SENSOR_MAX * 4095, 0, 4) # Print the water level print("Water level:", level) time.sleep(1) # Delay for one second before repeating

※ NOTE THAT:

  • SENSOR_MIN and SENSOR_MAX are set during calibration.
  • The mapping method explained may not be super precise, but it’s good enough for most uses. For better accuracy, measure the threshold values for each level as explained in the calibration section.

Water Level Sensor Calibration

The sensor's output depends on both the water level and its conductivity. Pure water doesn't conduct electricity, but water with minerals and impurities does. The more conductive the water, the more sensitive the sensor is. The output can also change based on the voltage supplied to the sensor's VCC pin.

To get accurate readings from the water sensor, it's best to calibrate it for the specific type of water you want to monitor. Before setting a threshold to trigger an action, test the actual value the sensor reads.

How to perform the test:

  • Use the code above to read the sensor's value.
  • Dip the sensor into the water at the level where you want to set the threshold.
  • Note the value shown in the Shell at the bottom of Thonny.
  • Use this value as the threshold to trigger an action.
  • You may need to repeat this test a few times to get it right.

The test can also help you find:

  • The SENSOR_MIN value when the sensor isn’t in water.
  • The SENSOR_MAX value when the sensor is fully submerged.
  • A threshold value to detect water leaks.
  • Threshold values for different levels on your scale.

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