USB Morse Key Keyboard Input

Funny little project to train your morse code sending capabilities. Let’s connect a morse key to a pc and turn it into a keyboard.
To do this, you’ll need: a Teensy 3.2, a morse key, two jumper cables and 15 minutes of your time.

USB Morse Keyer

First you have to program the Teensy 3.2. To do this you need to download the source code from https://github.com/nomblr/morse/. It’s a project of Nomblr, who posted it on imgur in 2017. It has been in my favorites for more than a year before I finally took the time to play with it.

if you can’t download the code, just copy it from the browser and past it in Arduino.

Connect the Teensy to your PC via USB.
USB Morse Keyer

In Arduino you’ll need the following “board settings”. You can find these in the menu Tools (or “Hulpmiddelen” in Dutch).
Board: “Teensy 3.2 / 3.1”
USB Type: “Keyboard”
Keyboard Layout: “US English”
and select the correct com-port for your Teensy 3.2.

Verify and upload the code to the Teensy.

Connect the black jumper wire to the ground pin of the teensy, and the red to “pin 7”
USB Morse Keyer
For info: “pin 7” is not the seventh pin of the Teensy. First pin is ground, second pin is “pin 0”, third pin is “pin 1”, etc…

Then connect crocodile clip of the black jumper wire to the ring of the morse key, and the red one to the tip.
USB Morse Keyer

When done, you can start keying and watch the letters appear on your screen.

People using a AZERTY keyboard have to switch their PC input settings to QWERTY. If you are using Windows you can do this by pressing “Shift + Alt” (do it again to switch back to AZERTY). This is probably because we choose for the “US English” keyboard layout, but all other keyboard layouts of the Teensy gave the same result.

Although a Teensy is perfectly capable of doing this job, it’s a rather expensive device for this simple task. I’ve ordered myself some “Beetle USB ATMEGA32U4” devices on AliExpress and some Mini buzzers and will provide you with an update when they arrive. The ATMEGA32U4 are much cheaper and the buzzer will give you audible feedback. I’ll reprogram the Teensy and put it back in my Automatic Magnetic Loop Controller 😉

*This blogpost may or may not have been written using this USB Morse Key Keyboard input method.

Because sometimes things get removed from gitHub, I made a copy of the code which you can find below.


// Turns Morse key into USB keyboard

#include // include de-bounce library

const int led = 13; // led is connected to pin 13
const int keyPin = 7; // morse key is connected to pin 7
Bounce morseKey = Bounce(keyPin, 10); // 10 ms debounce

const unsigned long dashThresh = 150; // time threshold in ms to differentiate dots from dashes
const unsigned long letterThresh = 500; // time threshold in ms to differentiate letter gaps
const unsigned long wordThresh = 3000; // time threshold in ms to differentiate word gaps

String inputString = ""; // initialise input string

unsigned long downTime = 0; // records the start time of state change
unsigned long upTime = 0; // records the end time of state change
unsigned long timeNow = 0; // records the current time
unsigned long changeDuration = 0; // records the duration of state change
unsigned long pauseDuration = 0; // records the duration of the last pause

int pauseFlag = 0; // initilise the flag to indicate whether a pause has already been evaluated

void setup()
{
pinMode(led, OUTPUT); // configure the pin connected to the led as an output
pinMode(keyPin, INPUT_PULLUP); // configure the pin connected to the morse key as a pullup
} // end of setup

void loop()
{
checkPause();
// start of IF loop
if (morseKey.update()){

if (morseKey.risingEdge()) { // if input from key has gone to 1 and model is still 0, update model

keyUp();

} else if (morseKey.fallingEdge()) { // if input from key has gone to 0 and model is still 1, update model

keyDown();

}
} // end of if update loop

} // end of loop

void keyDown()
{
downTime = millis();
digitalWrite(led, HIGH); // switch LED on
}

void keyUp()
{
upTime = millis();
changeDuration = upTime-downTime;
digitalWrite(led, LOW); // switch LED off

if (changeDuration>0 and changeDuration=dashThresh) {
inputString = inputString + "-";
Serial.println("DASH");

}

pauseFlag = 1;

}

void checkPause()
{
timeNow = millis();
pauseDuration = timeNow-upTime;

if (pauseDuration>=letterThresh and pauseDuration= wordThresh and pauseFlag) {

evaluateLetter();
newWord();
pauseFlag = 0;

}
}

