Project 2.6.21: Smart Irrigation Sms Alert
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project adds a cloud-based phone alert to an irrigation cycle so the operator knows when watering has happened.
Students will build a soil-triggered irrigation controller and send a webhook notification through a service such as IFTTT after a watering cycle completes.
The final system should water only when the soil is dry enough, then send a phone-alert event without spamming repeated messages during one drying period.
Required Components
![]() Soil moisture sensor |
![]() LED and 220 Ω resistor |
![]() Raspberry Pi Pico 2 W |
![]() 1-channel relay module |
![]() Small DC pump |
![]() External power supply |
Phone with notification service |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC moisture input |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Pump control |
| Status LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Pump activity indicator |
| Status LED cathode | GND | Any GND pin | LED return |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ │
│ GPIO 15 ───────────┤──── Relay IN
│ │
│ GPIO 16 ────220Ω───┤──── Status LED (+)
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ │
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── Status LED (-)
│ GND ───────────┤──── Relay GND
│ │
└─────────────────────┘
Relay Module External Supply
┌──────────┐ ┌────────────┐
│ COM ────┼────────────┤ (+) │
│ NO ────┼──┐ │ │
│ VCC ────┼──┼─────────┤ (+) (if │
│ GND ────┼──┼──┐ │ needed) │
└──────────┘ │ │ │ │
│ │ └────────────┘
│ │ │
│ └───────────┘
│ Pump (+)
│
└────────────── Pump (-) ──── External Supply (-)
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.
Step 2: Position the Soil Moisture Sensor
Place the soil moisture probe so the sensing end can enter the soil sample. Identify VCC, GND, and AOUT / AO / Signal before wiring.
Step 3: Connect the Soil Sensor
Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 26 (ADC0).
Step 4: Place and Wire the Relay Module
Identify relay VCC, GND, IN, COM, NO, and NC before wiring. Connect relay IN to GPIO 15. Power the relay module from the supply required by its label. Connect relay GND to Pico GND and to the external pump supply GND where shared grounding is required.
Step 5: Place and Connect the Status LED
Place the status LED on the breadboard. Identify the long leg as the anode (+) and the short leg as the cathode (-). Connect the status LED long leg through a 220Ω resistor to GPIO 16. Connect the status LED short leg to GND.
Step 6: Wire the Pump Through the Relay
Connect the external pump supply positive wire to relay COM. Connect relay NO to the pump positive lead. Connect the pump negative lead to the external pump supply negative wire.
Step 7: Prepare for Notification Testing
Configure the webhook event name and key in the code before testing notifications. Keep the hardware supervised during the first notification-triggered pump cycle.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Relay IN connects to GPIO 15
- [x] Status LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Status LED short leg connects to GND
- [x] Pump positive path uses relay COM and NO
- [x] Pump negative connects to external pump supply negative
- [x] Pico 2W is connected to Wi-Fi for the notification part
- [x] No loose jumper wires
Intermediate Note When running the networking part, the Pico 2W must be connected to Wi-Fi. Replace the SSID and PASSWORD placeholders in the MicroPython code before testing. Configure the webhook event name and key before expecting an SMS or notification. Test the pump cycle locally before relying on the remote alert.
Safety Note Do not power the pump from the Pico. Use an external pump supply, keep electronics away from water, and never let the pump run dry.
Testing Individual Components
Before running the full project, test each part separately. This makes it easier to find wiring, library, or code problems.
Hardware Setup
- Build the soil sensor, relay, and LED wiring before any network alert testing
- Keep the pump disconnected until the local irrigation logic is verified
Test the Input Sensor
- Calibrate the soil sensor and compare the live moisture percentage with real dry and wet conditions
- Confirm that the recovery threshold re-arms the system only after the soil becomes wetter again
Test the Output Device
- Test the relay and status LED before connecting the real pump
- Run one short supervised pump cycle locally before attempting a live notification
Test Communication
- Connect the Pico to Wi-Fi and verify that the notification service is configured
- Trigger one watering event and confirm that the phone receives the webhook-based alert
Run the Full System
- Allow the system to complete another dry-soil cycle and confirm that the cooldown prevents duplicate alerts
- Compare the local LED and pump behaviour with the timing of the phone notification
Save the Project
- Save the final code and record the alert cooldown and webhook event settings used in your test
- Write down whether your external service delivered a true SMS or a different type of phone notification
Additional Testing and Calibration Checks
Calibration and tuning notes
- Verify the local irrigation behaviour before testing remote notifications
- Remember that the default implementation sends a webhook-based phone alert; true SMS depends on the external service you attach to it
- If the service delivers no alert, recheck the event name, key, and network access before changing the irrigation code
- Use the cooldown to avoid repeated test alerts while you are still debugging
Quick testing checklist
- [ ] Wi-Fi connection succeeds
- [ ] Local irrigation runs only when soil is dry enough
- [ ] The status LED matches the pump cycle
- [ ] The phone receives a webhook-based alert after watering completes
- [ ] Cooldown prevents repeated alert spam
Full Project Code
After completing and checking the circuit connections, open Thonny IDE. Copy and paste the code below into a new file, or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.
import network
import socket
import ssl
import time
from machine import ADC, Pin
SSID = "YOUR_WIFI_NAME"
PASSWORD = "YOUR_WIFI_PASSWORD"
IFTTT_KEY = "YOUR_IFTTT_KEY"
EVENT_NAME = "irrigation_alert"
RELAY_ON = 0
RELAY_OFF = 1
PUMP_RUN_S = 5
MIN_REST_S = 60
ALERT_COOLDOWN_S = 300
soil = ADC(26)
relay = Pin(15, Pin.OUT)
status_led = Pin(16, Pin.OUT)
soil_dry = 52000
soil_wet = 22000
dry_threshold = 35
recovery_threshold = 45
last_cycle = time.time() - MIN_REST_S
last_alert = time.time() - ALERT_COOLDOWN_S
ready_to_water = True
relay.value(RELAY_OFF)
status_led.off()
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def soil_percent():
raw = soil.read_u16()
span = soil_dry - soil_wet
if span == 0:
return 0
percent = int(((soil_dry - raw) * 100) / span)
return clamp(percent, 0, 100)
def connect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print("Connecting to Wi-Fi...")
wlan.connect(SSID, PASSWORD)
start = time.time()
while not wlan.isconnected():
if time.time() - start > 20:
raise RuntimeError("Wi-Fi connection timeout")
time.sleep(0.5)
print("Wi-Fi connected:", wlan.ifconfig()[0])
def send_notification(message):
global last_alert
body = '{{"value1":"{}"}}'.format(message.replace('"', "'"))
path = "/trigger/{}/with/key/{}".format(EVENT_NAME, IFTTT_KEY)
address = socket.getaddrinfo("maker.ifttt.com", 443)[0][-1]
raw = socket.socket()
raw.settimeout(10)
raw.connect(address)
client = ssl.wrap_socket(raw)
request = (
"POST {} HTTP/1.1\r\n"
"Host: maker.ifttt.com\r\n"
"Content-Type: application/json\r\n"
"Content-Length: {}\r\n"
"Connection: close\r\n\r\n{}"
).format(path, len(body), body)
client.write(request.encode())
client.read(128)
client.close()
last_alert = time.time()
def water_cycle(moisture):
global last_cycle, ready_to_water
relay.value(RELAY_ON)
status_led.on()
print("Pump ON: irrigation alert cycle started")
time.sleep(PUMP_RUN_S)
relay.value(RELAY_OFF)
status_led.off()
print("Pump OFF: cycle complete")
last_cycle = time.time()
ready_to_water = False
if time.time() - last_alert >= ALERT_COOLDOWN_S:
send_notification("Irrigation completed at soil moisture {}%".format(moisture))
connect_wifi()
print("Smart irrigation SMS alert ready")
while True:
moisture = soil_percent()
if moisture >= recovery_threshold:
ready_to_water = True
if ready_to_water and moisture <= dry_threshold and (time.time() - last_cycle) >= MIN_REST_S:
water_cycle(moisture)
else:
print("Moisture:{}% Ready:{}".format(moisture, ready_to_water))
time.sleep(10)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
send_notification() |
Builds and sends the HTTPS webhook request | Remote alerts are only useful when the network message is formed correctly |
ALERT_COOLDOWN_S |
Limits how often alerts can be sent | Cooldown prevents message spam during one extended dry period |
water_cycle() |
Runs the local pump action and sends the remote event after completion | The alert should describe a real completed action |
| Recovery threshold logic | Re-arms the irrigation system after the soil becomes wetter again | This reduces repeated watering and repeated alerting |
Expected Result
When the soil falls below the dry threshold, the system should run one timed watering cycle, light the status LED, and then send a webhook-based phone alert. Because of the cooldown timer, repeated alerts should not be sent continuously during one drying event.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The phone never receives an alert | The webhook key, event name, or service setup is wrong | Recheck the external service configuration and the Pico network connection |
| Alerts arrive too often | The cooldown is too short or the soil never reaches the recovery threshold | Increase ALERT_COOLDOWN_S and inspect the re-arm logic |
| The pump waters but no notification is sent | The HTTPS request failed after the local cycle | Add temporary debug prints around send_notification() and verify network access |
| The system never waters | The soil threshold or calibration is wrong | Recalibrate the sensor and compare the live value with the dry threshold |





