Making a Morse code Keyboard

Nishant Joshi
5 min readMar 28, 2020

Once upon a time….

Well, who cares, this is a simple project I made using an Arduino Uno. I was thinking about making a keyboard when I started but making a Morse code interpreter felt much more exciting. So here’s how you can make the same for yourself.

Before You read this is a disclaimer that the prototype that I have made below is only compatible with any Linux Distribution and not Windows.

What you need

  • As I said before, I made it using an Arduino Uno, we can use any Arduino board at your disposal, given you know how to configure it.
Arduino Uno R3
  • Then here I am using a line tracking sensor as it uses IR it is perfect as a proximity sensor.
Line Tracking IR sensor
  • In the end, you will need a Linux computer and we are good to go.
  • It will be good to have a few coding skills regarding python and a little bit of C just to understand the Arduino code.

Let’ start building

Hardware Connections

For starters, the circuit design for the project should look more or less like the one given below.

The circuit design

Now, this might look confusing but don’t worry I am going to explain each component and I am going to answer the questions you may have.

where is the IR sensor?
Well sorry for the confusion, this is a diagram I build on the TinkerCAD.
It so happens they don’t have anything that closely resembles an IR proximity sensor. So, this is a similar prototype which uses a push button instead of a IR sensor, If you have such a sensor just connect the +Vcc to the red wire, GND to the black wire and the output to the green wire. of the switch

What is the big black thing ?
That is a piezo-electric buzzer, i have used it just to get the feel of operating the real morse code machine, and it also to get a feedback of how the time interval of the keystroke.

Once all the connections are done, Just hook up the Arduino to your PC and let’s start coding.

Software coding

Well now, this is the easy part, coding is always fun because once a great man said you are what you write.
Let’s start with writing the Arduino code and pushing it to the board.

In the code above the two variables represents the PIN on the Arduino board. Then the statvariable is for storing the output of the sensor temporarily. The function calls in the setup function are for making the pins available for usage as input and output.
In the loop function digitalRead reads the current value of the sensor. This loop function is called on repeat, thus using the delay function the call is delayed by n milliseconds, in our case 10. The if block is self-explanator.

Now, we just upload this sketch into the Arduino and we have a steady stream of bits representing the state of the sensor.

Now, we need to create an interpreter on our computer so that we can properly interpret the bitstream. As I have written the program in python3, we need to install an essential package from pip. But there is a catch as we are interpreting the raw bits, we need extended permission to do it, so we need to run our program as a superuser.

  • First, we install the serial interpreter in python
$ sudo pip3 install pyserial
  • Now, we write the program that is going to do the real magic

Now, when you are going to run this code remember that we have to give it 2 command-line arguments:
1. This first argument is the dev file which represents the serial device we can get it through the Arduino IDE when you upload a sketch the device name and the block file name is written there /dev/ttyXXX It might be something like this for me it was /dev/ttyACM and the second argument is the baud rate which is specified in the sketch it differs by the model of the Arduino board, but for Uno, it is 9600. Thus the command for me looks more or less like.

$ sudo python3 seriali.py /dev/ttyACM 9600

Here the file is saved as seriali.py

Now, if you might know python3 you must be worried about the import create_trie and the file data/trie.pkl . Well, these files are created by me for interpreting the morse code and storing them as a trie (It’s a datatype). Now you might think where you can find these files.

The various source code is already written in one of my GitHub repository: github.com/nishantjoshi00

If you just glanced through the code there are a lot of nested if .. else statements, you might be thinking about editing it to your needs, the changes should be made inside the try block and there’s where the magic happens.
There are two variables that you can tweak to your needs, that are short_ and long_ if you have configured them to 20 & 100. They represent the time offset for a short tap and long tap. they represent 20 x 10ms = 200ms and
1000 x 10ms = 1000ms = 1s as we have set the delay to 10ms .
If you want to pipe the entire output to act as a keyboard then you can push the latest character that has been written in the variable sent in Ubuntu, you can do that using the following.

import os
os.system("xdotool key {}".format(value))

This can be added to the part when the cur in added into the sent variable.

If you have checked the repo by now then we might see there is a file named code.txt there is the morse representation of all the characters but there is an extended character set for morse code you could experiment by adding them in the file and then running the command given in the README.md

Now, I must take your leave, if you have reached here and found the blog interesting please check out my GitHub page for other such amazing projects. Thank you so much for reading….

Stay humble and keep reading….

--

--

Nishant Joshi

Developer, Programmer, learner, and curious human being