Project 2.6.4: Smart Farm Analytics Display
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds an on-device analytics display that summarises farm sensor readings instead of showing only one raw value at a time.
Students will combine a soil moisture sensor, a BME280 sensor, an OLED display, and a page button to create a compact analytics dashboard.
The final system should show current readings, recent averages, minimum and maximum values, and a simple trend summary directly on the OLED display.
Required Components
![]() OLED display |
![]() Breadboard and jumper wires |
![]() Raspberry Pi Pico 2 W |
![]() BME280 sensor module |
![]() Soil moisture sensor (analog output) |
![]() Push button |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| OLED VCC | 3.3V | Physical pin 36 | I2C display power |
| OLED GND | GND | Any GND pin | Common ground |
| OLED SDA | GPIO 8 | GPIO 8 / physical pin 11 | I2C data |
| OLED SCL | GPIO 9 | GPIO 9 / physical pin 12 | I2C clock |
| Soil sensor AOUT | GPIO 26 / physical pin 31 | GPIO 26 / physical pin 31 | Analog soil input |
| Soil sensor VCC and GND | Physical pin 36 and any GND | Physical pin 36 and any GND | Sensor power |
| Push button one side | GPIO 5 / physical pin 7 | GPIO 5 / physical pin 7 | Uses internal pull-up |
| Push button other side | Any GND pin | Any GND pin | Pressing pulls the input low |
| BME280 VCC | 3.3V | Physical pin 36 | Sensor power |
| BME280 GND | GND | Any GND pin | Common ground |
| BME280 SDA | GPIO 2 | GPIO 2 / physical pin 4 | I2C data |
| BME280 SCL | GPIO 3 | GPIO 3 / physical pin 5 | I2C clock |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 8 ───────────┤──── OLED SDA
│ GPIO 9 ───────────┤──── OLED SCL
│ │
│ GPIO 2 ───────────┤──── BME280 SDA
│ GPIO 3 ───────────┤──── BME280 SCL
│ │
│ GPIO 26 ───────────┤──── Soil Sensor AOUT
│ GPIO 5 ───────────┤──── Push Button ──── GND
│ │
│ 3.3V ───────────┤──── OLED VCC
│ 3.3V ───────────┤──── BME280 VCC
│ 3.3V ───────────┤──── Soil Sensor VCC
│ │
│ GND ───────────┤──── OLED GND
│ GND ───────────┤──── BME280 GND
│ GND ───────────┤──── Soil Sensor GND
│ GND ───────────┤──── Push Button 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: Place and Connect the OLED Display
Place the OLED display so its I2C pins are easy to reach. Identify VCC, GND, SDA, and SCL before wiring. Connect OLED VCC to 3.3V. Connect OLED GND to GND. Connect OLED SDA to GPIO 8. Connect OLED SCL to GPIO 9.
Step 3: Place the BME280 Sensor Module
Place the BME280 module where it can sense the farm-area air. Connect BME280 VCC to 3.3V. Connect BME280 GND to GND. Connect BME280 SDA to GPIO 2 and BME280 SCL to GPIO 3. If your BME280 is bare, use the module pins labeled VCC, GND, SDA, and SCL.
Step 4: Position and Connect the Soil Sensor
Place the soil moisture probe so the sensing end can enter the soil sample. Connect soil sensor VCC to 3.3V. Connect soil sensor GND to GND. Connect soil sensor AOUT, AO, or Signal to GPIO 26.
Step 5: Place the Page Button
Place the page push button across the breadboard center gap. Connect one side of the page button to GPIO 5. Connect the opposite side of the page button to GND.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] OLED VCC connects to 3.3V
- [x] OLED GND connects to GND
- [x] OLED SDA connects to GPIO 8
- [x] OLED SCL connects to GPIO 9
- [x] BME280 SDA connects to GPIO 2 and SCL connects to GPIO 3
- [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
- [x] Page button connects between GPIO 5 and GND
- [x] No loose jumper wires
Intermediate Note Run an I2C scanner before the full analytics code to confirm the OLED is detected. The BME280 needs a short delay between readings, and the button uses the Pico internal pull-up.
Safety Note Power the OLED from 3.3V unless your exact module is clearly documented as 3.3V-safe at another voltage. Keep the Pico and OLED away from wet soil and water spills.
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 OLED I2C bus first, then add the BME280, soil sensor, and page button
- Keep SDA and SCL wires short and correctly matched so the display is detected reliably
Test the Input Sensor
- Test the BME280 and soil sensor separately and record calibration values for the soil sensor
- Press the page button and confirm the input changes before trusting page navigation
Test the Output Device
- Run
i2c.scan()and confirm the OLED is detected, then display a short test message - Verify that the display remains readable while the sensors are updating
Test Communication
- Watch the Thonny Shell and confirm the program prints the same analytics values that appear on the OLED
- Use the Shell if the OLED seems blank so you can tell whether the problem is display-only or system-wide
Run the Full System
- Let the project collect several samples so the average, minimum, and maximum values become meaningful
- Press the button to move through the analytics pages and compare the displayed values with real sensor changes
Save the Project
- Save the final code and record the soil calibration and window size used in your test
- Write down which analytics page was most useful for understanding the farm condition
Additional Testing and Calibration Checks
- Dry/wet reading test: record soil values in dry and wet samples before locking in the calibration
- Normal condition test: allow the project to collect data for a few minutes so averages become meaningful
- Button test: press the page button and confirm the OLED moves through each page once per press
- Display test: run
i2c.scan()and a short OLED text test before loading the full analytics program - Calibration note: if the soil percentage direction is reversed, swap SOIL_DRY and SOIL_WET based on your sensor output
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.
from machine import ADC, I2C, Pin
import bme280
import time
from ssd1306 import SSD1306_I2C
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=100000)
oled = SSD1306_I2C(128, 64, i2c)
i2c_bme = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
bme = bme280.BME280(i2c=i2c_bme)
def number_from_bme(text_value):
cleaned = "".join(ch for ch in str(text_value) if ch.isdigit() or ch in ".-")
return float(cleaned)
def read_bme280():
temp_text, pressure_text, humidity_text = bme.values
return number_from_bme(temp_text), number_from_bme(humidity_text), number_from_bme(pressure_text)
soil_sensor = ADC(26)
page_button = Pin(5, Pin.IN, Pin.PULL_UP)
oled_addresses = i2c.scan()
SOIL_DRY = 52000
SOIL_WET = 22000
WINDOW_SIZE = 20
SAMPLE_DELAY = 5
soil_history = []
temp_history = []
humidity_history = []
page = 0
last_button_ms = time.ticks_ms()
def clamp(value, low, high):
if value < low:
return low
if value > high:
return high
return value
def average(values):
if not values:
return 0
return sum(values) / len(values)
def soil_percent():
raw = soil_sensor.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 read_climate():
try:
temp_c, humidity, pressure_hpa = read_bme280()
return temp_c, humidity
except Exception as error:
print('BME28022 read error:', error)
return None, None
def update_history(target_list, value):
if value is None:
return
target_list.append(value)
if len(target_list) > WINDOW_SIZE:
target_list.pop(0)
def trend_label():
if len(soil_history) < 2:
return 'Collecting'
if soil_history[-1] < average(soil_history) - 5:
return 'Drying'
if soil_history[-1] > average(soil_history) + 5:
return 'Recovering'
return 'Stable'
def draw_page(current_soil, current_temp, current_humidity):
oled.fill(0)
if page == 0:
oled.text('Current Data', 0, 0)
oled.text('Soil: {}%'.format(current_soil), 0, 16)
oled.text('Temp: {}'.format('--' if current_temp is None else str(current_temp) + 'C'), 0, 28)
oled.text('Hum : {}'.format('--' if current_humidity is None else str(current_humidity) + '%'), 0, 40)
elif page == 1:
oled.text('Soil Stats', 0, 0)
oled.text('Avg: {:>5.1f}%'.format(average(soil_history)), 0, 16)
oled.text('Min: {:>5}%'.format(min(soil_history) if soil_history else 0), 0, 28)
oled.text('Max: {:>5}%'.format(max(soil_history) if soil_history else 0), 0, 40)
dry_events = sum(1 for value in soil_history if value <= 35)
oled.text('DryEv: {}'.format(dry_events), 0, 52)
else:
oled.text('Trend Summary', 0, 0)
oled.text('Trend: {}'.format(trend_label()), 0, 16)
oled.text('Tavg: {:>5.1f}C'.format(average(temp_history)), 0, 28)
oled.text('Havg: {:>5.1f}%'.format(average(humidity_history)), 0, 40)
state = 'DRY' if average(soil_history) <= 40 else 'STABLE'
oled.text('State: {}'.format(state), 0, 52)
oled.show()
print('=== Smart Farm Analytics Display ===')
if oled_addresses:
print('I2C devices found:', [hex(address) for address in oled_addresses])
else:
print('Warning: no I2C devices found. Check the OLED wiring and power.')
print('Press the page button to cycle through the OLED analytics pages.\n')
while True:
current_soil = soil_percent()
current_temp, current_humidity = read_climate()
update_history(soil_history, current_soil)
update_history(temp_history, current_temp)
update_history(humidity_history, current_humidity)
if page_button.value() == 0 and time.ticks_diff(time.ticks_ms(), last_button_ms) > 300:
page = (page + 1) % 3
last_button_ms = time.ticks_ms()
while page_button.value() == 0:
time.sleep(0.05)
draw_page(current_soil, current_temp, current_humidity)
print('Page: {} | Soil: {}% | Temp: {} | Humidity: {} | Trend: {}'.format(
page,
current_soil,
current_temp,
current_humidity,
trend_label()
))
time.sleep(SAMPLE_DELAY)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| I2C scan check | Lists detected I2C devices at startup | This gives a fast clue if the display wiring is wrong before the analytics loop runs |
| History lists | Store a rolling window of recent sensor values | Analytics need several samples, not just the newest value |
| Page button logic | Cycles the OLED between display pages | This lets one small screen show several views clearly |
| draw_page() | Renders the current OLED page based on analytics values | Students can turn statistics into a readable dashboard |
| trend_label() | Compares the latest soil value against the rolling average | A simple trend label helps students think beyond one instantaneous reading |
Expected Result
The OLED should show a current data page first, then change pages when the button is pressed.
After several samples, the statistics pages should show average, minimum, and maximum values based on the rolling history.
The Thonny Shell should print the same analytics information for debugging while the OLED shows the local display.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The OLED stays blank | The display is not detected on the I2C bus or ssd1306.py is missing | Run i2c.scan(), check GPIO 8 and GPIO 9, and confirm ssd1306.py is on the Pico |
| The button changes pages randomly | Debounce timing is too short or button wiring is loose | Check the GPIO 5 wiring and keep the debounce timing in the code |
| The statistics never change | Sensor read errors or incorrect wiring | Watch the Shell for sensor errors and check the BME280 and soil wiring |
| The trend label seems wrong | Soil calibration values do not match the sensor | Collect more readings and retune SOIL_DRY and SOIL_WET |





