Project 4.1.48: Climate-resilient Farming System
Overview
Build and prototype a climate-resilient farming system using the Raspberry Pi Pico 2 W and MicroPython.
This expert project is designed as an open-ended research and prototype challenge. Learners should use the concept, suggested components, and starter code as a guide, then conduct further research, improve the design, and build a working prototype.
The final result should demonstrate a working embedded system prototype with clear documentation, testing evidence, and a short presentation of design decisions.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Rain sensor |
![]() Temperature sensor |
![]() Breadboard |
![]() Jumper wires |
Components
Circuit Connections
| Component Pin | Connects To | Pico GPIO / Physical Pin Number | Notes |
|---|---|---|---|
| Rain Sensor: Analog Output | ADC0 (Pin 26) | See source design notes | Verify pin compatibility and voltage level |
| Temperature Sensor: Output | ADC1 (Pin 27) | See source design notes | Verify pin compatibility and voltage level |
Step-by-Step Assembly
Hardware Setup
Step 1: Connect the Rain Sensor
Connect the rain sensor analog output to ADC0 (GPIO 26) on the Raspberry Pi Pico 2 W.
Step 2: Connect the Temperature Sensor
Connect the temperature sensor output to ADC1 (GPIO 27) on the Raspberry Pi Pico 2 W.
Step 3: Verify All Connections
Confirm all wiring connections before powering the circuit.
Testing Individual Components
Before running the full project, test each subsystem separately. This makes it easier to find wiring, code, or logic problems.
- Test each sensor reading in the Thonny Shell.
- Test each actuator using a safe low-power test.
- Verify communication between system layers.
- Record results and adjust the prototype before final integration.
Full Project Code
After completing and checking the circuit connections, open Thonny IDE, copy and paste this code into a new file or upload the project file to the Raspberry Pi Pico 2 W, then run it from Thonny.
Starter Code
# Climate-Resilient Farming System - Project 353
from machine import Pin, ADC
import time
import math
rain_sensor = ADC(Pin(26))
temp_sensor = ADC(Pin(27))
def read_climate_data():
"""Layer 1: Climate Sensor Node"""
rain_raw = rain_sensor.read_u16()
temp_raw = temp_sensor.read_u16()
# Rain: Lower = wet, Higher = dry
rain_level = int((rain_raw / 65535) * 100)
# Temperature calculation
voltage = (temp_raw / 65535) * 3.3
resistance = (33000 / voltage) - 10000
temperature = 1 / (
math.log(resistance / 10000) / 3950 + 1 / 298.15
) - 273.15
return {
"rain_level": rain_level,
"temperature": round(temperature, 1),
"timestamp": time.time()
}
def assess_climate_risk(data):
"""Layer 2: Climate Risk Logic"""
rain = data["rain_level"]
temp = data["temperature"]
drought_risk = "HIGH" if rain < 20 else \
"MODERATE" if rain < 40 else \
"LOW"
heat_risk = "HIGH" if temp > 35 else \
"MODERATE" if temp > 30 else \
"LOW"
if drought_risk == "HIGH" or heat_risk == "HIGH":
return "DROUGHT_RISK", 85
elif drought_risk == "MODERATE" or heat_risk == "MODERATE":
return "STABLE", 50
else:
return "OPTIMAL", 20
def generate_advisory(farm_id, risk_level, risk_score):
advisories = {
"DROUGHT_RISK": [
"Activate emergency irrigation",
"Implement water conservation",
"Alert agricultural extension"
],
"STABLE": [
"Continue monitoring",
"Prepare irrigation schedule"
],
"OPTIMAL": [
"Normal farming operations",
"Ideal conditions"
]
}
return {
"farm_id": farm_id,
"risk_level": risk_level,
"risk_score": risk_score,
"advisories": advisories.get(risk_level, []),
"crop_status": "STRESSED" if risk_score > 70 else "HEALTHY"
}
# Main loop
def start_climate_system():
farm_id = "FARM_001"
while True:
data = read_climate_data()
risk_level, risk_score = assess_climate_risk(data)
advisory = generate_advisory(
farm_id,
risk_level,
risk_score
)
print("\nClimate Data:")
print(f" Rain: {data['rain_level']}%")
print(f" Temperature: {data['temperature']}°C")
print("\nRisk Assessment:")
print(f" Level: {risk_level}")
print(f" Score: {risk_score}")
print(f" Crop Status: {advisory['crop_status']}")
print("\nAdvisories:")
for a in advisory['advisories']:
print(f" - {a}")
time.sleep(15)
start_climate_system()
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Starter code | Implements one layer or subsystem of the prototype | Separating the project into parts makes expert systems easier to test and improve |
Expected Result
The system should collect data, process it, control the required outputs, and show clear evidence that the prototype solves the intended problem.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| No readings appear in Thonny | Incorrect wiring, wrong pins, or missing library | Recheck the circuit, confirm pin numbers, and test each module separately |
| Import error appears | Library file is missing or saved in the wrong location | Check the Pico file structure and save libraries exactly as instructed |
| Actuator does not respond | Relay/driver wiring or external power issue | Verify driver input, external power, and shared ground where required |
| Results are unstable | Sensor noise, loose wiring, or poor calibration | Secure the wiring and add calibration or averaging in code |
| Prototype works only sometimes | System is too complex to test all at once | Return to subsystem testing and integrate one part at a time |




