print("Hello, ESP32!")
from machine import Pin, I2C, Timer # Import I2C and Timer from machine
from time import sleep
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# Pin setup
pin_LED = Pin(0, Pin.OUT) # LED output pin
pin_PB = Pin(14, Pin.IN) # Button input pin with pull-up
slide_pin = Pin(27, Pin.IN, Pin.PULL_UP) # Slide switch input pin with pull-up
pir_sensor = Pin(26, Pin.IN) # PIR motion sensor input pin
rgb_red = Pin(25, Pin.OUT) # RGB LED Red pin
rgb_green = Pin(33, Pin.OUT) # RGB LED Green pin
rgb_blue = Pin(33, Pin.OUT) # RGB LED Blue pin
dip_switch = Pin(5, Pin.IN, Pin.PULL_UP) # DIP switch input pin
# I2C setup for LCD
I2C_ADDR = 0x27 # Default I2C address for 2004 LCD
i2c = I2C(0, sda=Pin(21), scl=Pin(22), freq=400000)
lcd = I2cLcd(i2c, I2C_ADDR, 4, 20) # 4 rows and 20 columns
# Timer setup
timer = Timer(0)
# Function to display a message based on the slide switch state
def display_message():
if slide_pin.value() == 0: # When the slide switch is ON (connected to GND)
lcd.clear()
lcd.putstr("HI")
else:
lcd.clear() # Clear the display when the slide switch is OFF
# Timer callback for RGB LED (Blue)
def timer_callback(timer):
rgb_red.off()
rgb_green.off()
rgb_blue.on()
# Interrupt handler for DIP switch
def dip_switch_handler(pin):
rgb_red.off()
rgb_blue.off()
rgb_green.on()
# PIR motion sensor handler
def pir_motion_handler():
if pir_sensor.value() == 1: # Motion detected
rgb_green.off()
rgb_blue.off()
rgb_red.on()
# Initialize components
lcd.clear() # Clear LCD at the start
lcd.putstr("Assalamualaikum")
sleep(2)
# Set up interrupt for DIP switch
dip_switch.irq(trigger=Pin.IRQ_FALLING, handler=dip_switch_handler)
# Start timer for RGB LED (Blue)
timer.init(period=1000, mode=Timer.PERIODIC, callback=timer_callback)
while True:
pin_LED.off() # Turn off the LED initially
val_PB = pin_PB.value() # Read the push button state
print(val_PB)
if val_PB == 0: # If the button is pressed
pin_LED.on() # Turn on the LED
display_message() # Update the LCD based on the slide switch state
pir_motion_handler() # Check PIR motion sensor
sleep(0.1) # Small delay to debounce the switch