How to Display a String as Morse Code on an LED Using Arduino

In this post, I’ll show you how to develop an application which executes on an Arduino and displays a user typed string, such as “Hello World”, as Morse code on an LED.

Named after Samuel F.B. Morse, the inventor of the telegraph, Morse code is a character encoding scheme that represents letters as long and short pulses of sound or light.

Requirements

The system that is developed must satisfy the following requirements:

  • It must execute on an Arduino. The Arduino version used was version 1.8.8. Most Arduino versions will work.
  • It must display a user typed string, such as “Hello World”, as Morse code on an LED (or several LEDs), or an LCD.
    • A dash is three times as long as a dot.
    • The time between each dot or dash in the same letter is equal to the duration of one dot.
    • The time between two letters is the duration of one dash.
    • The time between two words is the same duration as seven dots.
  • It must use a Round Robin design where a loop:
    • Waits for a string
    • Displays the string in Morse code
    • Exits the loop only if a sentinel is entered (e.g. !)

Hardware Design

The following components are used for the Morse code LED system. You will need:

Here is the schematic:

Software Design

Here are the steps for the Morse code LED system:

  • Step 1. Prompt the user to enter a message on the Serial Monitor. The input for the message contains characters (letters and/or numbers).
  • Step 2. Read each character, one at a time.
  • Step 3. Convert each character into the equivalent Morse Code.
  • Step 4. Convert each Morse code sequence into the corresponding sequence of dots and dashes.
  • Step 5. Transmit the dots and dashes to the LED while transmitting the message to the Serial Monitor.
  • Step 6. Exit the loop if user enters the sentinel value of “!”

Implementation

Here is the source code that you will need to load to your Arduino:

/**
 * In this program, we develop an application which executes on an Arduino 
 * Uno and displays a user typed string, such as “Hello World”, as Morse 
 * code on an LED.   
 * 
 * @version 1.0 2019-01-31
 * @author Addison Sears-Collins
 */
 
// Assign a name to the digital pin 12
unsigned int led_pin = 12;

// Declare an array named letters that holds addresses of string literals 
// (i.e. an array of pointers to strings composed of dots and dashes) 
// Done to preserve memory because strings are not equal in size. A 2D array
// would be a waste of space.
char *letters[] = {
  // The letters A-Z in Morse code  
  ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",    
  ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",  
  "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."          
};

char *numbers[] = {
  // The numbers 0-9 in Morse code  
  "-----", ".----", "..---", "...--", "....-", ".....", "-....", 
  "--...", "---..", "----."
};

unsigned int dot_duration = 200;
bool done = false;

/**
 *  Function runs only once, after each powerup or reset of the Arduino
 */
void setup() {
  // Set the LED to output
  pinMode(led_pin, OUTPUT);
  
  // Open the serial port and set the data transmission rate to 9600 bits 
  // per second. 9600 is the default baud rate for Arduino.
  Serial.begin(9600); 
  
  // Show welcome message as human-readable ASCII text
  Serial.println("MORSE CODE LED PROGRAM");
  Serial.println("This program translates your message into Morse code");
  Serial.println("and flashes it on an LED.");
  Serial.println("Author: Addison Sears-Collins");
  Serial.println("");
  Serial.println("Enter your message(s) or ! to exit: ");
}

/**
 *  Main function that drives the system
 */
void loop() {
  char ch;
  
  // This loop waits for a string, displays it in Morse code, and only exits 
  // the loop if the sentinel is entered. 
  while (!done) {
  
    // Check to see if there are letters or numbers available to be read
    if (Serial.available()) {     
  
      // Read one letter or number at a time
      // Serial.read() returns the first (oldest) character in the buffer 
      // and removes that byte of data from the buffer
      ch = Serial.read();    
  
      // Check for uppercase letters
      if (ch >= 'A' && ch <= 'Z') {
        Serial.println(ch);
        flash_morse_code(letters[ch - 'A']);        
      }
      // Check for lowercase letters
      else  if (ch >= 'a' && ch <= 'z') {
        Serial.println(ch);
        flash_morse_code(letters[ch - 'a']);
      }
      // Check for numbers
      else if (ch >= '0' && ch <= '9') {
        Serial.println(ch);
        flash_morse_code(numbers[ch - '0']);
      }
      // Check for space between words
      else if (ch == ' ') {
      
        // Put space between two words in a message...equal to seven dots
        delay(dot_duration * 7);       
      }
      // Check for sentinel value
      else if (ch == '!') {
        done = true;  
        Serial.println("Thank you! Your messages were sent successfully.");
        Serial.println("Goodbye.");
      }
    }     
  }  
  // Do nothing
  while(true) {}
}

/**
  *  Flashes the Morse code for the input letter or number
  *  @param morse_code pointer to the morse code
  */
