from machine import Pin
import utime
# Define LCD pins
lcd_rs = Pin(12, Pin.OUT) # Register Select pin
lcd_e = Pin(11, Pin.OUT) # Enable pin
lcd_d4 = Pin(10, Pin.OUT) # Data pin 4
lcd_d5 = Pin(9, Pin.OUT) # Data pin 5
lcd_d6 = Pin(8, Pin.OUT) # Data pin 6
lcd_d7 = Pin(7, Pin.OUT) # Data pin 7
# LCD control functions
def lcd_send_nibble(data):
lcd_d4.value((data >> 0) & 1)
lcd_d5.value((data >> 1) & 1)
lcd_d6.value((data >> 2) & 1)
lcd_d7.value((data >> 3) & 1)
lcd_e.value(1)
utime.sleep_us(1)
lcd_e.value(0)
utime.sleep_us(100)
def lcd_send_byte(data, rs):
lcd_rs.value(rs)
lcd_send_nibble(data >> 4)
lcd_send_nibble(data & 0x0F)
utime.sleep_us(100)
def lcd_init():
lcd_send_nibble(0x03)
lcd_send_nibble(0x03)
lcd_send_nibble(0x03)
lcd_send_nibble(0x02)
lcd_send_byte(0x28, 0) # Function set: 4-bit mode, 2 lines
lcd_send_byte(0x08, 0) # Display off
lcd_send_byte(0x01, 0) # Clear display
utime.sleep_ms(2)
lcd_send_byte(0x06, 0) # Entry mode set
lcd_send_byte(0x0C, 0) # Display on, cursor off
def lcd_write_string(string):
for char in string:
lcd_send_byte(ord(char), 1)
# Initialize the LCD
lcd_init()
# Get user input (name)
name = input("Enter your name: ")
year = int(input("Enter the current year (e.g., 2024): "))
month = int(input("Enter the current month (1-12): "))
day = int(input("Enter the current day (1-31): "))
hour = int(input("Enter the current hour (0-23): "))
minute = int(input("Enter the current minute (0-59): "))
second = int(input("Enter the current second (0-59): "))
date=str(day)+"/"+str(month)+"/"+str(year)
print(date)
# Display the simulated clock
while True:
# Format the time string
time_str = "{:02}:{:02}:{:02}".format(hour, minute, second)
date_str = date # You can set a fixed date or modify this
# Clear the display
lcd_send_byte(0x01, 0)
utime.sleep_ms(2)
# Display the current time and name on the first line
lcd_write_string(time_str + " " + name)
lcd_send_byte(0xC0, 0) # Move to the second line
lcd_write_string(date_str)
# Update time
utime.sleep(1) # Wait for one second
second += 1
if second >= 60:
second = 0
minute += 1
if minute >= 60:
minute = 0
hour += 1
if hour >= 24:
hour = 0