"""
ENGR 120/121 – Building Management System
Team 4: Emilia Hutchison & Nayan Soma
Raspberry Pi Pico W – MicroPython
PIN ASSIGNMENTS:
GP26 (ADC0) – Potentiometer (simulates temperature)
GP27 (ADC1) – Ambient Light Sensor (analog)
GP28 (ADC2) – IR Phototransistor (sunlight sensor)
GP10 – White LED 1
GP11 – White LED 2
GP12 – White LED 3
GP20 – Red LED (heating)
GP21 – Blue LED (cooling)
"""
import time
from machine import ADC, Pin, PWM
# ── Pin setup ──────────────────────────────────────────────────────────────────
pot = ADC(Pin(26)) # Potentiometer (simulates temperature)
ambient_pin = ADC(Pin(27)) # Ambient light sensor
ir_sensor = ADC(Pin(28)) # IR phototransistor (sunlight)
# White LEDs – PWM for dimming (0 = off, 65535 = full brightness)
led1 = PWM(Pin(10)); led1.freq(1000)
led2 = PWM(Pin(11)); led2.freq(1000)
led3 = PWM(Pin(12)); led3.freq(1000)
led_red = Pin(20, Pin.OUT) # Heating indicator
led_blue = Pin(21, Pin.OUT) # Cooling indicator
# ── Thresholds (tune these after testing with your sensors) ───────────────────
SUNLIGHT_THRESHOLD = 50 # % — above this = sunlight detected
AMBIENT_THRESHOLD = 40
AMBIENT_MAX = 80 # % — below this = back of room is dark
TEMP_TARGET = 21.0 # Target temperature in °C
# ── Potentiometer → temperature mapping ──────────────────────────────────────
def pot_to_celsius(raw):
"""Map potentiometer 0-65535 to a 15-30°C temperature range."""
return round(15.0 + (raw / 65535) * 15.0, 1)
# ── LED control ───────────────────────────────────────────────────────────────
def set_white_leds(level):
"""Turn on 0, 1, 2, or 3 white LEDs."""
d = 65535
led1.duty_u16(d if level >= 1 else 0)
led2.duty_u16(d if level >= 2 else 0)
led3.duty_u16(d if level >= 3 else 0)
# ── Main loop ─────────────────────────────────────────────────────────────────
print("Building Management System starting...")
while True:
# --- Read sensors ---
raw_sunlight = ir_sensor.read_u16()
raw_ambient = ambient_pin.read_u16()
temp = pot_to_celsius(pot.read_u16())
# Scale to 0-100% — both sensors are inverted (high = dark, low = bright)
# Sunlight: max dark=65000, max bright=36000 → invert and scale
sunlight = min(100, max(0, round((65000 - raw_sunlight) / (65000 - 36000) * 100)))
# Ambient: max dark=65000, max bright=55000 → invert and scale
ambient = min(100, max(0, round((65000 - raw_ambient) / (65000 - 59000) * 100)))
# --- Lighting control (auto) ---
if sunlight >= SUNLIGHT_THRESHOLD:
# Sunlight coming in — dim the lights
if ambient < AMBIENT_THRESHOLD:
# Back of room is still dark, use 2 LEDs
set_white_leds(2)
lights = 2
elif ambient < AMBIENT_MAX and ambient > AMBIENT_THRESHOLD:
# Room is bright enough, use just 1 LED
set_white_leds(1)
lights = 1
else:
set_white_leds(0)
lights = 0
else:
# No sunlight — full lights on
set_white_leds(3)
lights = 3
# --- Temperature control ---
if temp < TEMP_TARGET - 0.5:
led_red.on()
led_blue.off()
temp_status = "HEATING"
elif temp > TEMP_TARGET + 0.5:
led_blue.on()
led_red.off()
temp_status = "COOLING"
else:
led_red.off()
led_blue.off()
temp_status = "OK"
# --- Print status to Thonny console ---
print(f"Temp: {temp}°C [{temp_status}] | "
f"Sunlight: {sunlight}% | "
f"Ambient: {ambient}% | "
f"LEDs on: {lights}/3")
time.sleep(1)