void flash_morse_code(char *morse_code) {
   
  unsigned int i = 0;
   
  // Read the dots and dashes and flash accordingly
  while (morse_code[i] != NULL) {
    flash_dot_or_dash(morse_code[i]);
    i++;
  }
   
  // Space between two letters is equal to three dots
  delay(dot_duration * 3);    
}

/**
  *  Flashes the dot or dash in the Morse code
  *  @param dot_or_dash character that is a dot or a dash
  */
void flash_dot_or_dash(char dot_or_dash) {
  
  // Make the LED shine
  digitalWrite(led_pin, HIGH);
  
  if (dot_or_dash == '.') { // If it is a dot
    delay(dot_duration);           
  }
  else { // Has to be a dash...equal to three dots
    delay(dot_duration * 3);           
  }
  
  // Turn the LED off
  digitalWrite(led_pin, LOW);

  // Give space between parts of the same letter...equal to one dot
  delay(dot_duration); 
}

Video

Setting up the BNO055 Absolute Orientation Sensor

Video Transcript

In this video, we’ll set up the Adafruit BNO055 Absolute Orientation Sensor from scratch, step-by-step.

The BNO055 is actually three sensors in one device. It includes an accelerometer to measure acceleration forces, a gyroscope that uses Earth’s gravity to help determine orientation, and a magnetometer that measures magnetism. Typical applications of this sensor include navigation, robotics, fitness, augmented reality, and tablet computers.

By the end of this video, you will have all of the hardware and software of the BNO055 sensor set up and ready to go so you can measure the roll, pitch, and yaw of an aerial electronic device, such as a drone.

Ok, let’s get started. Feel free to pause the video at any time as you follow along with me.

You will need:

To begin, get your BNO055 IMU board, the header strip, and the solderless breadboard. Lay them out on a table.

Prepare the header strip by cutting it to an appropriate length and placing it into the breadboard. You want a piece with 4 pins and a piece with 6 pins so that they can fit into the holes on the BNO055 board.
Place the long pins down into the solderless breadboard.

Next place the BNO055 board on top of the pins.

We will now solder all of the pins to the BNO055 in order to solidify electrical contact:

  • Gather all the materials presented earlier in the video, and lay them out on a table.
  • If you have safety glasses and latex gloves, put them on. Eyeglasses are fine.
  • Turn on the USB fan for ventilation, in order to avoid breathing in the soldering smoke.
  • Turn on the soldering iron to the 4 heat setting.
  • Lay your damp sponge in the center of the silicon mat.
  • Once the iron is hot, tin the tip by adding a little bit of solder to cover the tip.
  • Now wipe the tip off on the damp sponge to remove all but a thin layer of solder to help the tip last longer and to facilitate the transfer of heat from the soldering iron to the joint.
  • Place the tip of the soldering iron to the joint, where the header pin and board meet, to heat it up.
  • Feed a small amount of solder to the joint and remove the soldering iron after 2 seconds in order to not damage the board. Do this for all the pins in the BNO055.
  • OK, we are done soldering. With the soldering iron hot, wipe it off on a damp sponge.
  • Turn off the soldering iron and clean everything up.

With the BNO055 all cooled off, we need to wire the BNO055 to the Arduino Uno using the solderless breadboard. Here are the connections we need to make.

  • Connect Vin of the BNO055 to the power supply of 5V on the Arduino
  • Connect GND of the BNO055 to GND on the Arduino
  • Connect the SDA pin of the BNO055 to the SDA pin of the Arduino, which is A4.
  • Connect the SCL pin of the BNO055 to the SCL pin of the Arduino, which is A5.

Next, we attach the Arduino Uno to the USB port on our main desktop computer.

Now, on our desktop computer, we open Arduino.

We navigate to the Library manager and install two libraries:

  1. The Adafruit Unified Sensor system library to retrieve orientation data in a standard data format, and…
  2. The Adafruit_BNO055 driver, which supports reading raw sensor data.

Now, go to File -> Examples -> Adafruit BNO055, and click sensorapi. The Arduino sketch that pops up is a demo that will help us test the BNO055.

First double check the Arduino is properly connected to your desktop computer by going to Tools -> Port. Make sure the Arduino/Genuino Uno has a checkmark next to it.

Now, let’s run the demo. Save the sensorapi sketch, compile it by clicking the checkmark, and upload it to the Arduino by clicking the right arrow button.

Once it is finished uploading, open the Serial monitor by clicking the magnifying glass in the upper right corner. We see the x, y, and z orientation data flowing in real-time.

However, we want Roll, Pitch, Yaw data and not X, Y, Z. So we need to change X, Y, and Z to Roll, Pitch, Yaw.

I’m going to use this website to refresh myself what roll, pitch, and yaw are.

