Project 3.9.11: Smart Agriculture National Demo
Advanced Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
The Pico demonstrates how one farm-monitoring prototype could be adapted for different agro-ecological profiles using local mode switching.
National-scale claims are too abstract when students never test how the same prototype would behave under different local farming profiles.
A Pico 2 W prototype with a soil moisture sensor, BME280, light sensor, mode button, and OLED regional-profile dashboard.
Profile-based adaptation, transparent thresholds, and honest national-demo wording.
Required Components
![]() Raspberry Pi Pico 2 W |
![]() Soil sensor |
![]() Breadboard |
![]() Jumper wires |
![]() LDR |
![]() BME280 |
![]() Push button |
![]() OLED |
| --- |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| Soil sensor AO | GPIO 26 ADC0 | GPIO 26 / physical pin 31 | Soil input |
| Light sensor output | GPIO 27 ADC1 | GPIO 27 / physical pin 32 | Light input |
| BME280 VCC | 3.3V output | Physical pin 36 | Sensor power |
| BME280 GND | GND | Physical pin 38 | Common ground |
| BME280 SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA shared with OLED |
| BME280 SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL shared with OLED |
| Button leg 1 | GPIO 16 | GPIO 16 / physical pin 21 | Profile button input |
| Button leg 2 | 3.3V output | Physical pin 36 | High when pressed |
| OLED SDA | GPIO 20 | GPIO 20 / physical pin 26 | I2C0 SDA |
| OLED SCL | GPIO 21 | GPIO 21 / physical pin 27 | I2C0 SCL |
| --- |
Wiring Diagram

