Skip to content

Project 2.3.10: Bluetooth-based Farm Monitor

Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython


Overview

This project creates a wireless farm monitor that streams sensor data to a phone over BLE without requiring Wi-Fi.

The real-world use case is a field edge, greenhouse corner, or temporary monitoring point where local wireless access is enough and internet access may not be available.

Students will combine soil moisture, temperature, humidity, and BLE UART communication into one portable monitoring node.

The final system should advertise over BLE, stream live readings to a phone, and respond to basic monitoring commands.

Required Components

Soil moisture sensor
Soil moisture sensor
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
BME280 sensor module
BME280 sensor module

Phone with BLUETOOTH app
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
Soil sensor AOUT GPIO 26 GPIO 26 / physical pin 31 ADC moisture input
Soil sensor VCC 3.3V Physical pin 36 Sensor power
Soil sensor 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
BME280 VCC 3.3V Physical pin 36 Sensor power
BME280 GND GND Any GND pin Common ground

Wiring Diagram

Circuit Diagram

Raspberry Pi Pico 2 W
┌─────────────────────────────────┐
│                                 │
│  GPIO 26 ───────────────────────┤──── Soil sensor AOUT / AO / Signal
│  3.3V  ─────────────────────────┤──── Soil sensor VCC
│  GND   ─────────────────────────┤──── Soil sensor GND
│                                 │
│  GPIO 2  ───────────────────────┤──── BME280 SDA
│  GPIO 3  ───────────────────────┤──── BME280 SCL
│  3.3V  ─────────────────────────┤──── BME280 VCC
│  GND   ─────────────────────────┤──── BME280 GND
│                                 │
└─────────────────────────────────┘

BLE connection:
Phone BLE app -> AIDER_FarmMonitor UART-style service

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 on the sensor module.

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 the BME280 Sensor Module

Place the BME280 module where it can sense the farm monitor air conditions.

Identify VCC, GND, SDA, and SCL before wiring.

Step 5: Connect the BME280 Sensor Module

Connect BME280 VCC to 3.3V.

Connect BME280 GND to GND.

Connect BME280 SDA to GPIO 2 and BME280 SCL to GPIO 3.

If you are using a bare BME280 sensor, use the module pins labeled VCC, GND, SDA, and SCL.

Step 6: Load the BLE Helper Files

Upload ble_uart.py, ble_advertising.py, and bme280.py to the Pico root folder before running the main code.

In the Thonny Shell, use os.listdir() to confirm the helper files are on the Pico.


Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] Soil sensor AOUT / AO / Signal connects to GPIO 26
  • [x] Soil sensor VCC connects to 3.3V
  • [x] Soil sensor GND connects to GND
  • [x] BME280 SDA connects to GPIO 2
  • [x] BME280 SCL connects to GPIO 3
  • [x] BME280 VCC connects to 3.3V
  • [x] BME280 GND connects to GND
  • [x] BLE helper files and bme280.py are saved on the Pico
  • [x] No loose jumper wires

Intermediate Note

This project uses BLE on the Raspberry Pi Pico 2W. Use a BLE app such as nRF Connect, LightBlue, or a BLE UART terminal to connect to AIDER_FarmMonitor. Do not use normal phone Bluetooth pairing. Test dry and wet soil readings before streaming data, and allow BME280 readings time to settle.

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 the sensor wiring first and confirm that both sensors can be read locally.
  • Do not start BLE troubleshooting until the local readings are believable.

Test the input sensors

  • Capture dry and wet references for the soil sensor.
  • Run a short BME280 test and confirm that temperature and humidity values appear.
  • If readings look wrong, recalibrate the soil sensor and check the BME280 I2C wiring.

Test communication

  • Upload the final code and scan for AIDER_FarmMonitor in the BLE app.
  • Connect to the BLE UART-style service and watch the live data stream.
  • Send status, stream off, and stream on to verify the command set.

Run the full system

  • Move the soil sensor between drier and wetter samples and observe the wireless updates.
  • Check that the BME280 values also change when the environment changes.
  • Confirm that streaming can be turned off and back on from the phone.

Save the project

  • Save the final code.
  • Record the BLE device name and report interval.
  • Write down which BLE commands were available in your final version.

Additional Testing and Calibration Checks

  • Use a BLE app, not the normal Bluetooth settings page.
  • Test the soil and BME280 sensors locally before troubleshooting BLE.
  • If streaming stops unexpectedly, resend stream on and watch the Shell for errors.
  • If the BME280 read rate is too fast, lengthen the report interval.

