Pico W plant moisture web page
Build it step by step
0 of 7 doneWire the sensor
VCC to 3V3(OUT), GND to GND, AOUT to GP26.
Measure the dry reading
Print read_raw() with the sensor held in dry air and note the number.
Measure the wet reading
Dip the tip, up to the line only, in a glass of water and note the number.
Edit the program
Put those two numbers in DRY and WET, and your Wi-Fi name and password in the placeholders.
Run the program
The shell prints an address like http://192.168.1.50.
Open the address
On a phone or computer on the same network.
Push the sensor into the soil
Refresh to see the reading change.
How do you build a pico W plant moisture web page?
A Pico W reads a capacitive soil moisture module and serves a dry, moist or wet message on a page on your home Wi-Fi. You need Raspberry Pi Pico W, Capacitive Soil Moisture Module and Breadboard 830 Tie Points. It takes 7 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 3
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Moisture module VCC | Pico W 3V3(OUT) pin 36 | Power at 3.3 V |
| Moisture module GND | Pico W GND pin 38 | Common ground |
| Moisture module AOUT | Pico W GP26 pin 31 | Analog signal to ADC0 |
Code
A capacitive soil sensor measures how much water is in the soil by sensing how the soil changes the electric field between two plates on the board, so nothing touches the soil with bare metal and the sensor does not corrode like the older two-prong type. It gives out a voltage that goes down as the soil gets wetter. The Pico W reads that voltage as a number between 0 and 65535, averages ten readings to smooth out noise, and turns it into a percentage using two numbers you measure yourself: the reading in dry air and the reading with the sensor tip in a glass of water. The Pico W joins your home Wi-Fi and runs a very small web server, so you can open the page on a phone on the same network. Push the sensor into the soil only up to the line on the board and keep the electronics at the top dry. Keep the page on your home network and do not forward a router port to it. The Wi-Fi name and password are placeholders; use your own and never share a copy with the real ones in it.
import time
import network
import socket
from machine import ADC
SSID = "YOUR_WIFI_NAME" # placeholders: use your own network
PASSWORD = "YOUR_WIFI_PASSWORD"
DRY = 50000 # reading in dry air, replace after you measure
WET = 22000 # reading in a glass of water, replace after you measure
sensor = ADC(26)
def read_raw():
total = 0
for _ in range(10):
total += sensor.read_u16()
time.sleep_ms(10)
return total // 10
def percent(raw):
pct = (DRY - raw) * 100 // (DRY - WET)
return max(0, min(100, pct))
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(30):
if wlan.isconnected():
break
time.sleep(0.5)
if not wlan.isconnected():
raise RuntimeError("Wi-Fi did not connect, check the name and password")
print("Open http://" + wlan.ifconfig()[0])
server = socket.socket()
server.bind(("0.0.0.0", 80))
server.listen(1)
while True:
client, addr = server.accept()
client.recv(1024)
raw = read_raw()
pct = percent(raw)
if pct < 30:
message = "Dry: time to water"
elif pct < 60:
message = "Moist: fine for now"
else:
message = "Wet: leave it alone"
page = ("<!DOCTYPE html><html><head><meta charset='utf-8'>"
"<meta name='viewport' content='width=device-width, initial-scale=1'>"
"<meta http-equiv='refresh' content='30'><title>Plant</title></head>"
"<body style='font-family:sans-serif;text-align:center;margin-top:3em'>"
"<h1>" + message + "</h1><p style='font-size:3em'>" + str(pct) + " %</p>"
"<p>Raw reading " + str(raw) + "</p></body></html>")
client.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
client.send(page)
client.close()Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Sensor in dry air | Near 0 percent, Dry |
| Sensor in a glass of water up to the line | Near 100 percent, Wet |
| Sensor in damp soil | Between, Moist |
| Water the soil | Percentage rises on the next refresh |
| Page refresh | Reloads by itself every 30 seconds |
If it does not work
Page does not open
Check the address from the shell and that the phone is on the same Wi-Fi.
Wi-Fi does not connect
Check the name and password, and that the network is 2.4 GHz.
Percentage stuck at 0 or 100
Measure the dry and wet readings again and set DRY and WET.
Readings jump around
Keep the sensor still and away from your hands while measuring.
Reading falls for no reason
Water may have reached the electronics above the line, dry the module.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico W plant moisture web page?
Raspberry Pi Pico W, Capacitive Soil Moisture Module and Breadboard 830 Tie Points.
How long does the pico W plant moisture web page take to build?
About 120 minutes for a beginner maker, in 7 steps.
How do I know the pico W plant moisture web page works?
Sensor in dry air: Near 0 percent, Dry. Sensor in a glass of water up to the line: Near 100 percent, Wet. Sensor in damp soil: Between, Moist.
Page does not open: what should I check?
Check the address from the shell and that the phone is on the same Wi-Fi.
Wi-Fi does not connect: what should I check?
Check the name and password, and that the network is 2.4 GHz.
Percentage stuck at 0 or 100: what should I check?
Measure the dry and wet readings again and set DRY and WET.
Readings jump around: what should I check?
Keep the sensor still and away from your hands while measuring.
