Pico W water level sensor on GP28
MicroPython on a Pico W: a water level sensor on GP28 reads higher as more of it is wet, and over 30000 the program warns of a leak. The whole build, a Raspberry Pi Pico W and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $6.60. On Mokxi they're free.
What you'll learn
- Reading an analog sensor with ADC and read_u16
- How a water level sensor changes with how wet it is
- Setting a threshold with an if statement
- Powering a sensor from 3.3 V
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Raspberry Pi Pico W
- Parts: 3
How it works
The water level sensor has exposed traces. Water makes a path between them, so the more of the sensor is wet, the higher the voltage on its S pin. That goes to GP28, which is read as an analog value.
Every half second the script prints water and the reading, from 0 to 65535. If the reading is over 30000 it also prints Water leakage!
Parts list
3 parts, plus the jumper wires. They retail for about $6.60; on Mokxi they're free.
- 1 × Raspberry Pi Pico W
- 1 × Water level sensor
- 1 × Power, 3.3 V
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Water level sensor pin S | Raspberry Pi Pico W pin GP28 |
| Water level sensor pin VCC | 3.3 V |
| Water level sensor pin GND | Ground |
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# Feel the water level: the sensor's S pin is on GP28. The deeper it
# stands in water, the higher the reading, and over 30000 the program
# warns of a leak.
from machine import ADC
import time
water = ADC(28)
while True:
value = water.read_u16()
print("water", value)
if value > 30000:
print("Water leakage!")
time.sleep_ms(500)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The leak warning comes too early or too late This project
Change the 30000 in theif value > 30000check. A lower number warns earlier. - The reading never changes This project
Check that S goes to GP28, and that VCC and GND are connected. - The reading keeps changing and the probe traces corrode. Water level sensor
Do not leave power on all the time. Power the sensor only when you read it, since DC current across wet traces causes corrosion. - The values are not the same depth to depth. Water level sensor
The output depends on the water, so it is not a precise gauge. Read it with analogRead and calibrate with your own liquid at known levels. Keep the electronics above the waterline. - Pins are powered from 5 V, or a 5 V signal goes into a GPIO. The RP2040 pins are 3.3 V only and are not 5 V tolerant. Raspberry Pi Pico W
Power the board from VBUS or VSYS at 5 V, but keep GPIO signals at 3.3 V. Use a level shifter or a divider for 5 V parts. - WiFi code fails to connect, or the on-board LED code does nothing. The LED on the Pico W is not on GP25 like on the plain Pico. Raspberry Pi Pico W
Use the board's LED_BUILTIN name so the library drives it through the WiFi chip. Join a 2.4 GHz network only, with the right SSID and password, and wait for the link before sending.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.