{"id":385,"date":"2020-03-18T08:58:51","date_gmt":"2020-03-18T08:58:51","guid":{"rendered":"https:\/\/starthardware.org\/en\/?p=385"},"modified":"2020-03-18T08:58:53","modified_gmt":"2020-03-18T08:58:53","slug":"morse-code-with-arduino","status":"publish","type":"post","link":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/","title":{"rendered":"Morse code with Arduino"},"content":{"rendered":"\n<p>If you dive into the world of microcontroller programming one of the first exercises is the flashing of an SOS in Morse code. But what happens then? Morse code with Arduino: In this project you&#8217;ll learn how to enter text via the serial monitor and convert it into blinking and beeping with Arduino.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Components<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li> Arduino UNO*<\/li><li> Piezo speaker* (Tip: You can also easily extract them from old melody greeting cards).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Circuit: Morse code with Arduino<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-circuit-schaltung.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"569\" height=\"548\" src=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-circuit-schaltung.jpg\" alt=\"Morse code with Arduino circuit\" class=\"wp-image-386\" srcset=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-circuit-schaltung.jpg 569w, https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-circuit-schaltung-300x289.jpg 300w\" sizes=\"auto, (max-width: 569px) 100vw, 569px\" \/><\/a><\/figure>\n\n\n\n<p>The circuit consists of an Arduino UNO and a piezo speaker. This is simply connected to digital pin 8 and the GND of the Arduino.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code: Morse code with Arduino<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>String inputString = \"\";        \/\/ stores incoming data\nbool stringComplete = false;    \/\/ is true if the received string is complete\nint ledPin = 13;                \/\/ Output Pin LED\nint speakerPin = 8;             \/\/ Pin of the loudspeaker\n\nstatic String morseLetters&#91;] = {\".-\", \"-...\", \"-.-...\", \"-...\", \"...\", \"...\", \"...\", \".---\", \"-.---\", \".-...\", \"--\", \"-...\", \"---\", \".---\", \".---\", \"--.---\",\n                                \".-.\", \"...\", \"-\", \"..-\", \"...-\", \".--\", \"-..-\", \"-.--\", \"--..\", \"E\"\n                               };\nstatic String morseNumbers&#91;] = {\"-----\", \".----\", \".----\", \"...---\", \"....\" , \"-....\", \"--...\", \"---..\", \"----.\"};\n\n\nvoid setup() {\n  Serial.begin(115200);           \/\/ start serial communication\n  inputString.reserve(200);       \/\/ creates a 200-byte buffer for incoming data\n  pinMode(ledPin, OUTPUT);        \/\/ declares ledPin as output\n}\n\nvoid loop() {\n  if (stringComplete) {           \/\/ if it was received completely\n    Serial.println(inputString);  \/\/ display it in the Serial Monitor\n    morseOutput(inputString);     \/\/ call morseOutput method and pass the string\n    inputString = \"\";             \/\/ Reset string\n    stringComplete = false;       \/\/ Set stringComplete to false until a string has been received completely\n  }\n}\n\nvoid serialEvent() {                    \/\/ is executed automatically after each pass of loop()\n  while (Serial.available()) {          \/\/ as long as serial data are available\n    char inChar = (char)Serial.read();  \/\/ reads the current byte\n    inputString += inChar;              \/\/ add the byte as Char (character) to the string inputString\n\n    if (inChar == '\\n') {               \/\/ if the character is a line break, the reception of the data is completed\n      stringComplete = true;            \/\/ then set stringComplete to true so that the loop method can output the data\n    }\n  }\n}\n\nvoid morseOutput(String myString) {\n  \/\/ current letter is converted to integer (Ascii code), which results in e.g. for the small a 97\n  String currentLetter = \"\";                                          \/\/ stores the current Morse code for a letter between\n  for (int i = 0; i &lt; myString.length(); i++) {                       \/\/ for 0 to length of the string\n    if ((int(myString&#91;i]) >= 97) &amp;&amp; (int(myString&#91;i]) &lt;= 122)) {      \/\/ if letter from a to z\n      currentLetter = morseLetters&#91;int(myString&#91;i]) - 97];            \/\/ currentLetter = position in Morse code array from 97\n    } else if ((int(myString&#91;i]) >= 65) &amp;&amp; (int(myString&#91;i]) &lt;= 90)) {\/\/ if letter from A to Z\n    currentLetter = morseLetters&#91;int(myString&#91;i]) - 65];              \/\/ currentLetter = position in Morse code array from 65\n    } else if ((int(myString&#91;i]) >= 48) &amp;&amp; (int(myString&#91;i]) &lt;= 57)) {\/\/ if number from 0 to 9\n    currentLetter = morseNumbers&#91;int(myString&#91;i]) - 48];              \/\/ currentLetter = position in Morse code number array from 48\n    } else {\n      currentLetter = \" \";                                            \/\/ for all other characters, insert a space\n    }\n\n    Serial.print(currentLetter);                                      \/\/ display the Morse code in the serial monitor\n    Serial.print(\" \");                                                \/\/ Space in Serial Monitor\n\n    for (int j = 0; j &lt; currentLetter.length(); j++) {                \/\/ for 0 to length of current Letter\n    if (int(currentLetter&#91;j]) == 46) {                                \/\/ check if current character is a dot (Ascii-Code 46)\n        digitalWrite(ledPin, HIGH);                                   \/\/ switch LED on\n        tone(speakerPin, 200);                                        \/\/ switch on loudspeaker, pitch 200 Hz\n        delay(50);                                                    \/\/ short pause\n        digitalWrite(ledPin, LOW);                                    \/\/ switch off LED\n        noTone(speakerPin);                                           \/\/ switch off speaker\n        delay(50);                                                    \/\/ short pause\n      } else if (int(currentLetter&#91;j]) == 45) {                       \/\/ check if current character is a bar (Ascii-Code 45)\n        digitalWrite(ledPin, HIGH);                                   \/\/ switch LED on\n        tone(speakerPin, 200);                                        \/\/ switch on loudspeaker, pitch 200 Hz\n        delay(150);                                                   \/\/ longer pause\n        digitalWrite(ledPin, LOW);                                    \/\/ switch off LED\n        noTone(speakerPin);                                           \/\/ switch off speaker\n        delay(150);                                                   \/\/ longer pause\n      } else {\n        delay(150);                                                   \/\/ longer pause, character unknown\n      }\n    }\n    delay(200);                                                       \/\/ Pause between\n  }\n}\n    <\/code><\/pre>\n\n\n\n<p>Translated with www.DeepL.com\/Translator (free version)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you dive into the world of microcontroller programming one of the first exercises is the flashing of an SOS in Morse code. But what happens then? Morse code with Arduino: In this project you&#8217;ll learn how to enter text via the serial monitor and convert it into blinking and beeping with Arduino. Components Arduino&hellip;&nbsp;<a href=\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Morse code with Arduino<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":387,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-385","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino-projects"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Morse code with Arduino - StartHardware - Tutorials for Arduino<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Morse code with Arduino - StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"og:description\" content=\"If you dive into the world of microcontroller programming one of the first exercises is the flashing of an SOS in Morse code. But what happens then? Morse code with Arduino: In this project you&#8217;ll learn how to enter text via the serial monitor and convert it into blinking and beeping with Arduino. Components Arduino&hellip;&nbsp;Read More &raquo;Morse code with Arduino\" \/>\n<meta property=\"og:url\" content=\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/\" \/>\n<meta property=\"og:site_name\" content=\"StartHardware - Tutorials for Arduino\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-18T08:58:51+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-03-18T08:58:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-titel.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stefan Hermann\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/starthardware.org\/en\/#website\",\"url\":\"https:\/\/starthardware.org\/en\/\",\"name\":\"StartHardware - Tutorials for Arduino\",\"description\":\"Arduino, Electronics, Fun\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/starthardware.org\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-titel.jpg\",\"contentUrl\":\"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-titel.jpg\",\"width\":1200,\"height\":675,\"caption\":\"Morse Code with Arduino\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#webpage\",\"url\":\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/\",\"name\":\"Morse code with Arduino - StartHardware - Tutorials for Arduino\",\"isPartOf\":{\"@id\":\"https:\/\/starthardware.org\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#primaryimage\"},\"datePublished\":\"2020-03-18T08:58:51+00:00\",\"dateModified\":\"2020-03-18T08:58:53+00:00\",\"author\":{\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\"},\"breadcrumb\":{\"@id\":\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/starthardware.org\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Morse code with Arduino\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59\",\"name\":\"Stefan Hermann\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/starthardware.org\/en\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g\",\"caption\":\"Stefan Hermann\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Morse code with Arduino - StartHardware - Tutorials for Arduino","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/","og_locale":"en_US","og_type":"article","og_title":"Morse code with Arduino - StartHardware - Tutorials for Arduino","og_description":"If you dive into the world of microcontroller programming one of the first exercises is the flashing of an SOS in Morse code. But what happens then? Morse code with Arduino: In this project you&#8217;ll learn how to enter text via the serial monitor and convert it into blinking and beeping with Arduino. Components Arduino&hellip;&nbsp;Read More &raquo;Morse code with Arduino","og_url":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/","og_site_name":"StartHardware - Tutorials for Arduino","article_published_time":"2020-03-18T08:58:51+00:00","article_modified_time":"2020-03-18T08:58:53+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-titel.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Written by":"Stefan Hermann","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/starthardware.org\/en\/#website","url":"https:\/\/starthardware.org\/en\/","name":"StartHardware - Tutorials for Arduino","description":"Arduino, Electronics, Fun","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/starthardware.org\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#primaryimage","inLanguage":"en-US","url":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-titel.jpg","contentUrl":"https:\/\/starthardware.org\/en\/wp-content\/uploads\/2020\/03\/arduino-morse-code-titel.jpg","width":1200,"height":675,"caption":"Morse Code with Arduino"},{"@type":"WebPage","@id":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#webpage","url":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/","name":"Morse code with Arduino - StartHardware - Tutorials for Arduino","isPartOf":{"@id":"https:\/\/starthardware.org\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#primaryimage"},"datePublished":"2020-03-18T08:58:51+00:00","dateModified":"2020-03-18T08:58:53+00:00","author":{"@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59"},"breadcrumb":{"@id":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/starthardware.org\/en\/morse-code-with-arduino\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/starthardware.org\/en\/morse-code-with-arduino\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/starthardware.org\/en\/"},{"@type":"ListItem","position":2,"name":"Morse code with Arduino"}]},{"@type":"Person","@id":"https:\/\/starthardware.org\/en\/#\/schema\/person\/811b16fabcbfeef4210ea79cf0990a59","name":"Stefan Hermann","image":{"@type":"ImageObject","@id":"https:\/\/starthardware.org\/en\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5b5a74ee1d07024fd1eff9b1f7137108089169010a93afaee907b9325ee579a6?s=96&d=mm&r=g","caption":"Stefan Hermann"}}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/comments?post=385"}],"version-history":[{"count":1,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/385\/revisions"}],"predecessor-version":[{"id":388,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/posts\/385\/revisions\/388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media\/387"}],"wp:attachment":[{"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/media?parent=385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/categories?post=385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/starthardware.org\/en\/wp-json\/wp\/v2\/tags?post=385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}