Tag Archives: led

Overengineered Morse Code with the Arduino

As a first foray into python, arduino, and pyfirmata (after lighting a single LED), I wrote this small python script for converting text into morse code. I first ran it with a single LED on a breadboard, but it was much more fun to throw in a piezo buzzer and get the unmistakeable morse beeps.

See the code and schematics below. As someone with less than a month of on and off again python study, don’t expect anything too ‘pythonic’ and pretty. It works though!

Script requires the pyfirmata module. Be sure to adjust the pin number and port/device location of the arduino in the script.

### Arduino / Raspi Morse Code Generator
### Enter a word, and get morse code translation in console output & LED lights / beeps



import pyfirmata


### MORSE CODE ALPHABET ###
morse = {'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':['-','-','.','-'],
	 ' ':' '}


### CONNECT TO ARDUINO ###
port = '/dev/ttyACM0'
board = pyfirmata.Arduino(port)

### SET PIN NUMBER ###
pin = 7 

def program():

### DELAY FACTORS ###
  delay = 0.15						# Length of time LED between inter elemental lighting
  delay_factor = 1					# Speed up or slow down entire program by set percentage Default: 1
  delay_intraletter = delay * delay_factor * 3		# Length of time between letters 
  delay_interword = delay * delay_factor * 7		# Length of time between words
  delay_dit = delay * delay_factor			# Length of illumination for dot/dit
  delay_dah = delay_intraletter	 


  user_list = [] 									# blank list
  user_input = raw_input("Enter your word to convert to morse code: ").lower()		# Take user input, convert to lowercase
  for x in user_input:
    user_list.extend(morse.get(x))
  print user_list
  print '\n Creating morse message now...'
  user_list = ''.join(user_list)
 # print 'DEBUG: joined user list'
 # print user_list
 
  for l in user_list:
    
    if l == '.':
      board.pass_time(delay_intraletter)
      board.digital[pin].write(1)
      print 'TRANSMITTING: .'
      board.pass_time(delay_dit)
      board.digital[pin].write(0)
    
    elif l == '-':
      board.pass_time(delay_intraletter)
      board.digital[pin].write(1)
      print 'TRANSMITTING: -'
      board.pass_time(delay_dah)
      board.digital[pin].write(0)
      
    elif l == ' ':
      board.pass_time(delay_interword)
      print 'NEW WORD'	

    else:
	pass
      
   
   
program()
  

The circuit is even simpler than the script. It amounts to nothing more than a piezo buzzer (unsure of the specs of mine), LED, and resistor in series.

Easy enough, right?
Easy enough, right?

So what had originally sprung up as an idea to play around with the pyfirmata syntax turned into a drive to learn morse code, which quickly faded into disinterest by the time I had finished transcoding the alphabet. I have since made an updated version that includes numbers and communicates between two computers on a network via the python SOCKS module. I was considering adding a reverse translator as well that translates user input (via pushbutton) morse into letters. I think I have a few other projects in mind that will be of more immediate practical use.

Download the latest source files for this project here

Morse key adopted from https://en.wikipedia.org/wiki/Morse_code.