Home » Blog » Projects » Electronics Projects » Arduino Analog Sampling – It’s All About Timing

Arduino Analog Sampling – It’s All About Timing

arduino analog sampling

Arduino analog sampling is a lot different than on an ordinary PC.

If I was writing a Morse Decoder to run on a PC, the audio would enter through the sound card. Because a sound card is a separate piece of hardware, it does the work for the computer. The PC just has to receive the chunk of data and do something with it.

Because there is no sound card on the Arduino, the CPU has to do all the work. The Arduino has to run the sampling on an analog input pin as well as process the data that comes out. You can summarize the important differences, as follows.

  • Lower sampling rates. A typical Arduino needs 13 clock cycles of processing per sample. If you do the math, this means the fastest audio sampling on an Arduino is around 8,900 samples per second. This is fast enough for a Morse decoder but not much beyond. A dedicated sound card typically does 44,000 samples per second.
  • Less processing power. Typically, an Arduino runs at less than 1% of the speed of a PC. It also has much less memory.

I plan to implement my Morse decoder on an Arduino Pro Mini 3.3V which only runs at 8 MHz. This means I can only sample at 4,400 samples per second. I set up the following code the check timing on my oscilloscope.


void Digitize()
{
	digitalWrite(probePin, HIGH); 
	goertzel.sample(audioInput);
	digitalWrite(probePin, LOW);
	magnitude = goertzel.detect();
}

The probe pin goes high when sampling starts and low when it ends. Attaching the oscilloscope to this Arduino pin creates the display.

Checking Timing on Arduino Analog Sampling

Typically, Morse code is transmitted between 10 to 40 words per minute WPM. Consequently, the duration of each single Morse code element ranges from 120 milliseconds down to 30 milliseconds. I want to sample at least twice for each element duration so I can catch the rising and falling edges. So, this means that I must sample at least every 15 milliseconds. Can my Arduino handle this?

Short answer is yes, but it depends on how many samples I take. The oscilloscope above shows the timing of collecting 88 samples. Each cycle runs at 15 milliseconds – 10 for data collection and 5 for processing. So far, I have been able to decode Morse and display the results within five milliseconds, around sixty times per second.

There is a trade off with the Goertzel filter. More samples provides better results but takes more time. I will experiment with different sample counts as I go along.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.