from machine import Pin, SoftI2C, ADC, PWM
from utime import sleep
import dht
import oled_library
button = Pin(12, Pin.IN, Pin.PULL_UP)
PIR = Pin(27, Pin.IN)
ldr = ADC(Pin(34))
ldr.atten(ADC.ATTN_11DB)
pot = ADC(Pin(35))
pot.atten(ADC.ATTN_11DB)
dht_sensor = dht.DHT22(Pin(4))
Green_LED = Pin(2, Pin.OUT)
Yellow_LED = Pin(18, Pin.OUT)
Red_LED = Pin(5, Pin.OUT)
buzzer = PWM(Pin(15))
buzzer.duty(0)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
oled = oled_library.SSD1306_I2C(128, 64, i2c)
oled.fill(0)
oled.text("SMART LEARNING", 0, 20)
oled.text("PANEL READY", 10, 35)
oled.show()
sleep(2)
while True:
try:
Green_LED.off()
Yellow_LED.off()
Red_LED.off()
buzzer.duty(0)
light_val = ldr.read()
pot_val = pot.read()
pir_state = PIR.value()
button_state = button.value()
dht_sensor.measure()
temp = dht_sensor.temperature()
hum = dht_sensor.humidity()
print("LDR:", light_val,
"POT:", pot_val,
"TEMP:", temp,
"HUM:", hum,
"PIR:", pir_state)
oled.fill(0)
oled.text("SMART LEARNING", 0, 0)
if pir_state:
Yellow_LED.on()
oled.text("Motion Detected", 0, 16)
oled.text("Student Present", 0, 28)
else:
oled.text("No Student", 0, 16)
if light_val > 600:
Red_LED.on()
buzzer.freq(2000)
buzzer.duty(600)
oled.text("Too Dark!", 0, 40)
else:
oled.text("Light OK", 0, 40)
if temp > 30:
Red_LED.on()
buzzer.freq(2500)
buzzer.duty(800)
oled.text("Too Hot!", 0, 52)
else:
oled.text("Temp OK", 0, 52)
if pot_val > 3000:
Green_LED.on()
oled.fill(0)
oled.text("Good Job!", 25, 25)
oled.text("High Score", 20, 40)
if button_state == 0:
buzzer.freq(3000)
buzzer.duty(900)
oled.fill(0)
oled.text("Answer", 40, 25)
oled.text("Submitted!", 20, 40)
oled.show()
sleep(0.5)
buzzer.duty(0)
oled.show()
sleep(0.5)
except Exception as e:
oled.fill(0)
oled.text("ERROR", 40, 30)
oled.show()
print("Error:", e)
sleep(1)