Skip to content

Project 1.8.5: Online Humidity Monitor

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

Pico 2 W Diagram

Pico 2 W Pinout


Overview

Build a humidity monitor that shows live BME280 humidity readings on a web page.

This project demonstrates combining sensor reading, simple decision logic, and a browser display.

The final result should show the humidity percentage and a comfort status such as Dry, Comfortable, or Humid.

Required Components

Raspberry Pi Pico 2 W
Raspberry Pi Pico 2 W
BME280 module
BME280 module
Breadboard
Breadboard
Jumper wires
Jumper wires

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
BME280 VCC 3.3V Physical pin 36
BME280 GND GND Physical pin 38
BME280 SDA GPIO 8 GPIO 8 / physical pin 11 I2C0 SDA
BME280 SCL GPIO 9 GPIO 9 / physical pin 12 I2C0 SCL

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2 W

Step 1

Step 2: Place the BME280 Module

Step 2

Step 3: Connect BME280 Power

Connect BME280 VCC to 3.3V and GND to GND.

Step 3

Step 4: Connect BME280 I2C Pins

Connect SDA to GPIO 8 and SCL to GPIO 9.

Step 4


Testing Individual Components

I2C Scanner Test

from machine import Pin, I2C

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
print([hex(addr) for addr in i2c.scan()])

BME280 Humidity Test

from machine import Pin, I2C
import BME280

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
try:
    bme = BME280.BME280(i2c=i2c, address=0x76)
except OSError:
    bme = BME280.BME280(i2c=i2c, address=0x77)

print('Humidity:', bme.humidity)

Full Project Code

import network
import socket
import time
from machine import Pin, I2C
import BME280

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

i2c = I2C(0, sda=Pin(8), scl=Pin(9), freq=400000)
try:
    bme = BME280.BME280(i2c=i2c, address=0x76)
except OSError:
    bme = BME280.BME280(i2c=i2c, address=0x77)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')
for _ in range(15):
    if wlan.isconnected():
        break
    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]
print('Connected. Open http://{} in your browser'.format(ip_address))

def humidity_value():
    humidity_text = str(bme.humidity).replace('%', '')
    return float(humidity_text)

def humidity_status(value):
    if value < 30:
        return 'Dry'
    elif value <= 60:
        return 'Comfortable'
    return 'Humid'

def web_page():
    value = round(humidity_value(), 1)
    status = humidity_status(value)
    html = """<!DOCTYPE html>
<html>
<head>
    <meta name='viewport' content='width=device-width, initial-scale=1'>
    <meta http-equiv='refresh' content='5'>
    <title>Online Humidity Monitor</title>
</head>
<body style='font-family:Arial;text-align:center;padding:40px'>
    <h1>Online Humidity Monitor</h1>
    <h2>HUMIDITY_TEXT</h2>
    <p>Status: STATUS_TEXT</p>
    <p>Page refreshes every 5 seconds</p>
</body>
</html>"""
    html = html.replace('HUMIDITY_TEXT', str(value) + '%')
    html = html.replace('STATUS_TEXT', status)
    return html

address = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
server = socket.socket()
server.bind(address)
server.listen(1)

while True:
    client, client_address = server.accept()
    print('Client connected from', client_address)
    response = web_page()
    client.recv(1024)
    client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\n'.encode())
    client.sendall(response.encode())
    client.close()

How the Code Works

Code Section What It Does Why It Matters
humidity_value() Converts humidity text into a number Status rules need numeric comparison
humidity_status() Classifies humidity Makes readings beginner-friendly
web_page() Builds the browser page Shows live data in a browser
Auto-refresh Reloads every 5 seconds Students can watch changes

Expected Result

Opening the Pico IP address in a browser shows the current humidity percentage and comfort status.


Troubleshooting

Problem Possible Cause Solution
Humidity stays fixed Air conditions are not changing much Breathe gently near the sensor or move it
Status looks wrong Thresholds need adjustment Edit humidity_status()
Wi-Fi does not connect Wrong SSID/password Use correct 2.4 GHz network credentials