void newWord()
{
Keyboard.press(KEY_SPACE);
Keyboard.release(KEY_SPACE);
}

void evaluateLetter()
{

if (inputString==".-") {
Keyboard.press(KEY_A);
Keyboard.release(KEY_A);
} else if (inputString=="-..."){
Keyboard.press(KEY_B);
Keyboard.release(KEY_B);
} else if (inputString == "-.-."){
Keyboard.press(KEY_C);
Keyboard.release(KEY_C);
} else if (inputString=="-.."){
Keyboard.press(KEY_D);
Keyboard.release(KEY_D);
} else if (inputString=="."){
Keyboard.press(KEY_E);
Keyboard.release(KEY_E);
} else if (inputString=="..-."){
Keyboard.press(KEY_F);
Keyboard.release(KEY_F);
} else if (inputString=="--."){
Keyboard.press(KEY_G);
Keyboard.release(KEY_G);
} else if (inputString=="...."){
Keyboard.press(KEY_H);
Keyboard.release(KEY_H);
} else if (inputString==".."){
Keyboard.press(KEY_I);
Keyboard.release(KEY_I);
} else if (inputString==".---"){
Keyboard.press(KEY_J);
Keyboard.release(KEY_J);
} else if (inputString=="-.-"){
Keyboard.press(KEY_K);
Keyboard.release(KEY_K);
} else if (inputString==".-.."){
Keyboard.press(KEY_L);
Keyboard.release(KEY_L);
} else if (inputString=="--"){
Keyboard.press(KEY_M);
Keyboard.release(KEY_M);
} else if (inputString=="-."){
Keyboard.press(KEY_N);
Keyboard.release(KEY_N);
} else if (inputString=="---"){
Keyboard.press(KEY_O);
Keyboard.release(KEY_O);
} else if (inputString==".--."){
Keyboard.press(KEY_P);
Keyboard.release(KEY_P);
} else if (inputString=="--.-"){
Keyboard.press(KEY_Q);
Keyboard.release(KEY_Q);
} else if (inputString==".-."){
Keyboard.press(KEY_R);
Keyboard.release(KEY_R);
} else if (inputString=="..."){
Keyboard.press(KEY_S);
Keyboard.release(KEY_S);
} else if (inputString=="-"){
Keyboard.press(KEY_T);
Keyboard.release(KEY_T);
} else if (inputString=="..-"){
Keyboard.press(KEY_U);
Keyboard.release(KEY_U);
} else if (inputString=="...-"){
Keyboard.press(KEY_V);
Keyboard.release(KEY_V);
} else if (inputString==".--"){
Keyboard.press(KEY_W);
Keyboard.release(KEY_W);
} else if (inputString=="-..-"){
Keyboard.press(KEY_X);
Keyboard.release(KEY_X);
} else if (inputString=="-.--"){
Keyboard.press(KEY_Y);
Keyboard.release(KEY_Y);
} else if (inputString=="--.."){
Keyboard.press(KEY_Z);
Keyboard.release(KEY_Z);
} else if (inputString==".----"){
Keyboard.press(KEY_1);
Keyboard.release(KEY_1);
} else if (inputString=="..---"){
Keyboard.press(KEY_2);
Keyboard.release(KEY_2);
} else if (inputString=="...--"){
Keyboard.press(KEY_3);
Keyboard.release(KEY_3);
} else if (inputString=="....-"){
Keyboard.press(KEY_4);
Keyboard.release(KEY_4);
} else if (inputString=="....."){
Keyboard.press(KEY_5);
Keyboard.release(KEY_5);
} else if (inputString=="-...."){
Keyboard.press(KEY_6);
Keyboard.release(KEY_6);
} else if (inputString=="--..."){
Keyboard.press(KEY_7);
Keyboard.release(KEY_7);
} else if (inputString=="---.."){
Keyboard.press(KEY_8);
Keyboard.release(KEY_8);
} else if (inputString=="----."){
Keyboard.press(KEY_9);
Keyboard.release(KEY_9);
} else if (inputString=="-----"){
Keyboard.press(KEY_0);
Keyboard.release(KEY_0);
} else {
Keyboard.press(KEY_MINUS);
Keyboard.release(KEY_MINUS);
}

inputString = ""; // re-initialise inputString ready for new letter

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.