Yaw is movement from side to side. Like when you shake your head “no.”

Pitch is movement up and down. Like when you nod your head “yes.”

Roll is movement from left to right. Like when you tilt your head from side to side.

Ok, let’s start the sensorapi program up again and see what happens to the X, Y, and Z values when we move the BNO055 board around, in different orientations. The front of the BNO055 board is labeled BNO055. 

We rotate the BNO055 board around to 90 degrees yaw in the East direction. I see the X value changes. Then we roll the board to the right. The Z value changes. I then pitch the board by pointing the board straight up at the sky. The Y value changes.  So there we have it. Yaw is X, Roll is Z, and Pitch is Y.

We go to the sensorapi code and modify it accordingly so that we use the terms Yaw, Roll, and Pitch, instead of X, Z, and Y, respectively.

Let’s compile and upload the program again to make sure everything is working properly. Here is the output of the Serial monitor.

Ok. That completes this video. You are now ready to use your BNO055.

Setting up the Raspberry Pi 3 in 15 Minutes

Video Transcript

In this video, we’ll set up the Raspberry Pi 3 from scratch, step-by-step. By the end of this video, which will take no more than 15 minutes, you will have all of the hardware and software of the Raspberry Pi set up and ready to go, so you can begin building whatever you want: robots, a weather station, a smoke alarm, a home surveillance system, games and much more.

Ok, let’s get started. Feel free to pause the video at any time as you follow along with me.

You will need:

  • A USB keyboard and mouse. If all you have is a wireless keyboard and mouse that is OK.
  • You will also need a spare computer monitor or television with an HDMI port.
  • An HDMI cord.
  • A computer with Windows, Mac, or Linux.
  • And finally you will need a Raspberry Pi 3 starter kit. The one I’m using in this video is the Canakit Raspberry Pi 3 complete starter kit available from Amazon.

Step 1 – Open the Raspberry Pi Case

First, get your Raspberry Pi 3 kit and open it up. It should come in a small cardboard box. Take out all of the pieces and lay them out on the table.

Grab the plastic case for the Raspberry Pi. It should be inside the small white box that came with the kit. The case is made up of three separate plastic pieces. Stick your thumbs inside one of those notches and pull apart the pieces.

Step 2 – Add the Heat Sinks to the Development Board

Next, we are going to add the heat sinks to our development board. The development board is the green credit card sized board that comes inside the kit. This is your Raspberry Pi computer.

The heat sinks are those two silver square shaped pieces that also come with your kit. Heat sinks enable the Raspberry Pi to remain cool. Heat sinks also help extend the life of the Raspberry Pi by reducing the risk of hardware failure.

Peel off the sticky backing on the underside of the heat sinks. Place them on to the two black squares on your development board. After you do that, your board, should look like this.

Step 3 – Secure the Development Board Inside the Case

Now we are going to secure the Development Board inside the Case. Place your development board inside the bottom part of the black plastic Raspberry Pi case. It snaps into place. Get the middle portion of the case and snap it into place. Finally get the top part of the case and snap it into the middle portion of the case.

Step 4 – Insert the MicroSD card

Next, we need to insert the MicroSD card. Your MicroSD card comes preloaded with software to make the process of installing the operating system for Raspberry Pi much simpler.  

Find your MicroSD card. It should be inside a small plastic bag.

Slide the MicroSD card inside the small notch on the end of the case. Make sure it is in there nice and snug so that it doesn’t fall out.

Step 5 – Attach the Keyboard, Mouse, and Monitor

Next, we need to attach our USB keyboard and mouse as well as our spare computer monitor or television to the black case.

First, plug the keyboard into the USB port on the black case.

Next, plug in the mouse into the USB port on the black case. In this case I have a wireless mouse, so I will plug in the USB dongle to the back of the case.

Now get the HDMI cord. Plug it in to the back of the monitor, and then plug it in to the HDMI port of the board.

The mouse, keyboard, and monitor are now plugged in to the Raspberry Pi.

Step 6 – Power It Up

Next, we need to connect the Raspberry Pi to a power supply.

Get the power adapter and attach it to the back of the case and then plug it into the wall. Take a look at your monitor. Your Raspberry Pi should be powering up.

You will see a prompt to select Raspbian the recommended operating system for Raspberry Pi. Select it and then click “Install.”

Click “Yes” to confirm.

Wait a few minutes for the software to install.

Click OK at the prompt.

Your Raspberry Pi will now reboot to the desktop.

Step 7 – Configure the Settings

Next, we will configure the Raspberry Pi’s settings.

Go to the upper left part of the screen and click the Raspberry Pi icon. Go down to “Preferences” and then click “Raspberry Pi Configuration.” The Raspberry Pi Configuration window will pop up.

