Project 2.4.3: Greenhouse Temperature Control
Intermediate Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython
Overview
This project builds a greenhouse temperature controller that switches a cooling fan when the air becomes too hot.
Students will build a shared I2C sensor-and-display bus using a BME280 and OLED, then connect a relay-controlled fan output.
The final system should show live climate values, turn the fan on above the high temperature threshold, and turn it off again only after the temperature drops below the low threshold.
Required Components
![]() BME280 module |
![]() External fan power supply |
![]() Raspberry Pi Pico 2 W |
![]() OLED display |
![]() 1-channel relay module |
![]() Small DC fan Motor |
![]() LED and 220 Ω resistor |
![]() Breadboard and jumper wires |
Circuit Connections
| Component terminal | Connect to | Pico physical pin | Notes |
|---|---|---|---|
| BME280 SDA | GPIO 8 | GPIO 8 / physical pin 11 | Shared I2C SDA |
| BME280 SCL | GPIO 9 | GPIO 9 / physical pin 12 | Shared I2C SCL |
| OLED SDA | GPIO 8 | GPIO 8 / physical pin 11 | Shared I2C SDA |
| OLED SCL | GPIO 9 | GPIO 9 / physical pin 12 | Shared I2C SCL |
| Relay IN | GPIO 15 | GPIO 15 / physical pin 20 | Fan control |
| Status LED anode | GPIO 16 through 220 Ω resistor | GPIO 16 / physical pin 21 | Fan indicator |
| Status LED cathode | GND | Any GND pin | LED return |
| Fan supply path | Relay COM to NO contacts | Not a Pico GPIO | External power only |
Wiring Diagram

