Skip to content

Project 2.6.7: Smart Farm Weather Station

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


Overview

This project builds a small farm weather station that combines temperature, humidity, rainfall state, and brightness into one field-style summary.

Students will wire a BME280 climate sensor, a digital rain sensor, and an LDR light sensor, then classify the environment with rule-based weather states.

The final system should print live weather readings and label conditions such as CLEAR, HOT, COLD, RAIN WATCH, or RAINING.

Required Components

BME280 sensor module
BME280 sensor module
Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
Rain sensor module
Rain sensor module
LDR and 10 kΩ resistor
LDR and 10 kΩ resistor
Breadboard and jumper wires
Breadboard and jumper wires

Circuit Connections

Component terminal Connect to Pico physical pin Notes
BME280 sensor module 1 Requires bme280.py on the Pico
Rain sensor DOUT 1 GPIO 5 / physical pin 7 Digital rain-state input
LDR divider node 1 GPIO 26 / physical pin 31 ADC brightness input
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

Circuit Diagram

  Raspberry Pi Pico 2 W
  ┌─────────────────────┐
  │                     │
  │  GPIO 2  ───────────┤──── BME280 SDA
  │  GPIO 3  ───────────┤──── BME280 SCL
  │                     │
  │  GPIO 5  ───────────┤──── Rain Sensor DOUT
  │                     │
  │  GPIO 26 ───────────┤──── LDR + 10kΩ to GND
  │                     │
  │  3.3V    ───────────┤──── BME280 VCC
  │  3.3V    ───────────┤──── LDR (one leg)
  │  3.3V    ───────────┤──── Rain Sensor VCC
  │                     │
  │  GND     ───────────┤──── BME280 GND
  │  GND     ───────────┤──── Rain Sensor GND
  │  GND     ───────────┤──── 10kΩ resistor
  │                     │
  └─────────────────────┘

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

Place the BME280 module where it can sense the air around the weather station. Identify VCC, GND, and DATA before wiring.

Step 3: 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 4: Position the Rain Sensor

Place the rain sensor area where test drops can reach it without dripping onto the Pico or breadboard. Identify VCC, GND, and DOUT before wiring.

Step 5: Connect the Rain Sensor

Connect rain sensor VCC to 3.3V when the module supports 3.3V operation. Connect rain sensor GND to GND. Connect rain sensor DOUT to GPIO 5. Confirm the DOUT signal is safe for 3.3V Pico GPIO before powering the circuit.

Step 6: Build the LDR Divider

Place the LDR and 10kΩ resistor on the breadboard. Connect one LDR leg to 3.3V. Connect the other LDR leg to GPIO 26. Connect one end of the 10kΩ resistor to GPIO 26. Connect the other end of the 10kΩ resistor to GND.

Wiring Check

  • [x] Pico 2W is placed correctly across the breadboard center gap
  • [x] DHT22 DATA connects to GPIO 4
  • [x] Rain sensor DOUT connects to GPIO 5
  • [x] Rain sensor output is confirmed 3.3V-safe before connecting to the Pico
  • [x] LDR divider node connects to GPIO 26
  • [x] 10kΩ resistor connects from GPIO 26 to GND
  • [x] No loose jumper wires

Intermediate Note The BME280 needs a short delay between readings. Test the rain sensor dry and with a few drops, and test the LDR in bright light and shade before trusting the weather state.

Safety Note Keep water drops away from the Pico, breadboard, USB cable, and jumper wires. If the rain sensor board is designed for 5V modules, confirm its digital output is 3.3V-safe before connecting it to GPIO 5.


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 BME280 and LDR circuits first, then add the rain sensor input
  • Keep the rain sensor physically away from the Pico so water testing stays safe

Test the Input Sensors

  • Run a quick BME280 script and confirm that temperature and humidity values appear
  • Cover and uncover the LDR to check brightness changes
  • Wet the rain sensor lightly and confirm that its digital output changes state

Test the Output Data

  • Upload the final code and compare the raw values with the printed state label
  • Temporarily create high-humidity or low-light conditions to test the RAIN WATCH logic

Run the Full System

  • Let the station run for several reading cycles and compare how the state changes when the environment changes
  • Test real rain-sensor activation and check that the state becomes RAINING immediately

Save the Project

  • Save the final code and record the thresholds used for RAIN WATCH, HOT, and COLD states
  • Write down which local condition was easiest to detect reliably

Additional Testing and Calibration Checks

Calibration and tuning notes

  • Test the BME280, LDR, and rain sensor separately before trusting the combined weather state
  • Remember that RAIN WATCH is only a simple rule-based warning, not a meteorological forecast
  • If the rain sensor triggers too easily, adjust its sensitivity module if available or change its physical placement
  • Tune brightness and humidity thresholds to match your real test environment

Quick testing checklist

  • [ ] BME280 readings appear correctly
  • [ ] LDR brightness percentage changes clearly
  • [ ] Rain sensor changes state when wet
  • [ ] Weather state label matches the current conditions
  • [ ] RAIN WATCH and RAINING can both be demonstrated safely

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

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)


rain_sensor = Pin(5, Pin.IN, Pin.PULL_UP)
light_sensor = ADC(26)

temp_min = 10
temp_max = 35
humidity_high = 90
rain_light_threshold = 35


def light_percent():
    raw = light_sensor.read_u16()
    return int((raw / 65535) * 100)


def read_weather():
    temp_c, humidity_pct, pressure_hpa = read_bme280()
    raining = rain_sensor.value() == 0
    brightness = light_percent()
    return temp_c, humidity_pct, raining, brightness


def weather_state(temp_c, humidity_pct, raining, brightness):
    if raining:
        return "RAINING"

    if humidity_pct >= humidity_high and brightness <= rain_light_threshold:
        return "RAIN WATCH"

    if temp_c > temp_max:
        return "HOT"
    if temp_c < temp_min:
        return "COLD"

    return "CLEAR"


print("Smart farm weather station ready")
time.sleep(2)

while True:
    try:
        temp_c, humidity_pct, raining, brightness = read_weather()
    except Exception as error:
        print("Sensor error:", error)
        time.sleep(2)
        continue

    state = weather_state(temp_c, humidity_pct, raining, brightness)
    print("Temp:{}C Hum:{}% Light:{}% Rain:{} State:{}".format(
        temp_c,
        humidity_pct,
        brightness,
        raining,
        state,
    ))
    time.sleep(5)

How the Code Works

Code Section 1 Why It Matters
read_weather() 1 A weather station is useful only when several conditions are available at once
weather_state() 1 Students see how rule-based summarising simplifies monitoring
Rain watch condition 1 This is a heuristic, not a true forecast model
Continuous Shell output 1 Students can troubleshoot classification errors more easily

Expected Result

The weather station should print temperature, humidity, brightness, rain state, and a condition label. When the rain sensor is wet, the state should become RAINING. In high humidity and low light, the system should report RAIN WATCH even before the rain sensor is triggered.


Troubleshooting

Problem 1 Solution
The station always says CLEAR 1 Test humidity, brightness, and rain inputs separately to find which sensor is stuck
RAINING appears all the time 1 Dry the board and confirm whether active-low logic matches your sensor
Brightness never changes 1 Rebuild the divider and check the ADC node at GPIO 26
Humidity is always zero 1 Check wiring, power, and read timing for the BME280 sensor