Learn How to Make a Morse Code Transceiver With Arduino

by shu_engineering in Circuits > Arduino

1779 Views, 1 Favorites, 0 Comments

Learn How to Make a Morse Code Transceiver With Arduino

IMG_1843.jpg

In this tutorial, you will learn how to set up an Arduino to communicate using Morse code. Morse code is a communication system that transmits information as a series of on-off tones, lights, or clicks. With an Arduino board, you can create your own Morse code transmitter and receiver.

Supplies

IMG_2048.jpg
IMG_2049.jpg
IMG_2050.jpg
IMG_2051.jpg
IMG_2053.jpg

Arduino board (i.e. Arduino Uno)

Breadboard

Resistor and Photoresistor

Screen

Jumper wires (about 10)

Installing Arduino Software on MacOS and Windows

Installing Arduino Software on MacOS

  1. Make sure your software is running at 10.13 or above.
  2. To download the Arduino IDE go to https://www.arduino.cc and on the Arduino homepage, click on the Software menu and select Downloads. Click on Mac OS.
  3. Press the download button, locate the downloaded Arduino IDE file (usually in your Downloads folder), and double-click on it to open the installer.
  4. If the page doesn't open automatically to Applications, Since you don't want to have the Arduino IDE in your Downloads folder, go to Finder, open Downloads, click the downloaded Arduino software, and move it to Applications.
  5. To launch it, go to your Applications folder and find the Arduino application. Double-click on it to launch the Arduino IDE.

Installing Arduino on Windows

  1. Go to the Arduino website at https://www.arduino.cc
  2. Click on the Software menu and select Downloads. Click on Window installer.
  3. Once downloaded, open the downloaded file and locate the downloaded Arduino IDE file (usually in your Downloads folder) and double-click on it to open the installer.
  4. To run the installer, click Next to proceed through the installation process.
  5. Connect your Arduino board to your computer.
  6. Go to "Tools" and select the board (Arduino UNO). Then, go to the Port submenu and select the Arduino UNO port.
  7. To make sure that the Arduino IDE is installed correctly, you can open the Blink example from the File menu, select the Arduino UNO board and port, and click on the Upload button. If everything is set up correctly, the onboard LED on your Arduino board should start blinking.

Uploading a Blinking Code

Uploading a Blinking Code

  1. First, open the Arduino app.
  2. Click on File > Examples > Basics > Blink. When you click on Blink a new window will open that says Blink on the top.
  3. In order for it to know where to send the code, go to Tools > Arduino AYR Boards > Arduino UNO. We choose Arduino UNO because that is the type of board we are using.
  4. Next, go to Tools > Port > and click the serial port that says Arduino UNO. On Windows, it may not show up as Arduino UNO in the Port options, it may come up as COM 3 or COM 4, so you can unplug and replug your Arduino into the computer and see which one is the new one. This lets it know where to talk to.
  5. Plug in your Arduino, click Upload, and the light should be on for one second, and off for one second.

Blinking External LEDs Using Arduino Uno

blink code screenshot.png
Photo 1.png
IMG_1851-fotor-2023083002441.jpg
IMG_1852-fotor-2023083002647.jpg
IMG_1853-fotor-2023083002827.jpg
IMG_1854-fotor-2023083003228.jpg
IMG_1855-fotor-202308300317.jpg
IMG_1857-fotor-2023083003348.jpg
IMG_1858-fotor-2023083003438.jpg
IMG_1859-fotor-2023083003536.jpg

