Skip to content

Instantly share code, notes, and snippets.

@soren121
Last active December 17, 2015 05:58
Show Gist options
  • Save soren121/c0b7dcb905ae45d4249f to your computer and use it in GitHub Desktop.
Save soren121/c0b7dcb905ae45d4249f to your computer and use it in GitHub Desktop.
Raspberry Pi LED Morse code script
#!/usr/bin/python
import RPi.GPIO as gpio
import sys, time
def blink(pin, duration):
gpio.output(pin, gpio.HIGH)
time.sleep(0.4 * duration)
gpio.output(pin, gpio.LOW)
time.sleep(0.4)
morse = {
'a': [1, 3],
'b': [3, 1, 1, 1],
'c': [3, 1, 3, 1],
'd': [3, 1, 1],
'e': [1],
'f': [1, 1, 3, 1],
'g': [3, 3, 1],
'h': [1, 1, 1, 1],
'i': [1, 1],
'j': [1, 3, 3, 3],
'k': [3, 1, 3],
'l': [1, 3, 1, 1],
'm': [3, 3],
'n': [3, 1],
'o': [3, 3, 3],
'p': [1, 3, 3, 1],
'q': [3, 3, 1, 3],
'r': [1, 3, 1],
's': [1, 1, 1],
't': [3],
'u': [1, 1, 3],
'v': [1, 1, 1, 3],
'w': [1, 3, 3],
'x': [3, 1, 1, 3],
'y': [3, 1, 3, 3],
'z': [3, 3, 1, 1],
' ': [4],
}
def main(msg):
gpio.setmode(gpio.BOARD)
gpio.setup(11, gpio.OUT)
try:
for char in msg:
print(char)
if char in morse:
for beep in morse[char]:
blink(11, beep)
time.sleep(0.8)
finally:
gpio.cleanup()
if len(sys.argv) == 2:
main(sys.argv[1])
else:
print("usage: morse.py [input string]")
@soren121
Copy link
Author

Connect an LED to pin 11 (+) and pin 9 (-) with a resistor between 11 and the LED. Run this script as root and pass a message in as the only parameter to see it blinked out in Morse code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment