Morse Code Texting


If you're a proper amateur radio enthusiast then you probably know morse code off by heart... But what if you had to fake it and all you could do was text really fast on a numeric keypad? Espruino is here to help!

This code will make a little device that lets you type out your message like a non-T9 text message, and will then convert it into morse code, which it'll beep out of a speaker for you...

You'll Need

Wiring Up


Just copy and paste this into the right-hand window, then click the Send to Espruino button.

// list of letters that each key on the keypad represents
var letters = {"0":" 0","1":" 1","2":"ABC2","3":"DEF3","4":"GHI4","5":"JKL5","6":"MNO6","7":"PQRS7","8":"TUV8","9":"WXYZ9"};
// the morse code for each latter
var 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":"-.--",
  "0":"-----", "1":".---", "2":"..---", "3":"...--", "4":"....-",
  "5":".....", "6":"-....", "7":"--...", "8":"---..", "9":"----."
// where we connected the speaker
var SPEAKER = A1;

// Morse code to output
var morseInProgress = "";
// Output beeps for the morse code
function doMorseCode(str) {
  // if we're already doing something, just append our extra code and return
  if (morseInProgress.length>0) {
    morseInProgress += str+" ";
  // otherwise start beeping...
  morseInProgress = str+" ";
  // do a beep and move onto the next character
  var beeper = function () {
    var time = 0;
    // look at first character to see what beep to use
    if (morseInProgress[0]==".") time = 100; // short beep
    else if (morseInProgress[0]=="-") time = 400; // long beep
    // a character of ' ' just leaves a pause
    if (time>0) {
      // start beeping
      // turn speaker off after a time
      setTimeout(function() {
      }, time);
    // remove the first character
    morseInProgress = morseInProgress.substr(1);
    // if there's anything left, carry on
    if (morseInProgress.length>0)

function finalChar(ch) {
  var code = morse[ch];
  console.log("Chosen '"+ch+"' -> '"+code+"'");
  if (code!==undefined)

var charTimeout; // timeout after a key is pressed
var charCurrent; // current character (index in letters)
var charIndex; // index in letters[charCurrent]

function newCharacter(ch) {
  console.log("newCharacter "+ch);
  // send the old one
  if (charCurrent!==undefined)
  // reset our values
  charCurrent = ch;
  charIndex = 0;

function onKeyPad(key) {
  // remove the timeout if we had one
  if (charTimeout!==undefined) {
    charTimeout = undefined;
  // work out which char was pressed
  var ch = "123A456B789C*0#D"[key];
  if (ch==charCurrent) {
    charIndex = (charIndex+1) % letters[charCurrent].length;
  } else {
  console.log("... '"+letters[charCurrent][charIndex]+"'");
  // set a timeout
  charTimeout = setTimeout(function() {
    charTimeout = undefined;
  }, 1500);

require("KeyPad").connect([B2,B3,B4,B5],[B6,B7,B8,B9], onKeyPad);

Now, press the number keys on the Key Pad. Just like text-messaging on an old telephone you'll cycle through letters.

If you want to type Espruino, you'd type: 37777 wait 7 wait 7778844466 wait 666.

As a character is selected (1.5 seconds after you finish pressing a button, or when you press a different button), Espruino will beep out the morse code for that letter.

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.