How to Make the LEDs Blink on A Breadboard

  1. Gather your supplies: an Arduino UNO board, a breadboard, two LEDs, two resistors, and three wires (Photo 1).
  2. Put the long end (the positive end) of one LED (in the photos it is white) in one row, and put the other end in a different row (Photo 2).
  3. Put the resistor in the same row as the short leg, and the other end in an empty row (Photo 3).
  4. Do the same thing with your second LED (mine is blue), the longer leg in one row, and the shorter leg goes on the same line as the second resistor (Photos 4 and 5).
  5. Make sure you put the end of both resistors on the same line (Photo 6).
  6. Connect a wire to the same line as the resistors, and connect the other end to GND on the Digital side of the port (Photo 7).
  7. Then connect another wire to the long leg of the second LED (blue) line, and the other end to a digital output, I chose nine (Photo 8).
  8. Do the same thing with the other LED, connecting the wire to the same line and to a digital output, I chose ten (Photo 9).
  9. Connect your Arduino to your computer if it isn't already.
  10. Go to your code, and change pinMode(LED_BUILTIN, OUTPUT) to pinMode(9, OUTPUT) or whatever Digital port you chose to connect your first LED to.
  11. Now copy and paste that same line, and replace 9 with 10, or whichever port you chose for your second LED.
  12. Do the same replacing LED_BUILTIN with the digital port numbers below at digitalWrite(9, HIGH) and digitalWrite(19, HIGH).
  13. Press upload and voila!

Morse Code Basics

Morse-code-tree.svg.png
morse code drawing.png

Morse code is a system of communication that uses combinations of dots and dashes to represent letters, numbers, and other characters. It was widely used in telegraphy and is still relevant today, like in radio and communication-based projects such as this Arduino one. When using light to communicate, the light blinks short for a dot, and long for a dash (reference the photo to see how it looks). There is a Morse code tree that simplifies each letter and how to decipher the code (reference photo). Using these dots and dashes, we will build a code that translates Morse code into letters.

Different Ways to Practice Morse Code

Learning Morse code will make understanding what you are coding easier, so here are some ways you can practice them:

Dot = Squat

Dash = Plank

Space = Snapping three times (for a three-unit space)

For example, to make the word "Today" you would do 1 plank ; snaps ; 3 planks ; snaps ; 1 plank 2 squats ; snaps ; 1 squat 1 plank ; snaps ; 1 plank 1 squat 2 planks ; end. And you would be spelling out this : - --- -.. .- -.--

You can come up with your own fun ways to memorize Morse code!

How to Set Up the Circuit

sender drawing.png
IMG_2025.JPG
IMG_2024.JPG
IMG_2023.JPG
IMG_2022.JPG
IMG_2021.JPG
receiver drawing.png
IMG_2020.JPG
IMG_2019.JPG
IMG_2018.JPG
IMG_2017.JPG
IMG_2016.JPG
IMG_2015.JPG
IMG_2014.JPG
screen drawing.png
IMG_2013.JPG
IMG_2012.JPG
IMG_2011.JPG
IMG_2010.JPG
IMG_2009.JPG
IMG_2008.JPG
IMG_2007.JPG
IMG_2006.JPG

Here is a step-by-step tutorial on how to completely set up the Arduino circuit:

Building the Sender

D12 to LED to Resistor to GND

  1. Connect a jumper wire from the Digital side of the board on D12 to a spot on a breadboard (Photo 1).
  2. Since we want to connect it to an LED, we put the longer wire of the LED on the same line as the jumper wire and put the shorter wire on a different line (Photos 2 and 3)
  3. We then put one end of the receiver on the same line as the shorter LED wire, and attach a jumper cable to the same line as the lone end of the receiver (Photo 4).
  4. Then you connect the other end of the jumper wire that is joined with the receiver to the GND port on the Digital side of the Arduino (Photo 5).

Building the Receiver

Power (5V) to Photoresistor to Analog 1 to Resistor to GND

  1. Connect a jumper cable to the 5V port on the Power side of the Arduino (Photo 6).
  2. Connect the other end anywhere on a new breadboard (Photo 7).
  3. As the photoresistor doesn't have any polarity, connect one side to the same line as the jumper wire (Photo 8).
  4. Connect the resistor to the same line as the photoresistor (Photo 9).
  5. Finally, connect a wire to the same line as the resistor, and the other end to GND, situated directly next to 5V (Photo 10).
  6. To connect the Analog in between the photoresistor and resistor, locate A1 on the Analog side of the Arduino, and connect a wire to it (Photo 11).
  7. Then connect the other end of the jumper wire on the same line as the photoresistor and resistor (Photo 12).

Building the Screen

GND --- GND

VCC --- 5V

SDA --- A4