Go to the Localization tab. Click “Set Locale”, and select your language and your country. Then click OK.

Now we are going to set the time zone. Click “Set Timezone.” Set your Area and location. For example, I set my Area to America and set my location to Los Angeles. Then click OK.

Now, we are going to set the Wi-Fi country. Click “Set Wifi Country.” I am in the United States, so I will select United States. Click OK.

Now, we are going to change the password. Go to the System tab and click “Change Password.” Enter any password you would like. Confirm your new password. Click OK. Then click OK again.

Click “Yes” to reboot the Raspberry Pi so that all those changes you’ve just made take effect.

OK, we are at the Raspberry Pi desktop again. We now need to make sure that our Raspberry Pi is connected to the Wifi. In the upper right part of the screen, click the Wifi icon and find your Wifi network. Connect to it by typing in the Wifi password and clicking OK.

Step 8 – Get the Internal IP Address of Your Raspberry Pi

I would prefer to access my Raspberry Pi from my own laptop computer instead of the monitor setup I have currently. This way I can access my Raspberry Pi from anywhere in the world. In order to do that, I need to get the internal address of my Raspberry Pi and make some small changes to the settings. So let’s do that now.

Start a terminal session by clicking on the black terminal icon at the top left of the screen. Type the command hostname -I and press enter. You can see in my case, the IP address of my Raspberry Pi is 192.168.0.17. Write this number down on a piece of paper because you will need it later.

Now, close the terminal window and click on the Raspberry Pi icon in the upper left of the desktop. Go to Preferences and then Raspberry Pi Configuration.

Go over to Interfaces. We are now going to enable some of the common interfaces you are likely to use with your Raspberry Pi project. This includes SSH, SPI, I2C, and 1-Wire. Don’t worry about what those terms mean. Just click the enable button next to each and click OK. Click Yes to reboot.

Let’s start a terminal session again by clicking on the black terminal icon at the top left  of the screen. Type sudo raspi-config and press Enter. Go down to option 5 “Interfacing Options” and click Enter. Select VNC. Select Yes to enable the VNC Server. Click OK. Select Finish. Now in the terminal window, type sudo reboot and press Enter to restart the Raspberry Pi.

Step 9a – Connect From Your Personal Computer

Now, we need to download the software on our own personal computer so that we can connect to the Raspberry Pi remotely instead of using the mouse, HDMI monitor, and keyboard setup we have now.

For this step, you need to log on to your Windows, Mac, or Linux computer, open a web browser, and go to the website shown on the screen (https://www.realvnc.com/en/connect/download/viewer/).

Select your operating system and click “Download VNC Viewer.” Follow the instructions for downloading the software.

We’ve finished downloading the software and now need to open it up. On Windows, we can go to the Start Menu and click on “VNC Viewer.”

In the main VNC Viewer window, you need to enter the IP address of your Raspberry Pi so that you can connect to it. This was the number you wrote down on that piece of paper. The username will be pi and the password will be the password you chose for your Raspberry Pi back in Step 7.

If you see your Raspberry Pi’s desktop, congratulations. You can now connect to your Raspberry Pi from your own computer.

If your screen is a little fuzzy and hard to see, you can change the screen resolution. To do that, open the terminal window and type the command sudo raspi-config . Select “Advanced Options”. Select “Resolution” and choose your desired screen resolution. Select Finish to save the changes, and you can now close all the windows. You now have all you need to begin your desired Raspberry Pi projects.

Step 9b – Connect From Your Personal Computer

Some people prefer to control Raspberry Pi through a command line interface instead of the user friendly graphical interface we have now. If you want to do this, you need to download a software called PuTTY.  

Go to putty.org and download the installer for your machine. I am using a 64-bit Windows computer so that is what I will select.

Follow the instructions to download Putty. Once you’ve finished, open up Putty. If you’re using Windows, you can usually find it in the Start Menu.

The first thing you will do is type in the IP address of your Raspberry Pi. If you get a popup window, click “Yes” and then you will go to a black terminal window. Type in the username and password of your Raspberry Pi. The username will be pi and the password will be the password you chose in Step 7. That’s it. You’re logged in to your Raspberry Pi via the command line interface.

Step 10 – Congratulations

At this stage, you can go back to your USB keyboard, mouse, and HDMI monitor and shutdown your Raspberry Pi by clicking the Raspberry Pi icon in the top left of the desktop and clicking “Shutdown”.

Unplug the USB keyboard, mouse, and HDMI monitor and store them away somewhere if you like. You don’t need them anymore since you can connect to your Raspberry Pi from your own personal computer.

Plugging in your Raspberry Pi into any wall socket will restart it so that you can access it from your own personal computer.

Ok. That completes this video. You are now ready to use your Raspberry Pi to build projects. Thank you for watching and enjoy your Raspberry Pi!