Arduino with Python - Morse Code Blinking LED

pyfirmata
is a package that can make a connection between Arduino and Python, this post is a brief tutorial on using pyfirmata
and create an interesting small project.
Installing and Loading Firmata
Install Script
pip install pyfirmata
Before we can code with Python, we need to load a format framework to the Arduino.
We need to select File - Instance - Firmata - StandardFirmata in the Arduino IDE with Arduino plugged in. Then we press upload in the interface of IDE.
That is, then we can code with Python.
Coding with Python
Either an IDLE command line or a Text Editor is ok, after loading StandardFirmata
, Arduino IDE is not used. Writing a code that declares an instance of pyfirmata
Class. Notice that 'com3'
is the port of the Arduino device, you can get it from Arduino IDE Menu: Tools - Port.
board = pyfirmata.Arduino('com3')
Then we can modify the sensors which are plugged into pins. At this moment, I use a simple LED.
board.digital[13].write(1) # Open LED in pin=13
Create something fun
I want to let the LED blink in morse code of some information, so what we should do first is to get the basic morse code of [a-z]
, easily can be found at the online morse code decoder.
# This means A - Z
.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..
Then, let's declare some parameters.
pin = 13 # LED pin
port = 'com3' # Arduino Port
b_long = 0.6 # Long Blink duration
b_short = 0.2 # Short Blink duration
w_wait = 1 # Wait duration between words
a_wait = 0.6 # Wait duration between alphas
debug = False
Internal data and functions declaration.
m = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."
morse = m.split(" ")
alpha = 'abcdefghijklmnopqrstuvwxyz'
w_wait = w_wait - a_wait
def long_blink():
board.digital[pin].write(1)
if debug: print("[LOG] Long")
time.sleep(b_long)
board.digital[pin].write(0)
time.sleep(a_wait)
def short_blink():
board.digital[pin].write(1)
if debug: print("[LOG] Short")
time.sleep(b_short)
board.digital[pin].write(0)
time.sleep(a_wait)
Here is the complete code. The program will prompt you to input a word and turn it into Morse Code.
import pyfirmata
import time
pin = 13
port = 'com3'
b_long = 0.6
b_short = 0.2
w_wait = 1
a_wait = 0.6
debug = False
m = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.."
morse = m.split(" ")
alpha = 'abcdefghijklmnopqrstuvwxyz'
w_wait = w_wait - a_wait
def long_blink():
board.digital[pin].write(1)
if debug: print("[LOG] Long")
time.sleep(b_long)
board.digital[pin].write(0)
time.sleep(a_wait)
def short_blink():
board.digital[pin].write(1)
if debug: print("[LOG] Short")
time.sleep(b_short)
board.digital[pin].write(0)
time.sleep(a_wait)
if __name__ == "__main__":
# board.digital[pin].write(0)
print("Initialization...")
board = pyfirmata.Arduino(port)
word = input("Input word: ")
for s in word:
dig = morse[alpha.find(s)]
print(s)
for d in dig:
if d == '.':
short_blink()
if d == '-':
long_blink()
time.sleep(w_wait)