Soil sensor AO -> GPIO 26
Light sensor output -> GPIO 27
BME280 SDA -> GPIO 20
BME280 SCL -> GPIO 21
BME280 VCC -> 3.3V
BME280 GND -> GND
Button -> GPIO 16 and 3.3V
GPIO 20 -> OLED SDA
GPIO 21 -> OLED SCL
Common GND -> sensors, button, and OLED
Diagram Placeholder [Insert wiring diagram or breadboard image here]
Step-by-Step Assembly
- Connect the soil sensor analog output to GPIO 26.
- Build the light sensor analog path and connect it to GPIO 27.
- Connect the BME280 to Pico 3.3V, GND, GPIO 20 (SDA), and GPIO 21 (SCL).
- Wire the profile button between GPIO 16 and 3.3V so the input goes high when pressed.
- Connect the OLED to Pico 3.3V, GND, GPIO 20, and GPIO 21, then upload ssd1306.py to the Pico.
Testing Individual Components
Before running the full project, test each subsystem separately. This makes it easier to find wiring, library, or logic problems before full integration.
- Hardware setup: Assemble the Pico, sensor, indicator, and load wiring exactly as shown in the connection table before applying power.
- Test the input sensor: Test the sensors and the profile button separately so the adaptation demo starts from trusted values.
- Test the output device: Run an OLED hello-world test before adding the profile logic.
- Test the decision logic: Keep the same physical condition and switch profiles to compare how the recommendation changes.
- Run the full system: Run the full demo and compare the printed values with the profile-specific recommendation.
- Validate the prototype: Discuss what extra field data would be needed before adapting one prototype for real use in many regions.
- Save the project: Save the validated program on the Pico as main.py and keep a copy on the computer for future edits.
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.
Upload and run this code after the individual tests work correctly.
from machine import ADC, I2C, Pin
import bme280
import time
try:
from ssd1306 import SSD1306_I2C
HAS_OLED = True
except ImportError:
HAS_OLED = False
SOIL_PIN = 26
LIGHT_PIN = 27
BME_SDA_PIN = 20
BME_SCL_PIN = 21
BUTTON_PIN = 16
I2C_SDA_PIN = 20
I2C_SCL_PIN = 21
SOIL_DRY_RAW = 48000
SOIL_WET_RAW = 18000
PROFILES = ['COASTAL', 'FOREST', 'SAVANNA']
soil = ADC(SOIL_PIN)
light = ADC(LIGHT_PIN)
button = Pin(BUTTON_PIN, Pin.IN, Pin.PULL_DOWN)
i2c = I2C(0, sda=Pin(BME_SDA_PIN), scl=Pin(BME_SCL_PIN), freq=400000)
climate = bme280.BME280(i2c=i2c)
if HAS_OLED:
oled = SSD1306_I2C(128, 64, i2c)
profile_index = 0
last_button = 0
def clamp(value, low, high):
return max(low, min(high, value))
def dryness_percent(raw):
span = SOIL_DRY_RAW - SOIL_WET_RAW
if span <= 0:
return 0
pct = int((raw - SOIL_WET_RAW) * 100 / span)
return clamp(pct, 0, 100)
print('Smart agriculture national demo ready')
while True:
button_now = button.value()
if button_now == 1 and last_button == 0:
profile_index = (profile_index + 1) % len(PROFILES)
last_button = button_now
dry_pct = dryness_percent(soil.read_u16())
light_raw = light.read_u16()
try:
values = climate.values
temperature = float(values[0].replace('C', ''))
except Exception:
temperature = 0
profile = PROFILES[profile_index]
if profile == 'COASTAL':
recommend = 'IRRIGATE' if dry_pct >= 60 else 'WAIT'
elif profile == 'FOREST':
recommend = 'IRRIGATE' if dry_pct >= 70 else 'WAIT'
else:
recommend = 'IRRIGATE' if dry_pct >= 55 or temperature >= 32 or light_raw > 45000 else 'WAIT'
print('Profile:{} Dry:{}% Temp:{}C Light:{} Recommend:{}'.format(profile, dry_pct, temperature, light_raw, recommend))
if HAS_OLED:
oled.fill(0)
oled.text('National Demo', 0, 0)
oled.text(profile[:16], 0, 18)
oled.text('D:{} T:{}'.format(dry_pct, temperature), 0, 34)
oled.text(recommend[:16], 0, 50)
oled.show()
time.sleep(2)
How the Code Works
| Code Section | What It Does | Why It Matters | What to Modify During Testing |
|---|---|---|---|
| Profile adaptation | Applies different threshold sets to the same local measurements. | This demonstrates how one prototype might be adapted across regions without claiming a real deployment. | Change one profile at a time so the differences remain easy to explain. |
| Button profile switching | Cycles through the regional profiles with one button. | The user can compare adaptation choices quickly on the same hardware. | Use deliberate presses and keep edge detection in place. |
| OLED demo view | Shows the active profile and resulting recommendation. | The display keeps the adaptation logic visible during testing. | Verify ssd1306.py and the I2C wiring if the display is blank. |
Expected Result
The serial monitor reports the current reading or state clearly.
The hardware output responds when the decision logic changes state.
Subsystem behavior matches the thresholds, timing, or rules described in the document.
Validation Checks
- Normal condition test: confirm the system stays in its safe or idle state under baseline conditions.
- Warning condition test: move the input close to the chosen threshold and confirm the transition is sensible.
- Critical condition test: trigger the strongest alert or control state and confirm the output response is correct.
- Calibration test: compare the sensor or timing against a known reference or repeated trial.
- Limitation test: deliberately create an awkward or noisy condition and note how the prototype behaves.
Deployment and Limitations
- This prototype works well for classroom demonstrations and local monitoring pilots.
- Before deployment, it needs calibration records, protective mounting, and regular validation checks.
- Display-only systems still depend on correct sensor placement and trained users.
Troubleshooting
| Problem | Possible Cause | Solution |
|---|---|---|
| The profile changes too fast | The button is bouncing or being held too long | Use deliberate presses and verify the edge-based button logic. |
| The recommendation never changes | The thresholds do not match your test conditions | Create clearer soil or temperature changes and adjust one profile at a time. |
| Light values seem unrealistic | The light-sensor divider is incorrect | Print the raw ADC value and rebuild the analog light path if needed. |
| OLED is blank | The display library or I2C wiring is wrong | Confirm ssd1306.py is present and verify GPIO 20 and GPIO 21. |







