Skip to content

Project 1.2.16

Soil Dryness Indicator

Overview

Build a soil dryness monitor that warns when soil moisture is too low.

This project combines an analog moisture sensor, a buzzer alert, an OLED display, and a local web page.

The final result should show a moisture percentage, sound an alert when the soil is too dry, and display the same status in a browser.

Required Components

Pico 2 W
Raspberry Pi Pico 2 W
Soil Moisture Sensor
Soil moisture sensor
OLED Display
SH1106 OLED display
Buzzer
Active buzzer
Breadboard
Breadboard
Jumper Wires
Jumper wires
2.4 GHz Wi-Fi network Phone or computer browser

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
Soil sensor VCC 3.3V Physical pin 36
Soil sensor GND GND Physical pin 38
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC input
Buzzer positive (+) GPIO 0 GPIO 0 / physical pin 1
Buzzer negative (-) GND Physical pin 38
OLED VCC 3.3V Physical pin 36
OLED GND GND Physical pin 38
OLED SDA GPIO 8 GPIO 8 / physical pin 11 I2C0 SDA
OLED SCL GPIO 9 GPIO 9 / physical pin 12 I2C0 SCL

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2W

Place the Raspberry Pi Pico 2W on the breadboard so it sits across the center gap. Keep the USB port facing outward so you can easily connect it to your computer.

Circuit 2

Step 2: Place the Soil Sensor, OLED, and Buzzer

Place the soil moisture sensor module on the breadboard, or position it so the probe can be inserted into soil.

Place the SH1106 OLED display module on the breadboard.

Place the active buzzer on the breadboard and identify its positive (+) and negative (-) pins.

Identify soil sensor VCC, GND, and AOUT before wiring.

Circuit 3

Step 3: Connect the Soil Sensor

Connect soil sensor VCC to 3.3V.

Connect soil sensor GND to GND.

Connect soil sensor AOUT to GPIO 26.

Circuit 4

Step 4: Connect the Buzzer

Connect the buzzer positive (+) pin to GPIO 0.

Connect the buzzer negative (-) pin to GND.

Circuit 5

Step 5: Connect OLED Power and I2C

Connect OLED VCC to 3.3V.

Connect OLED GND to GND.

Connect OLED SDA to GPIO 8.

Connect OLED SCL to GPIO 9.

Circuit 6

Wiring Check

Pico 2W is placed correctly across the breadboard center gap

Soil moisture sensor VCC connects to 3.3V

Soil moisture sensor GND connects to GND

Soil moisture sensor AOUT connects to GPIO 26

Buzzer positive pin connects to GPIO 0

Buzzer negative pin connects to GND

OLED VCC connects to 3.3V

OLED GND connects to GND

OLED SDA connects to GPIO 8

OLED SCL connects to GPIO 9

No loose jumper wires

Beginner Note

Test the sensor in dry soil and wet soil before choosing dryness thresholds.

Safety Note

Do not pour water onto the Pico or breadboard. Only the soil probe should go into the soil.

Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

Soil sensor ADC test

Check that the sensor reading changes between dry and wet conditions.

from machine import ADC, Pin
import time
adc = ADC(Pin(26))
while True:
    print(adc.read_u16())
    time.sleep(0.5)

Expected test result: The raw ADC value should change between dry air, dry soil, and wet soil.

Buzzer test

Check that the buzzer can sound before the full project code.

from machine import Pin
import time
buzzer = Pin(0, Pin.OUT)
buzzer.on()
time.sleep(0.3)
buzzer.off()

Expected test result: The buzzer should sound briefly.

OLED text test

Check that the OLED driver works.

from machine import I2C, Pin
import sh1106
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
oled = sh1106.SH1106_I2C(128, 64, i2c)
oled.fill(0)
oled.text('Soil Monitor', 12, 28, 1)
oled.show()

Expected test result: The OLED should show Soil Monitor.

Wi-Fi connection test

Check that the Pico connects to Wi-Fi and prints its IP address.

import network
import time
SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(15):
    if wlan.isconnected():
        break
    print('Connecting...')
    time.sleep(1)
print('Connected:', wlan.isconnected())
if wlan.isconnected():
    print('IP address:', wlan.ifconfig()[0])

Expected test result: The Shell should show Connected: True and print an IP address.

Full Project Code

Upload and run this code after the individual tests work correctly.

import network
import socket
import time
from machine import ADC, I2C, Pin
import sh1106

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

adc = ADC(Pin(26))
DRY_VALUE = 58000
WET_VALUE = 20000
THRESHOLD = 30

buzzer = Pin(0, Pin.OUT)

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
oled = sh1106.SH1106_I2C(128, 64, i2c)


def moisture_percent():
    raw = adc.read_u16()
    if raw >= DRY_VALUE:
        return raw, 0
    if raw <= WET_VALUE:
        return raw, 100
    percent = int((DRY_VALUE - raw) / (DRY_VALUE - WET_VALUE) * 100)
    return raw, percent


def status_text(percent):
    if percent < THRESHOLD:
        return 'DRY - WATER NOW!'
    return 'OK'


def web_page(percent, status):
    return '''<!DOCTYPE html>
<html>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <meta http-equiv='refresh' content='3'>
    <title>Soil Dryness Indicator</title>
</head>
<body style='font-family:Arial;text-align:center;padding:30px'>
    <h1>Soil Dryness Indicator</h1>
    <h2>{}%</h2>
    <p>{}</p>
    <p>Alert threshold: {}%</p>
</body>
</html>'''.format(percent, status, THRESHOLD)


wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')
for _ in range(15):
    if wlan.isconnected():
        break
    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))

address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)
server.settimeout(0.2)

while True:
    raw, percent = moisture_percent()
    status = status_text(percent)
    buzzer.value(1 if percent < THRESHOLD else 0)

    oled.fill(0)
    oled.text('Soil Moisture', 8, 8, 1)
    oled.text('{}%'.format(percent), 44, 28, 1)
    oled.text(status[:16], 0, 50, 1)
    oled.show()

    try:
        client, client_address = server.accept()
    except OSError:
        continue

    client.recv(1024)
    response = web_page(percent, status)
    client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
    client.sendall(response.encode())
    client.close()

How the Code Works

Code Section What It Does Why It Matters
Calibration values Store dry and wet ADC readings The soil sensor needs calibration before the percentage makes sense
Inverted calculation Turns lower wet readings into higher moisture percentages Many soil sensors behave in the opposite way beginners expect
Threshold check Decides when the buzzer should sound This turns the monitor into an alert system
OLED and web output Show the same dryness status in two places Students can compare local and remote display results

Expected Result

After entering your Wi-Fi details and running the code, the OLED and browser page should show the soil moisture percentage. When the moisture drops below the threshold, the buzzer should sound and the status should change to a dry warning.

Troubleshooting

Problem Possible Cause Solution
Always shows 0% Calibration values are wrong or sensor is disconnected Print raw ADC values and recheck DRY_VALUE, WET_VALUE, and wiring
Always shows 100% DRY_VALUE and WET_VALUE may be reversed Swap or recalibrate the values based on your sensor
Buzzer never sounds Threshold is too low or buzzer wiring is wrong Raise the threshold for testing and recheck the buzzer
Sensor corrodes over time This is common with resistive soil probes Remove the sensor when not needed or switch to a capacitive sensor for long-term use