# Import library untuk akses pin, PWM, I2C, waktu, dan LCD
from machine import Pin, PWM, SoftI2C
from time import sleep, ticks_ms, ticks_diff
from lcd_api import LcdApi # Library kontrol LCD
from i2c_lcd import I2cLcd # Library LCD via I2C
# Inisialisasi pin PWM untuk LED RGB
# LED Merah di pin 25, LED Hijau di pin 33, LED Biru di pin 32
pin_red = PWM(Pin(25), freq=19531, duty=0) # LED Merah
pin_green = PWM(Pin(33), freq=19531, duty=0) # LED Hijau
pin_blue = PWM(Pin(32), freq=19531, duty=0) # LED Biru
# Inisialisasi tombol utama pada pin 14 (ganti warna LED)
btn = Pin(14, Pin.IN, Pin.PULL_UP)
# Inisialisasi tombol kedua pada pin 12 (mematikan LED)
btn_off = Pin(12, Pin.IN, Pin.PULL_UP)
# LED bawaan ESP32 (biasanya digunakan sebagai indikator) di pin 2
led = Pin(2, Pin.OUT)
# Inisialisasi I2C untuk LCD (SCL: pin 22, SDA: pin 21)
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=400000)
# Inisialisasi LCD dengan alamat I2C 0x27, ukuran 2 baris x 16 karakter
lcd = I2cLcd(i2c, 0x27, 2, 16)
# Variabel untuk menghitung jumlah klik
click_count = 0
# Waktu debounce (ms) agar tombol tidak terdeteksi ganda
debounce_time = 300
# Waktu terakhir tombol ditekan
last_press = 0
# Fungsi untuk mematikan semua LED RGB
def all_off():
pin_red.duty(0)
pin_green.duty(0)
pin_blue.duty(0)
# Fungsi untuk menampilkan pesan di LCD
def show_lcd_message(count, color_name):
lcd.clear()
lcd.putstr("Klik: {}\nWarna: {}".format(count, color_name))
# Loop utama program
while True:
# Tombol untuk mematikan semua LED (GPIO 12)
if btn_off.value() == 0 and ticks_diff(ticks_ms(), last_press) > debounce_time:
last_press = ticks_ms()
click_count = 0
all_off()
lcd.clear()
lcd.putstr("Tombol OFF\nLampu Dimatikan")
led.on()
sleep(0.2)
led.off()
while btn_off.value() == 0:
sleep(0.1)
# Tombol utama (GPIO 14) untuk siklus warna LED
if btn.value() == 0 and ticks_diff(ticks_ms(), last_press) > debounce_time:
last_press = ticks_ms()
click_count += 1
if click_count > 3:
click_count = 1
all_off()
if click_count == 1:
pin_red.duty(1023)
show_lcd_message(1, "Merah")
elif click_count == 2:
pin_green.duty(1023)
show_lcd_message(2, "Hijau")
elif click_count == 3:
pin_blue.duty(1023)
show_lcd_message(3, "Biru")
led.on()
sleep(0.2)
led.off()
while btn.value() == 0:
sleep(0.1)
sleep(0.1)Loading
esp32-devkit-c-v4
esp32-devkit-c-v4