Quick testing checklist

  • [ ] AIDER_FarmMonitor appears in the BLE app scan
  • [ ] The app connects to the BLE UART-style service
  • [ ] Live data streams to the phone
  • [ ] status returns a fresh measurement
  • [ ] stream on and stream off change the reporting behaviour

Full Project Code

from machine import ADC, I2C, Pin
import bluetooth
import bme280
import time
from ble_uart import BLEUART

DEVICE_NAME = "AIDER_FarmMonitor"
REPORT_INTERVAL_S = 5

soil = ADC(26)
i2c_bme = I2C(1, sda=Pin(2), scl=Pin(3), freq=400000)
bme = bme280.BME280(i2c=i2c_bme)

soil_dry = 52000
soil_wet = 22000
command_queue = []
stream_enabled = True
last_report = 0

ble = bluetooth.BLE()
ble.active(True)
uart = BLEUART(ble, name=DEVICE_NAME)


def clamp(value, low, high):
    if value < low:
        return low
    if value > high:
        return high
    return value


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)


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 climate_values():
    temp_c, humidity_pct, pressure_hpa = read_bme280()
    return temp_c, humidity_pct


def send_line(message):
    print(message)
    uart.write((message + "\n").encode())


def on_rx(data):
    command = data.decode("utf-8").strip().lower()
    if command:
        command_queue.append(command)


def send_sensor_report(prefix=""):
    moisture = soil_percent()
    temp_c, humidity_pct = climate_values()
    send_line("{}Soil:{}% Temp:{}C Hum:{}%".format(prefix, moisture, temp_c, humidity_pct))


uart.on_rx(on_rx)
send_line("Bluetooth-based farm monitor ready")

while True:
    now = time.time()

    if stream_enabled and (now - last_report) >= REPORT_INTERVAL_S:
        try:
            send_sensor_report("")
        except Exception as error:
            send_line("Sensor error: {}".format(error))
        last_report = now

    if command_queue:
        command = command_queue.pop(0)
        if command == "status":
            try:
                send_sensor_report("Status ")
            except Exception as error:
                send_line("Sensor error: {}".format(error))
        elif command == "stream on":
            stream_enabled = True
            send_line("Streaming enabled")
        elif command == "stream off":
            stream_enabled = False
            send_line("Streaming disabled")
        elif command == "help":
            send_line("Commands: status, stream on, stream off, help")
        else:
            send_line("Unknown command")

    time.sleep(0.2)

How the Code Works

Code Section What It Does Why It Matters
BLEUART setup Creates the BLE UART service named AIDER_FarmMonitor This is the main communication path of the project
soil_percent() Converts the ADC reading into a soil moisture percentage Makes the moisture reading easier to compare and tune
read_bme280() Reads temperature, humidity, and pressure from the BME280 Adds environmental context to the farm report
stream_enabled Controls whether the monitor sends periodic reports Students can compare streaming and on-demand behaviour
command_queue Stores received BLE commands until the main loop can process them Queued commands are easier to parse and debug
status command Sends one fresh measurement on request On-demand reporting is useful when continuous streaming is disabled
stream on / stream off commands Enables or disables automatic reports Gives the user control over BLE traffic and battery use

Expected Result

The phone should find AIDER_FarmMonitor in a BLE app, connect to the BLE UART-style service, and receive live soil, temperature, and humidity readings.

Commands such as status, stream off, and stream on should change the reporting behaviour as described.


Troubleshooting

Problem Possible cause Solution
The phone cannot find the device ble_uart.py or ble_advertising.py is missing, or the Pico is not advertising Confirm both helper files are on the Pico and restart the board.
The phone tries to pair but nothing works The normal phone Bluetooth settings page is being used Open a BLE app and connect to the BLE UART-style service instead of pairing in settings.
The soil data looks wrong The soil sensor is not calibrated or the ADC reading is unstable Recalibrate soil_dry and soil_wet, then test the sensor in dry and wet soil.
The BME280 values are missing The BME280 is not connected, bme280.py is missing, or the I2C address is wrong Check SDA/SCL wiring, confirm bme280.py is on the Pico, and test the BME280 locally.
Commands do nothing The command text does not match the supported commands Send the exact commands shown in the help response.
Streaming stops unexpectedly The BLE connection dropped or the app is disconnected Reconnect to AIDER_FarmMonitor in the BLE app and send stream on.