Project 2.1.24: Soil Moisture Cloud Analytics
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project sends calibrated soil moisture data from the Pico 2 W to an online dashboard so the readings can be viewed later as a trend.
Students will build a Wi-Fi node, calibrate an analog moisture sensor, and send updates to a ThingSpeak channel using a simple HTTP request.
The final system should connect to Wi-Fi, print live moisture percentages locally, and upload new values to the cloud at a fixed interval.
Required Components
![]() Soil moisture sensor |
![]() Raspberry Pi Pico 2 W |
![]() Phone or computer |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor VCC | 3.3V | Physical pin 36 | Sensor power |
| Soil sensor GND | GND | Physical pin 38 | Common ground |
| Soil sensor AOUT | GPIO 26 | GPIO 26 / physical pin 31 | ADC input |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ │
│ 3.3V ───────────┤──── Soil Sensor VCC
│ GND ───────────┤──── Soil Sensor GND
│ │
└─────────────────────┘
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. Check the module labels and identify VCC, GND, and AOUT / AO / Signal.
Step 3: Connect Sensor VCC
Connect the soil sensor VCC pin to 3.3V.
Step 4: Connect Sensor GND
Connect the soil sensor GND pin to GND.
Step 5: Connect AOUT to GPIO 26
Connect the soil sensor AOUT, AO, or Signal pin to GPIO 26 (ADC0).
Step 6: Stabilize the Sensor
Keep the probe cable still so the cloud or analytics readings do not jump because of loose wiring.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] Soil sensor VCC connects to 3.3V
- [x] Soil sensor GND connects to GND
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Pico 2W is connected to Wi-Fi for the networking 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. Test dry soil and wet soil before using the readings for analytics. Configure the code's web or cloud endpoint before expecting uploads.
Safety Note Do not pour water onto the Pico, breadboard, USB cable, or jumper wires. Add water to the soil sample away from the electronics.
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 only the soil sensor wiring for this project
- Keep the analog wiring neat because bad calibration will produce bad cloud data
Test the Input Sensor
- Read dry and wet sensor values locally first and update the calibration numbers in the code
- Confirm that the moisture percentage makes sense before trying any cloud upload
Test Communication
- Connect the Pico to Wi-Fi and confirm that an IP address is printed
- Send one cloud update and check your ThingSpeak field for the new value
Run the Full System
- Leave the node running long enough to see repeated uploads at the configured interval
- Move the sensor between drier and wetter samples and confirm that the dashboard trend changes accordingly
Save the Project
- Save the final code and record the calibration values and report interval
- Record the channel name used so you can compare it with later cloud projects
Additional Testing and Calibration Checks
Calibration and tuning notes
- Calibrate the soil sensor before judging whether the cloud data is correct
- Start with a short send interval for testing, then return to a more realistic value later
- If uploads fail, verify the API key and check whether your network allows the request
- Watch the cloud graph change as you move the sensor between dry and wet samples
Quick testing checklist
- [ ] Wi-Fi connection succeeds and an IP address appears
- [ ] Soil moisture values change with real sensor conditions
- [ ] A cloud update is accepted by the service
- [ ] The dashboard shows multiple points over time
- [ ] The send interval prevents constant traffic
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 time
from machine import ADC
SSID = "YOUR_WIFI_NAME"
PASSWORD = "YOUR_WIFI_PASSWORD"
THINGSPEAK_HOST = "api.thingspeak.com"
API_KEY = "YOUR_WRITE_API_KEY"
soil = ADC(26)
dry_reading = 52000
wet_reading = 22000
SEND_INTERVAL_S = 60
last_send = time.time() - SEND_INTERVAL_S
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def moisture_percent():
raw = soil.read_u16()
span = dry_reading - wet_reading
if span == 0:
return 0
percent = int(((dry_reading - 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])
return wlan
def send_to_thingspeak(moisture):
path = "/update?api_key={}&field1={}".format(API_KEY, moisture)
address = socket.getaddrinfo(THINGSPEAK_HOST, 80)[0][-1]
client = socket.socket()
client.settimeout(10)
client.connect(address)
request = "GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n".format(path, THINGSPEAK_HOST)
client.send(request.encode())
response = b""
while True:
chunk = client.recv(256)
if not chunk:
break
response += chunk
client.close()
return response.startswith(b"HTTP/1.1 200") or response.startswith(b"HTTP/1.0 200")
connect_wifi()
print("Soil moisture cloud analytics ready")
while True:
moisture = moisture_percent()
now = time.time()
print("Moisture:{}%".format(moisture))
if now - last_send >= SEND_INTERVAL_S:
if send_to_thingspeak(moisture):
print("Cloud update sent successfully")
last_send = now
else:
print("Cloud update failed")
time.sleep(5)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
connect_wifi() |
Brings the Pico onto the network with a timeout | Cloud projects fail early if Wi-Fi is not handled clearly |
moisture_percent() |
Converts raw ADC readings into calibrated percentages | Cloud analytics are only useful if the uploaded values are meaningful |
send_to_thingspeak() |
Creates and sends the HTTP request over a socket | Students see what a simple IoT data upload really looks like |
SEND_INTERVAL_S |
Controls how often data is uploaded | Throttling prevents unnecessary traffic and messy dashboards |
Expected Result
The Pico should join Wi-Fi, print a live soil moisture value in Thonny, and periodically upload that value to the configured ThingSpeak channel. After a few updates, the dashboard should show a visible trend rather than just one single reading.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| Uploads always fail | The API key or host settings are wrong | Recheck the ThingSpeak channel settings and the API_KEY value |
| The dashboard shows unrealistic values | The soil sensor is uncalibrated | Capture fresh dry and wet references and update the code |
| Wi-Fi never connects | SSID or password is wrong or the signal is weak | Confirm the network credentials and test closer to the router |
| The graph updates too often or too rarely | SEND_INTERVAL_S is not set appropriately |
Choose a shorter interval for testing and a longer one for real logging |