Raspberry Pi Pico 2 W
┌─────────────────────┐
│ │
│ GPIO 8 ───────────┤──── BME280 SDA & OLED SDA
│ GPIO 9 ───────────┤──── BME280 SCL & OLED SCL
│ GPIO 15 ───────────┤──── Relay IN
│ GPIO 16 ────220Ω───┤──── Status LED (+)
│ │
│ 3.3V ───────────┤──── BME280 VCC & OLED VCC
│ GND ───────────┤──── BME280 GND
│ GND ───────────┤──── OLED GND
│ GND ───────────┤──── Status LED (-)
│ GND ───────────┤──── Relay GND
│ │
└─────────────────────┘
Relay Module External Supply
┌──────────┐ ┌────────────┐
│ COM ────┼────────────┤ (+) │
│ NO ────┼──┐ │ │
│ VCC ────┼──┼─────────┤ (+) (if │
│ GND ────┼──┼──┐ │ needed) │
└──────────┘ │ │ │ │
│ │ └────────────┘
│ │ │
│ └───────────┘
│ Fan (+)
│
└────────────── Fan (-) ──── External Supply (-)
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 and OLED
Place the BME280 and OLED display so their I2C pins are easy to reach. Identify VCC, GND, SDA, and SCL on both modules before wiring.
Step 3: Connect BME280 Power and I2C
Connect BME280 VCC to 3.3V. Connect BME280 GND to GND. Connect BME280 SDA to GPIO 8. Connect BME280 SCL to GPIO 9.
Step 4: Connect OLED Power and I2C
Connect OLED VCC to 3.3V. Connect OLED GND to GND. Connect OLED SDA to the same GPIO 8 I2C bus. Connect OLED SCL to the same GPIO 9 I2C bus.
Step 5: Place and Wire the Relay Module
Identify relay VCC, GND, IN, COM, NO, and NC. Connect relay IN to GPIO 15. Power the relay module from the supply required by the module label. Connect relay GND to Pico GND and to the external fan supply GND where shared grounding is required.
Step 6: Place the Status LED
Place the status LED on the breadboard. Connect its long leg through a 220Ω resistor to GPIO 16. Connect its short leg to GND.
Step 7: Wire the Fan Through the Relay
Connect the external fan supply positive wire to relay COM. Connect relay NO to the fan positive lead. Connect the fan negative lead to the external fan supply negative wire.
Step 8: Check the Fan
Make sure the fan blades can spin freely and no wire can touch the blades.
Wiring Check
- [x] Pico 2W is placed correctly across the breadboard center gap
- [x] BME280 VCC and OLED VCC connect to 3.3V
- [x] BME280 GND and OLED GND connect to GND
- [x] BME280 and OLED SDA share GPIO 8
- [x] BME280 and OLED SCL share GPIO 9
- [x] Relay IN connects to GPIO 15
- [x] Status LED long leg connects through a 220Ω resistor to GPIO 16
- [x] Fan positive path uses relay COM and NO
- [x] Fan negative connects to external fan supply negative
- [x] Fan can spin freely
- [x] No loose jumper wires
Intermediate Note Both I2C devices must share the same SDA and SCL bus. Run an I2C scanner before the full code to confirm that the BME280 and OLED are detected.
Safety Note Do not power the fan from the Pico. Use an external supply that matches the fan, keep fingers and wires away from moving blades, and power I2C modules from 3.3V unless their boards are documented as 3.3V-safe at another voltage.
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 I2C bus first so the BME280 and OLED can be tested together
- Keep the SDA and SCL wires short and do not cross them
Test the Input Sensor
- Run
i2c.scan()and confirm that the BME280 and OLED addresses appear on the bus - Run a short BME280 test and check that temperature and humidity values look realistic
Test the Output Device
- Test the relay and fan indicator LED before connecting the real fan
- Confirm that the relay does not stay energised at boot
Run the Full System
- Upload the full code and confirm that the OLED shows temperature, humidity, pressure, and fan state
- Temporarily lower the high threshold or warm the sensor slightly with your hand to trigger the fan safely
- Confirm that the fan only turns off after the temperature drops below the low threshold
Save the Project
- Save the working code and record the high and low temperature thresholds you chose
- Note the I2C addresses shown by
i2c.scan()for future reference
Additional Testing and Calibration Checks
Calibration and tuning notes
- Use
i2c.scan()before troubleshooting the code so you know the hardware bus is working - If the OLED stays blank, confirm its address and the SDA/SCL wiring
- For safe classroom testing, lower the high threshold temporarily instead of overheating the space
- If the relay chatters near the threshold, widen the gap between
temp_highandtemp_low
Quick testing checklist
- [ ] Both I2C devices appear in
i2c.scan() - [ ] BME280 values look realistic
- [ ] OLED displays the current readings clearly
- [ ] Relay and status LED switch on when temperature is above the high threshold
- [ ] Fan turns off only after temperature drops below the low threshold
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 I2C, Pin
import time
import BME280
from ssd1306 import SSD1306_I2C
RELAY_ON = 0
RELAY_OFF = 1
i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=100000)
bme = BME280.BME280(i2c=i2c)
oled = SSD1306_I2C(128, 64, i2c)
fan_relay = Pin(15, Pin.OUT)
status_led = Pin(16, Pin.OUT)
temp_high = 30.0
temp_low = 27.0
fan_running = False
fan_relay.value(RELAY_OFF)
status_led.value(0)
def first_number(text):
digits = ""
for char in text:
if char in "-0123456789.":
digits += char
elif digits:
break
return float(digits)
def read_environment():
temp_text, pressure_text, humidity_text = bme.values
temp_c = first_number(temp_text)
humidity_pct = first_number(humidity_text)
return temp_c, humidity_pct, pressure_text
def update_display(temp_c, humidity_pct, pressure_text, fan_state):
oled.fill(0)
oled.text("Greenhouse", 18, 0)
oled.text("Temp:{:.1f}C".format(temp_c), 0, 18)
oled.text("Hum :{:.1f}%".format(humidity_pct), 0, 30)
oled.text("Fan :{}".format("ON" if fan_state else "OFF"), 0, 42)
oled.text(pressure_text[:16], 0, 54)
oled.show()
print("Greenhouse temperature control ready")
while True:
temp_c, humidity_pct, pressure_text = read_environment()
if temp_c >= temp_high and not fan_running:
fan_relay.value(RELAY_ON)
status_led.value(1)
fan_running = True
print("Fan ON: {:.1f}C is above the high threshold".format(temp_c))
elif temp_c <= temp_low and fan_running:
fan_relay.value(RELAY_OFF)
status_led.value(0)
fan_running = False
print("Fan OFF: {:.1f}C is below the low threshold".format(temp_c))
print("Temp:{:.1f}C Hum:{:.1f}% Fan:{}".format(
temp_c,
humidity_pct,
"ON" if fan_running else "OFF",
))
update_display(temp_c, humidity_pct, pressure_text, fan_running)
time.sleep(3)
How the Code Works
| Code Section | What It Does | Why It Matters |
|---|---|---|
| Shared I2C bus | Lets the BME280 and OLED use the same SDA and SCL pins | Students learn multi-device bus wiring rather than one-device-only wiring |
first_number() |
Extracts numeric values from the BME280 text strings | MicroPython sensor libraries often return formatted strings that must be parsed |
temp_high and temp_low |
Create hysteresis for the fan control | A single threshold would cause faster relay chatter near the set point |
update_display() |
Draws the climate data and fan state on the OLED | Local feedback makes the system easier to test without a computer |
Expected Result
The OLED should show live greenhouse readings. When the temperature rises above the high threshold, the relay should switch the fan on and the status LED should light. The fan should stay on until the temperature drops below the low threshold.
Troubleshooting
| Problem | Possible cause | Solution |
|---|---|---|
| The OLED is blank | Wrong I2C wiring or the display address is not on the bus | Run i2c.scan(), confirm power, and recheck SDA/SCL |
| The sensor values do not change | The BME280 is not responding correctly | Check the power, I2C bus, and the BME280.py file on the Pico |
| The fan runs at boot | Relay logic polarity is reversed | Swap RELAY_ON and RELAY_OFF or confirm the relay module type |
| The fan toggles too often | The high and low thresholds are too close together | Increase the hysteresis gap between temp_high and temp_low |







