[Swlug] Brag time - Morse code sending on a Raspberry Pi Pico

Rhys Sage rhys_sage at yahoo.com
Sat Apr 16 18:24:23 UTC 2022


It could do with some tidying and inclusion of Morse number & symbols  but this code seems to work on the Pi Pico for transmitting painfully slow Morse code via the onboard LED. I just felt like bragging about a Saturday morning's playing around.


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 16 11:02:05 2022
@author: mary
"""
from machine import Pin, Timer
import utime
led = Pin(25, Pin.OUT)
timer = Timer()
led.value(1)
utime.sleep(1)
led.value(0)
def SendLetter(Letter):
    led.value(0)
    MaxLength = len(Letter)
    for MorseCounter in range (0,MaxLength):
        if Letter[MorseCounter] == '-':
            led.value(1)
            utime.sleep(1.5)
            led.value(0)
        else:
            led.value(1)
            utime.sleep(0.5)
            led.value(0)    
    utime.sleep(1.5)
#Morse Code
Alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
            'q','r','s','t','u','v','w','x','y','z']
Morse = ['.-','-...','-.-.','-..','.','..-.','--.','--.','..','.---','-.-',
         '.-..','--','-.','---','.--.', 
         '--.-','.-.','..','-','..-','...-','.--','-..-','-.--','--..']
TimeUnit = 1 #in seconds)
MessageToSend = 'monty python'
MessageToSend = MessageToSend.lower()
MaxNum = len(MessageToSend)
for MessageCounter in range (0,MaxNum):
    if MessageToSend[MessageCounter] != ' ':
        SendLetter(Morse[MessageCounter])
        print (MessageToSend[MessageCounter])
        print (Morse[Alphabet.index(MessageToSend[MessageCounter])])
    else:
        print ('space')
        utime.sleep(3.5)




Rhys Sage



More information about the Swlug mailing list