from machine import Pin, I2C, PWM
import utime
from lcd_api import LcdApi
from i2c_lcd import I2cLcd
# Initialize LCD (I2C)
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=400000)
lcd = I2cLcd(i2c, 0x27, 2, 16) # 16x2 LCD at I2C address 0x27
# Initialize Buzzer on Pin 15
buzzer = PWM(Pin(15))
buzzer.duty(0) # Buzzer off initially
# Happy Birthday melody: (frequency in Hz, duration in ms)
melody = [
(262, 250), (262, 250), (294, 500), (262, 500), (349, 500), (330, 800),
(262, 250), (262, 250), (294, 500), (262, 500), (392, 500), (349, 800),
(262, 250), (262, 250), (523, 500), (440, 500), (349, 500), (330, 500), (294, 700),
(466, 250), (466, 250), (440, 500), (349, 500), (392, 500), (349, 700)
]
def play_melody():
for note, duration in melody:
if note == 0:
buzzer.duty(0) # Silence
else:
buzzer.freq(note)
buzzer.duty(678) # Around 50% duty (range: 0-1023)
utime.sleep_ms(duration)
buzzer.duty(0)
utime.sleep_ms(50) # Short pause between notes
# Show message on LCD
lcd.clear()
lcd.putstr(" Sannah Helwah 🎉")
lcd.move_to(0, 1)
lcd.putstr(" Sayang >3")
utime.sleep(2)
# Play birthday melody
play_melody()
# Final message
lcd.clear()
lcd.putstr(" May Allah SWT")
lcd.move_to(0, 1)
lcd.putstr(" Bless You 💖")
utime.sleep(5)
# Turn off buzzer
buzzer.duty(0)
buzzer.deinit()