print("Hello, ESP32!")
#####################################
###### IMPORT LIBRARIES
#####################################
from machine import Pin, ADC, I2C, Timer
from utime import sleep, sleep_ms
from i2c_lcd import I2cLcd
from ssd1306 import SSD1306_I2C
#####################################
###### PIN CONFIGURATIONS
#####################################
# Digital Outputs
led_pin = Pin(18, Pin.OUT) # For DIP switch
rgb_red = Pin(0, Pin.OUT)
rgb_green = Pin(2, Pin.OUT)
rgb_blue = Pin(15, Pin.OUT)
# Digital Inputs
push_btn = Pin(14, Pin.IN, Pin.PULL_DOWN)
slide_switch = Pin(12, Pin.IN, Pin.PULL_DOWN)
dip_switch = Pin(19, Pin.IN, Pin.PULL_DOWN)
pir_sensor = Pin(32, Pin.IN)
# Analog Input
pot = ADC(Pin(13))
pot.atten(ADC.ATTN_11DB) # Allow full range of 0-3.3V
# I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16)
oled = SSD1306_I2C(128, 64, i2c)
# Timer
timer = Timer(0)
#####################################
###### SUBROUTINES
#####################################
def led_on():
led_pin.on()
def led_off():
led_pin.off()
def rgb_off():
rgb_red.value(1)
rgb_green.value(1)
rgb_blue.value(1)
def display_motion():
lcd.clear()
lcd.move_to(0, 0)
lcd.putstr("Motion Detected")
def display_pot(val):
oled.fill(0)
oled.text("Pot: {}".format(val), 0, 0)
oled.show()
def pushbutton_handler(pin):
rgb_red.value(0)
sleep(0.5)
rgb_red.value(1)
#####################################
###### INTERRUPT CONFIGURATION
#####################################
push_btn.irq(trigger=Pin.IRQ_RISING, handler=pushbutton_handler)
#####################################
###### TIMER FUNCTION
#####################################
def timer_callback(t):
rgb_blue.value(0)
sleep(1)
rgb_blue.value(1)
# Start the timer
# Calls every 2 seconds
# Note: Wokwi doesn't simulate Timer accurately; use sleep if needed
timer.init(period=2000, mode=Timer.PERIODIC, callback=timer_callback)
#####################################
###### MAIN LOOP
#####################################
while True:
# DIP switch 1 controls LED
if dip_switch.value():
led_on()
else:
led_off()
# Slide switch controls GREEN RGB
if slide_switch.value():
rgb_green.value(0)
rgb_red.value(1)
else:
rgb_green.value(1)
# PIR Motion Sensor to LCD
if pir_sensor.value():
display_motion()
# Potentiometer value to OLED
pot_value = pot.read()
display_pot(pot_value)
# Timer now handles RGB BLUE blinking — removed manual sleep here