SCL --- A5

  1. Connect your first wire to the GND space on the screen. Place the other end on the last remaining ground hole on the board (Photo 13).
  2. Connect the next wire to the VCC space on the screen, and since the 5V is being used by the resistor, connect the wire to the same line on the breadboard as the resistor and the wire that is connected to 5V (Photo 14).
  3. Then connect a wire to the SDA port on the screen and the A4 port on the analog part of the Arduino (Photo 15).
  4. Finally, connect a wire to the third and last wrung on the screen, the SCL, and connect the other end to A5 on the Arduino (Photos 16 and 17).

You have now set up your circuit! For added instructions, you can go to the SHU youtube channel and watch the video with the step-by-step instructions.

Downloading the Morse Code Transceiver From GITHUB

  1. Go to https://github.com/shumakerspace
  2. Click on morse-code, and it will take you to a page, where you will see a green button that says Code, and click it, and it will download the code for you.
  3. Open the file, and you will see the Sender, Receiver, and the Transceiver. Click on the Transceiver, and it will open up the code on the Arduino app.
  4. There you have the code!

Installing Liquid Crystal I2C for Arduino Libraries

When uploading your code for the first time, you might run into an error that highlights the line red. It is because we are missing that library (a bundle of code that allows us to operate equipment), in our case the liquid crystal display. So to install that library, go to Tools > Manage Libraries. It will open up a tab that shows all the libraries you can upload to Arduino. In order to find the one we need, type LiquidCrystal_I2C onto the search bar. Click on the one that has the exact title and is by Frank de Brabander. Click the latest version and install. Press upload again and it should work.

Why Do We Use a Resistor With LEDs

The main reason for using a resistor when using LEDs is that the LED will blow up if you don't. To make sure the LED doesn't get damaged and works correctly, we use a resistor. The resistor acts like a traffic controller, controlling the flow of electric current, and it makes sure that only the right amount of current flows through the LED. It also helps adjust the voltage because sometimes, the power source provides more voltage than the LED needs, and the resistor acts like a voltage regulator, reducing the excess voltage so that the LED gets the right amount.

Morse Code Circuits Explained

IMG_1876.jpg

LEDs (light emitting diode): one leg is longer than the other, with the longer one having a positive charge and the shorter one having a negative. The circuit symbol or diode symbol, makes sure that the current only goes one way.

Resistor: the resistor resists the current or electricity. There are different values, which the lines of different colors represent. And the resistor zig-zag symbol creates more resistance for the electricity.

Photoresistor: the photoresistor senses the light, it can change its value unlike, the resistor which has a fixed value. The circuit symbol looks like the resistor except it has a circle around the zig-zag because when light comes through the circle, the resistor value changes.

Morse Code Receiver: We apply 5V, a photoresistor, and a resistor. Depending on the value of the resistance, it provides the corresponding voltage from the 5V. The voltage divider is in between each resistor. As the voltage in the circuit is between 0 and 5 volts, it can change based on how much light the resistor gets. If we connect the divider to the analog port of the microcontroller, we can know exactly what the value is, therefore we know how much light there is and how much to use.

Morse Code Sender: there is one LED and one resistor. We need to use a resistor because if we don't the light burns up. The digital port connected to the microcontroller sends 1s and 0s so that the LED blinks.

Morse Receiver: we put the LED and photoresistor facing each other, so when the LED emits light, it changes the voltage, we read it on the analog voltage from the microcontroller. So with the code, we know how much light is coming and how much the frequency is so we can tell what code is coming.

Screen: the microcontroller knows what the message is, but we don't, which is why we have a screen. It has four connections that are connected to the microcontroller, ground, 5V, clock, and data. All four of these help us see the message on the display.

Morse Code Transceiver Code Explained

screenshot code tranceiver.png

When looking at the code, there are two main parts on top. The USER SETTINGS and the DEBUG SETTINGS. Under unit settings, it says timeUnitLength, which gives the speed of the Morse code. You can change it, therefore, changing how fast the Morse code will blink. Under it, in MorseLed it asks you to choose the pin where the LED is connected. You can change the pin to match what you have on your Arduino. The ControlLed is the onboard led. And the photoresistor pin is A1 as it is an analog pin. The debug settings is for more advanced users, so don't pay any attention to it for right now.

