from machine import Pin, PWM, I2C
from time import sleep
from pico_i2c_lcd import I2cLcd
import time
# Initialize I2C with GPIO 4 (SDA) and GPIO 5 (SCL) for the Raspberry Pi Pico
i2c = I2C(0, scl=Pin(5), sda=Pin(4), freq=400000) # Default I2C pins (GPIO 5 and GPIO 4)
buzzer = PWM(Pin(28))
buzzer.duty_u16(0)
# Scan for devices on the I2C bus
devices = i2c.scan()
if devices:
I2C_ADDR = devices[0] # Use the first detected device
print(f"I2C address found: {I2C_ADDR}")
else:
print("No I2C devices found!")
while True:
pass # Halt execution if no I2C devices are found
# Initialize the LCD with 2 rows and 16 columns
lcd = I2cLcd(i2c, I2C_ADDR, 2, 16)
# Morse code dictionary for letters and digits
morse = [
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..",
"-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----."
]
# Function to convert text to Morse code
def text_to_morse(text):
result = ""
for ch in text:
if 'a' <= ch <= 'z':
ch = chr(ord(ch) - 32) # Convert to uppercase
if 'A' <= ch <= 'Z':
result += morse[ord(ch) - ord('A')] + " "
elif '0' <= ch <= '9':
result += morse[ord(ch) - ord('0') + 26] + " "
elif ch == ' ':
result += "/"
else:
result += "?" # Unknown characters
return result
# Pin configuration for the keypad
rows = [Pin(0, Pin.OUT), Pin(1, Pin.OUT), Pin(2, Pin.OUT), Pin(3, Pin.OUT)]
cols = [Pin(8, Pin.IN, Pin.PULL_DOWN), Pin(9, Pin.IN, Pin.PULL_DOWN), Pin(6, Pin.IN, Pin.PULL_DOWN), Pin(7, Pin.IN, Pin.PULL_DOWN)]
# Keypad layout
keys = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
# Function to read keypress from the keypad
def read_keypad():
for i, row in enumerate(rows):
row.value(1) # Activate the row
for j, col in enumerate(cols):
if col.value() == 1: # Check if the key is pressed
row.value(0) # Deactivate the row after detection
return keys[i][j]
row.value(0) # Deactivate the row if no key is pressed
return "" # Return empty string if no key is pressed
# Initialize the LCD
lcd.clear()
lcd.putstr("# - Enter Morse code, * - Backspace")
lcd.move_to(0, 1) # Move to the second line of the LCD
text = "" # Initialize the text input
# Main loop to process keypad input and generate Morse code
while True:
key = str(read_keypad()) # Read the key pressed
if key == "#": # Generate Morse code for the entered text
if text == "":
lcd.clear()
lcd.putstr("Invalid input!")
time.sleep(2)
lcd.clear()
lcd.putstr("# - Enter Morse code")
lcd.move_to(0, 1)
else:
morse_code = text_to_morse(text)
lcd.clear()
lcd.putstr(f"Morse: {morse_code[:16]}") # Display first 16 chars of Morse code
time.sleep(2)
lcd.clear()
lcd.putstr("# - Enter Morse code")
lcd.move_to(0, 1)
# Output Morse code with buzzer sound
for i in morse_code:
if i == '.':
buzzer.freq(1000)
buzzer.duty_u16(32768) # Turn on the buzzer
time.sleep(0.3)
buzzer.duty_u16(0) # Turn off the buzzer
time.sleep(0.1)
elif i == '-':
buzzer.freq(1000)
buzzer.duty_u16(32768) # Turn on the buzzer
time.sleep(0.7)
buzzer.duty_u16(0) # Turn off the buzzer
time.sleep(0.1)
elif i == "/" or i == " ":
buzzer.duty_u16(0) # No sound for space
time.sleep(1)
else:
pass
time.sleep(0.3)
elif key == "*": # Backspace functionality
text = text[:-1] # Remove last character
lcd.clear()
lcd.putstr("Text: " + text)
elif key != "": # Add the pressed key to the text
text += key
lcd.clear()
lcd.putstr("Text: " + text)
time.sleep(0.3)