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:
- How to connect the water sensor to Arduino
- How to write MicroPython code for Arduino to read values from the water sensor
- How to write MicroPython code for Arduino to detect water leaks, rainfall, and tank overflow
- How to write MicroPython code for Arduino to measure water levels
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 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
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.
This image is created using Fritzing. Click to enlarge image
Arduino MicroPython Code - Reading Value from Water Sensor
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.
※ 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.
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.
※ 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.