Morse Code Transceiver Software Explanation

Sender:

  1. Read Serial Input: On the transceiver, the code needs to read the serial input. Whatever you typed on the serial window on the computer needs to be sent to the Arduino.
  2. Translate to Morse Code: We need to get the characters that were typed, and then translate it into Morse code. To do this we have a library of each letter that translates to the morse code equivalent.
  3. Ending the Letter or Word: Each character is not the same length in Morse code. For example, e is only one dot whereas some other characters might be five dots long, so we need to know exactly when it stops. To do this, every time a letter is read from the serial input, there needs to be dots and dashes, but there also has to be a sign that says signifies the end of a letter. If there was nothing to signify the end of a letter, it would be just a long strip of dots and dashes and the computer would never know when to stop reading one letter and start reading the next.
  4. Translate to On/Off for LED: The last thing that needs to be done is having the Arduino send the Morse code to the LED. We have to transform the Morse code to the high and low of the LED. So what we do in the code is have it turn the light on and off accordingly.

Receiver:

  1. Reading the Photoresistor: we need to read the photoresistor so we can see how much light goes into it. But there's a challenge with doing so and this applies to anything on the Arduino. It does everything very fast, it reads the sensor, it translates whatever it has to translate and then it goes back to reading. Even though the light is on for a short amount of time it's going to be enough for the Arduino to loop once over it. I will explain more in the future.
  2. Decide if it is High or Low: We know how much light hit the sensor but we don't know if it is a high or a low, and to decide this we need to decide on the threshold. Because it's an analog value we're going to need to say this is an equivalent of a low and this is an equivalent of a high, and we usually pick the middle and say anything that's above the threshold line becomes a high and anything under that line becomes a low. And then we end up with something like a high and a low or three highs and one low.
  3. Translate to Morse Code: In Morse code, one dot is one high and one low a dash is three highs and one low, and we need to translate that. What we then need to do is translate it back to characters so again we use a dictionary or library inside of the code to translate it. And when all is done and good, we have the message.
  4. Display on the LCD and Serial: The way we display it back to the user is by talking over a communication protocol called I2C and we talk to the LCD display and we send the characters over to display to the user. At the same time over your serial connection, you also see both the Morse and the translated character.

Using a Cell Phone As a Sender

Using the sender with the LED isn't the only way to signal to the receiver the code. You can also use a Morse Code app on your phone that emits light in Morse Code. Put in whatever you want to signal and it will flash your message to the receiver. However, it might not show the word you typed, because the phone and the sender-receiver are not talking at the same speed. To change this, go to the Debug Settings, go to debugTiming, and change false to true. This is going to activate lines of code that are going to help us understand what's going on with the phone. Now upload the code to the Arduino. Open the serial monitor. Now when you flash your message, times will appear on how long the flash is on and off. You may find that your message is speaking at 200 or 300 milliseconds, so just adapt the time in User Settings to the lowest time that you see and now it should show you the correct message.

How the Photoresistor and Circuit Work

In the debug settings of the code, you will see a Debug Plotter line, and next to it, it says false. If we change the false to true, click upload, and open Tools > Serial Plotter, a new window will appear with what the photoresistor values are seeing. The red line is the threshold value, and the blue line is what the photoresistor is reading. When you shine a light on the photoresistor, the lines will go to 1, as the LED gives ones and zeroes. Interpreting the serial plotter gives us an understanding of how the photoresistor and circuit work together.

FAQs

  • Why isn't the uploaded transceiver code working? What can I do to fix it?

Try seeing if you have the Liquid Crystal I2C library installed. This may be the issue because without it the code will not run. Go to step 8 and follow the instructions.

  • The LED is blinking but the code isn't showing up on the screen or is showing incorrect letters.

This may be because either the LED is too far away from the photoresistor, or because the photoresistor is being moved too much. Make sure they are both facing each other directly and are not being moved.

  • What is the value of the photoresistor?

The photoresistor is connected via wire to a 5V port, which gives it its value. This means that the value of the photoresistor can be anywhere from 0 to 5 volts.