Skip to main content

Morse Code to Text using LDRs

Okay, so the idea was to convert light signals (Morse code) to text that would be displayed on the computer screen via the Serial Monitor. So, I set up an LDR circuit (in voltage divider configuration) to test the brightness of light. When the intensity of light is high, the LDR has low resistance and thus most of the voltage difference is across the 100k-ohm resistor, giving us a high voltage (analog values > 1000), and when there's no light values of 300-400 were common. To prevent ambient/surrounding lights from influencing the LDR, I set up a closed box housing the LDR+Arduino with a small hole, where the signal would be flashed.

The circuit
 Essentially the circuit is this:



The Morse Signal would be flashed using my cell-phone back camera flash using an app called Morse Code Flasher.

The opaque box - there are two holes - one for the light/signal and another on the side to run in the USB cable to Arduino.

Inside of the box
 
After constructing an opaque box to house the LDR circuit and arduino I ended up with the following results:
  • In darkness, general analog values < 100.
  • In light, general analog values > 1000

Another view of the inside - the paper separating the Arduino and the breadboard is to prevent the onboard LED lights from affecting the LDR.

This was the simple part. However, a few challenges that cropped up:
  1. We don't know the base time (or time taken for a dot) of the Morse Code. Morse code specifies that a dash should be thrice the length of a dot, but does not exactly specify the time for a dot. We can use different "base times" to transmit the same message. I attempted to solve this by measuring the duration of the first 10 intervals of light/darkness and use this to estimate the base-time which would then be used to translate Morse to text. 
  2. Since this is an analog signal there is no such things as fully off/on for the light. So we need to develop threshold above which we consider the bulb to be on, and threshold below which the bulb is said to be off. I used 800 as the cutoff for light, and 200 as the cutoff for darkness.
All in all, this was more challenging than expected... and sadly did not work. The code I wrote simply refused to give out any output - also the base-time estimates that it showed were waay of the mark.

View the code I used here. 

Despite a lot of trials in trying to debug, the errors persisted, and for today, I give up. I spent more than 6 hours on this and am just bored of it. Possible reasons for errors:
  1. Maybe the LDRs response time is not fast enough to note the quick transition from light to dark associated with a dot. When I tried sending the morse signal "SOS" the program could generally detect only 5-6 on/offs, while there should be atleast a total of 9. This however, doesn't explain the fact of why the code doesn't do better even when the transmission speed is decreased (to even as low as  50 dots/minute or basetime of ~0.8s)
  2. Maybe the threshold values of 800 and 200 that I chose are causing the problems. Maybe the limits I set are too strict... But then - general tests showed analog values > 1000 when my cell flash was on and <100 when there was no light. So, the boundaries should work. 
  3. If you've looked at the code you'll notice that:
    • First 10 durations of dark/light are observed. 
    • Then the program does a bunch of calculations to determine base-time
    • While those calculations are being done, we do not continue to check the brightness levels. This *might* cause us to miss a quick flash of light or two, but I think it's unlikely - C is an extraordinarily fast language, and those instructions can't slow it down more than a few milliseconds at most. 
Things to try:
  1. Simplify the program. Maybe stick to a fixed base-time that is entered by the user. 
  2. Debug the existing program. 
Edit (4/July/2018): Finally got the program working (somewhat). The main problem I had yesterday was how do I know the time duration of a dot? Yesterday I tried determining that automatically, but that didn't really work. The "auto-detection" code I wrote was a miserable failure (I tried to save the time durations of the first five light flashes and the darkness between them. The light flashes/darkness must either be x  or 3x - using that info, I tried arriving at an average dot duration or 'x' value by weighted averages but that was a miserable failure). Today, I got three ways for asking the user to enter the base time/duration of a dot:

  • Ask user to input at what speed he'll transmit in (in units of WPM or words per minute) - buuut there's no standard value for 1 WPM as words vary in length. Wikipedia said dot duration = 1200/(WPM value) but that again was an approximation. Also, asking users to type in stuff is lame.
  • Ask user to first transmit the letter 'E' and measure the time period of flash. Since morse codeof letter 'e' is just a dot ('.'), we get dot duration. 
  • Another approach would be to ask the user to first transmit the string "EEE" and average the time duration of the three flashes
I wrote code for all the above three approaches but ended up using the second option because it was working the most effectively + simplicity.

Click here for today's code.

A screenshot of the program at work. The string I entered was the alphabets "ABCD...Z"

With that out of the way, more problems cropped up today:
  1. That morse code that the Android App is flashing is kinda weird - dashes, on average, are only 2.25 times more than dots; according to official morse rules, dash = 3 * dot.
  2. The program detects the first few letters okayishly. After the 5th or 6th letter it starts messing up - maybe it is due to fatigue of the LDR due to the constant changes in light. After a while perhaps the LDR just gets saturated and doesn't respond as quickly to changes in light intensity. 
  3. It's slow. Anything faster than 5WPM and accuracy in results takes a plunge. The LDR isn't fast enough to detect Morse at 40-50WPM.
Well, that's that! Time to move on to something else, I guess!

PS: I made another small change in the final version. In all versions before that, I had two threshold values - a value above which it was considered light (800) and a value below which it was considered dark (200) - between 200 and 800 it was neither dark nor light. In the final version, I ditched that in favor of a binary/digital cutoff - above 800 it's light, and below it it's dark. That change helped quite a but in getting better results.

Comments

Post a Comment

Popular posts from this blog

So, WebDev: My First Project Idea

Okay. So it's summer. And there's tons of time. So I'm trying to figure out WebDev cuz why not. Some people say that WebDev is not real "coding/CS" per se - but who cares? I'm not doin' it cuz it be CS, I'm doin' it because I want to build something I can see, ok? Anyways, I digress. Since this is my first post here - I'll make the purpose of this blog clear: to keep a record of what I'm trying to do so I can laugh at myself half a decade later. That's it. No other purpose. Nothing. Zero. Nada. Zip. The Idea: A TShirt Exchange Portal (copy-pasting the idea I typed in a notepad file at midnight yesterday): Project statement : To create a T-Shirt exchange website that would enable the students of BPHC to exchange T-Shirts bought on SWD Portal, if they have the same size. Impact : This project on completion will make it much more simpler and faster to exchange TShirts. A process that today involves tons of posts on FEG spamming...

Java MiniProject

This was a small Java Mini-project I made as a part of a team of three as partial credit towards our CS F213 (Object Oriented Programming Course). Click here for the source code on GitHub . It's a hotel booking application where users can create accounts, new bookings and manage their bookings. Installing the required software itself took more time than expected. We started with Oracle NetBeans and shifted to Apache NetBeans 9 IDE, because it supports the latest version of Java/JDK. This was also the first time we tried to use git (and failed miserably). We ended up just merging our parts of the work by hand (the old-fashioned way - by email and copying source files from one pen drive to another) and then finally one person pushing/committing all the changes. Now we know how.  This was something made kind of in a rush on the last day, and the code isn't perfect. The project used a MySQL database to store the room, user information and JDBC to connect to the database. The G...