Wednesday, October 3, 2018

Audio Test Gadget

While I was waiting on some parts and a set of boards to come in from China, I decided to try making an audio frequency test gadget.
Since I had some of what I wanted to do already done for a Nano, I will see if I can quickly port that over to the Nano Test Gadget mother board.  The mother board is set up for a I2C LCD that I quickly replaced with a 1.3" OLED  display that is also I2C.   Only software required is a driver library from Adafruit, and their graphics library.  This is small but its 128 x 64 pixel display gives fairly good resolution for displaying things like a simulated analog meter and a simple AF spectrum analyzer.  These are things that I have already working on a pro-mini as a stand alone simulated analog meter for a transceiver.

The upper limit for the spectrum analyzer software with this display is about 4Kkz.  which is fine for the audio frequencies usually found in normal HF transceivers.  With that in mind I decided to limit the display range and the tone generator to 4500 Hz.  Using an Arduino tone library, I wrote a small function to generate tones from 50 to 4500 Hz.  This is a square wave output, so I will have to calculate the values for a low pass filter to make the output appear to be more of a sine wave. 

 A function similar to what I used in other projects is used to display the frequency set the value sent to the tone() function. To make it a little easier I just chose 50Hz. as the step size as a compromise between speed and resolution.

Next thing I waned was a AC voltmeter with a frequency response up to 4.5 KHz. at least.  I found some code from Adafruit  for a sound level meter and used  part of that to get the peak to peak value of the AC signal.  This code samples the input pin for  50 ms. and finds the highest and lowest values and uses the difference as the peak to peak value.  After I had it working I checked the values compared to that of a scope over the desired 4.5 KHz. frequency range and found it to be very accurate.

unsigned int  getACVolts( char inputPin) {
#define sampleWindow  50  //  50 ms  20 Hz. sample rate 
  unsigned long startMillis = millis();  // start of sample window
  unsigned int PeaktoPeak = 0;          // peak-to-peak level
  unsigned int SignalMax = 0;
  unsigned int SignalMin = 1024;
  while ( millis() - startMillis < sampleWindow ) {
    sample = analogRead( inputPin);
    if (sample < 1024) {
      if (sample > SignalMax) {
        SignalMax = sample;                                
// saves just the max levels
      else if (sample < SignalMin) {
        SignalMin = sample;                               
// saves just the min levels
  PeaktoPeak = SignalMax - SignalMin;                      
// max - min = peak-peak amplitude
  return  PeaktoPeak ;


Since most of what I work on has a peak to peak value of less than 5 volts I am going with a single range for now.  I could easily switch between the 5 volt reference and the 1.1 volt internal reference for the ADC If I want a more sensitive range,
I decided to just use a current limiting  resistor and forget about a voltage divider for the input.  After getting the p-p value from the ADC, I used that as the value  to find the position of the needle on the simulated display.  This is the same code I have used in several projects detailed in earlier Blog posts.  I decided to convert this to rms for the display scale, and also a digital display on the meter face.  

While I was working on the simulated analog meter for a Bitx, I added some code to do a audio frequency spectrum analyzer display.  While I found this would be mostly useless for a transceiver, it will fit in nicely with this test gadget.  

The code ported over very easily, and after adding a rough frequency scale to the display I was very pleased with the result.  Using the simple menu system I have used on most of my recent projects, I wrapped these up into a fairly simple multifunction test gadget for working with audio frequency circuits.  One thing I did find while trying to get this working is that the combination of the OLED display with its screen buffer stored in the Arduino RAM and the buffers used by the FFT, I quickly started to run out of memory.   It took quite a bit of playing to get every thing to play well together.  But now I can set a frequency on the tone generator and read it with either the voltmeter or the spectrum analyzer.

I have a little work to do on the hardware before this is finished, but it is coming along nicely so far.  The other day I saw a article on a
small Arduino based oscilloscope.  Just thinking about seeing if I can cram another function into this test gadget.