from machine import Pin, I2C
import time
from ssd1306 import SSD1306_I2C
pir = Pin(14, Pin.IN)
led = Pin(15, Pin.OUT)
buzzer = Pin(16, Pin.OUT)
# --- Configuración I2C para OLED ---
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
estado_anterior = 0
contador = 0
oled.fill(0)
oled.text("Sistema PIR listo", 0, 0)
oled.text("Esperando...", 0, 16)
oled.show()
print("Sistema iniciado. Esperando detecciones...")
while True:
estado_actual = pir.value()
if estado_actual == 1 and estado_anterior == 0:
contador += 1
print("⚠️ Presencia detectada! Total:", contador)
led.value(1)
buzzer.value(1)
time.sleep(0.5)
led.value(0)
buzzer.value(0)
# Formatear hora manualmente
t = time.localtime()
hora_str = "{:02d}:{:02d}:{:02d}".format(t[3], t[4], t[5])
oled.fill(0)
oled.text("PIR: DETECTADO!", 0, 0)
oled.text("Eventos:", 0, 20)
oled.text(str(contador), 80, 20)
oled.text("Ultimo evento:", 0, 40)
oled.text(hora_str, 0, 52)
oled.show()
elif estado_actual == 0 and estado_anterior == 1:
oled.fill(0)
oled.text("PIR: Listo", 0, 0)
oled.text("Eventos:", 0, 20)
oled.text(str(contador), 80, 20)
oled.show()
estado_anterior = estado_actual
time.sleep(0.05)